MJV
MJV

Reputation: 1882

How to wait until a file is available in a Jake build (Node.js)?

Is there a way in a Node.js Jake build to wait until a certain file has been copied, and advance to do some operation only after the destination file can be found? I think this question pretty much comes down to "is there a way to copy files synchronously in Node.js/Jake?" (Perhaps something else than writing something from scratch, using the combination of fs.readSync and fs.writeSync.)

Background:

I'm developing a web app that is run on Node.js (with Express) during development, but will be deployed on a Java server in production. (We use Jade and Stylus in the client and Express enables us to run the app without generating all the HTML files etc. and deploying it after every change.)

I use Jake for making the build, i.e. generating HTML files from Jade files and CSS from Stylus files etc. Now I'm also trying to concatenate all of the app's JavaScript files into one minimized file and change all the HTML files to use that instead of all the separate JS files that are used in "raw" form during development.

However, I now have a problem with that last step. My idea was to copy all of my Jade files into a temporary directory for the deployment build and replace the reference (in a Jade file used as a header on all HTML pages) to a list of all separate JS files to the one that has just been generated by concatenating and minimizing the whole bunch. But as I first copy all of the Jade files to another location (which happens asynchronously) and try to edit one of the files, opening the file always fails since the copy operation hasn't really finished yet.

This is what I have now (in a simplified form) in my jakefile:

var fs = require('fs');
var fse = require('fs-extra');
var path = require('path');
var glob = require('glob');
var Snockets = require('snockets');
var snockets = new Snockets();

// generating the minimized JS file
snockets.getConcatenation(baseDir + '/scripts/all.js', { minify: true }, function(err, allJs) {
  if (err) {
    throw err;
  }

  fs.writeFileSync(generatedJsFileName, allJs);
});

// copying all the Jade files to a temp dir
glob.sync('**/*.*', {
  cwd : srcDir
}).forEach(function(file) {
  var loadPath = srcDir + '/' + file;
  var savePath = targetDir + '/' + file;
  fse.mkdirsSync(path.dirname(savePath));
  fse.copy(loadPath, savePath);
});

// trying to read one of the copied files (which fails, since the file cannot be found yet)
fs.readFile(targetDir + '/views/includes/head.jade', 'utf8', function(err, data) {
  ...
});

This might be a stupid question, and a stupid way to try to solve the problem in the first place. So, also suggestions for a better approach are very welcome.

Update:

I also tried using Parseq, putting each operation (creating the JS file, copying the Jade files, reading one file) in its own function, but even that gives me the same error. If I run the script several times without deleting the target directory of the copy operation in between, the file can be found. So e.g. the path is correct and the problem really seems to be about timing.

Upvotes: 0

Views: 426

Answers (1)

MJV
MJV

Reputation: 1882

I didn't really find an answer to the main question so I don't know if this helps anyone else facing the same problem. But I did find a way to get around the problem.

I ended using the same original Jade files for the two different conversions, but in the second conversion I use a custom js function to change the script tag reference to point to the minified file.

I.e.

var data = jade.compile(str, { filename: file, pretty: true })({
  css: function(path) {
    return '<link rel="stylesheet" href="/styles/' + path + '.css" />';
  },
  js: function(path) {
    var name = '<script src="/scripts/';

    if (path == 'all') {
      name += generatedJsFileName;
    }
    else {
      name += path + '.js';
    }
    name += '"></script>'; 

    return name;
  }
});

It might not be the prettiest workaround but it works.

Upvotes: 0

Related Questions