6

I've found myself in a situation where I'm having to run a single command e.g. node compile.js

that .js file needs to run the following

browserify -t jadeify client/app.js -o bundle.js

All the dependencies are installed, and by running this command in the CLI works fine, just need to figure out how to execute it from within a node script.

We've also got inside our package.json the following which contains something similar to

"script" : [ "compile": "browserify -t jadeify client/app.js -o bundle.js" ] this works perfectly when you execute cd /project && npm run compile via ssh however not via exec

Thanks

brandonscript
  • 68,675
  • 32
  • 163
  • 220
owenmelbz
  • 6,180
  • 16
  • 63
  • 113
  • Other than invoking a separate process, you can also use [browserify api](https://github.com/substack/node-browserify#api-example) in your `compile.js` file. – hassansin Aug 07 '15 at 13:29
  • Sure, but this is what I dont know how to do, so needed an example/it done - cant find any results on google that are helpful – owenmelbz Aug 10 '15 at 07:53
  • possible duplicate of [node.js shell command execution](http://stackoverflow.com/q/14458508/1168892)? applying the solution there leads to `require('child_process').spawn('browserify', ['-t', 'jadeify', 'client/app.js', '-o', 'bundle.js])` – Dominik Schreiber Aug 10 '15 at 08:10

2 Answers2

2

You should be able use the api-example and extend it with the transform as suggested by the jadeify setup paragraph.

var browserify = require('browserify');
var fs = require('fs');
var b = browserify();
b.add('./client/app.js');

// from jadeify docs
b.transform(require("jadeify"));

// simple demo outputs to stdout, this writes to a file just like your command line example.
b.bundle().pipe(fs.createWriteStream(__dirname + '/bundle.js')); 
Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
  • Thank you Simon for your generous input! I'll have a look at this tonight - I've never actually done any node scripting so this is 100% new for me, so your clear and simple response is much appreciated. Would you however be able to point me in the direction as to how to pass a variable/string into the script? e.g `node compile.js --lang enGB`, many thanks – owenmelbz Aug 10 '15 at 14:10
  • @owen You should have a look at https://www.npmjs.com/package/minimist, as hinted at in the answers on http://stackoverflow.com/questions/4351521/how-to-pass-command-line-arguments-to-node-js – Simon Groenewolt Aug 11 '15 at 10:17
  • @OwenMelbourne did it work? I see you put a bounty, but why ask about php exec when it is not in the original question? (You should be able to run the command-line version using php exec just fine, if it isn't working it's probably an issue with the path to the executable, an environment setting or something permission-related) – Simon Groenewolt Aug 15 '15 at 12:06
1

You can access script arguments via process.argv.

An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.

You can then use the browserify api together with jadeify to get what you need.

var browserify = require('browserify')();
var fs = require('fs');

var lang = process.argv[2];
console.log('Doing something with the lang value: ', lang);

browserify.add('./client/app.js');
browserify.transform(require("jadeify"));
browserify.bundle().pipe(fs.createWriteStream(__dirname + '/bundle.js'));

Run it with $ node compile.js enGB

Robbie
  • 18,750
  • 4
  • 41
  • 45