Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate through directory with Assets.getText

In meteor I can read a file like this:

myjson = JSON.parse(Assets.getText("lib/myfile.json"))

Now i want to iterate through a folder, and read all the available json files. What would be the best way to do this without installing extra NPM packages. Thank you for your time.

like image 284
Johannes Avatar asked Oct 14 '13 21:10

Johannes


2 Answers

I'm not sure if this is the best way, but is certainly an easy one:

var fs = Npm.require('fs');

fs.readdir('./assets/app/myFolder', function(e, r) {
    _.each(r, function(filename) {
        Assets.getText('myFolder/' + filename);
    });
});
like image 160
Hubert OG Avatar answered Oct 21 '22 02:10

Hubert OG


I wrapped Hubert OGs code into a function with Meteor.bindEnvironment. I believe this is necessary because of fibre not available outside of the Meteor environement. see https://www.eventedmind.com/feed/49CkbYeyKoa7MyH5R

Beware that external Node packages have different document root than Meteor.

var done, files;

var fs = Npm.require('fs');

files = fs.readdirSync("../../../../../server/collections/lib/", function(e, r) {});

done = Meteor.bindEnvironment(function(files) {
  return _.each(files, function(filename) {
    var myjson;
    myjson = JSON.parse(Assets.getText("lib/" + filename));
    /* do Something */

  });
}, function(e) {
  throw e;
});

done(files);
like image 44
Johannes Avatar answered Oct 21 '22 02:10

Johannes