shelljs node module
Let me share a little gem I've found: shelljs node module. I was working on updating my Cordova hooks to work with Windows environment and had to replace my bash script with the corresponding JS code. Unfortunately, the node modules I've found were disappointing, especially copying the files. Surely I could write the copying code by myself, but I supposed that in 2016 year one is not required to do such menial tasks.
The biggest problem for the modules I've tried was the glob matching - I have to copy files from the sibling directory, and modules couldn't find it (looks like they don't parse ".." notation). I've tried fs.extra, fs-extra, ncp, filecopy, cp, but no luck. I guess I could find the good module after all, but after I've discovered shelljs I don't need it anymore anyway. With shelljs porting the code was a piece of cake - see the results below (the original and the new scripts).
The biggest problem for the modules I've tried was the glob matching - I have to copy files from the sibling directory, and modules couldn't find it (looks like they don't parse ".." notation). I've tried fs.extra, fs-extra, ncp, filecopy, cp, but no luck. I guess I could find the good module after all, but after I've discovered shelljs I don't need it anymore anyway. With shelljs porting the code was a piece of cake - see the results below (the original and the new scripts).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "Building Web Project."; | |
cd ../web/; | |
gulp; | |
cd ../cordova/; | |
echo "Deleting files in ./www"; | |
rm -rf ./www/*; | |
echo "Copying files from ./web/dist to ./www"; | |
cp -r ../web/dist/* ./www/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = (ctx) => { | |
var Q = ctx.requireCordovaModule('q'), | |
shell = ctx.requireCordovaModule('shelljs'); | |
var gulpDeferral = new Q.defer(), | |
exitDeferral = new Q.defer(); | |
console.log("Building Web Project."); | |
shell.cd('../web/'); | |
shell.exec('gulp', (error, stdout, stderr) => { | |
if (!error) { | |
gulpDeferral.resolve(); | |
} else { | |
exitDeferral.reject('gulp failed!'); | |
} | |
}); | |
gulpDeferral.promise.then(() => { | |
console.log("Deleting files in ./www"); | |
shell.cd('../cordova/'); | |
shell.rm('-rf', 'www/*'); | |
console.log("Copying files from ../web/dist to ./www"); | |
shell.cp('-Rf', '../web/dist/*', './www/'); | |
exitDeferral.resolve(); | |
}); | |
return exitDeferral.promise; | |
}; |
Comments
Post a Comment