Thursday 15 June 2017

[Help] - 'npm run script1.js; npm run script2.js' vs synchronous exported functions via bin executable

I'm currently testing two approaches to some build scripts for a set of projects and running into (what appears to be) a race condition when executing npm scripts in different ways.The two specific scripts first (script1) copy a set of files into a single directory, and then (script2) modify and re-write said files based on some regular expressions.script1.js:for (let file of files) { let fileSplit = file.split("/"); let fileName = fileSplit[fileSplit.length - 1]; let readStream = fs.createReadStream(file); let newFile = path.join(outDir, fileName); readStream.pipe(fs.createWriteStream(newFile)); } script2.js// some regex modifications fs.writeFileSync(file, code, "utf8"); When running these back-to-back as an npm script via my package.json, the files are properly copied and then modified accordingly:{ "scripts": { "build": "npm run script1; npm run script2;" } } However, when running these as a bin command, it seems as if many of the files (most) are not properly copied from script1, before the second exported function from script2 is run.var script1 = require("script1.js"); var script2 = require("script2.js"); script1.main(); script2.main(); { "bin": { "build": "./index.js" } } It feels like using these exported functions as a bin command via index.js is encountering a race condition/asynchronous issues compared to the semicolon delimited npm script approach. Any advice on why this occurs/how to resolve it?Thanks

Submitted June 15, 2017 at 08:33PM by fugular

No comments:

Post a Comment