运行 节点包 + 来自另一个脚本的参数

Run Node Package + Arguments from another script

我发现自己处于不得不 运行 单个命令的情况,例如node compile.js

该 .js 文件需要 运行 以下内容

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

所有依赖项都已安装,通过 运行在 CLI 中执行此命令可以正常工作,只需要弄清楚如何从节点脚本中执行它。

我们还在 package.json 中获取了以下内容,其中包含类似于

的内容

"script" : [ "compile": "browserify -t jadeify client/app.js -o bundle.js" ] 当您通过 ssh 执行 cd /project && npm run compile 但不是通过 exec

时,这非常有效

谢谢

您应该可以使用 api-example and extend it with the transform as suggested by the jadeify setup 段落。

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')); 

您可以通过 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.

然后您可以使用 browserify api together with jadeify 来获得您需要的东西。

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'));

运行 它与 $ node compile.js enGB