Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wait until a file is available in a Jake build (Node.js)?
    primarykey
    data
    text
    <p>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 <code>fs.readSync</code> and <code>fs.writeSync</code>.)</p> <p><strong>Background:</strong></p> <p>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.)</p> <p>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.</p> <p>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.</p> <p>This is what I have now (in a simplified form) in my <code>jakefile</code>:</p> <pre><code>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) { ... }); </code></pre> <p>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.</p> <p><strong>Update:</strong></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload