node.js 通过 npm 自动运行 gulp 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27078245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:09:07  来源:igfitidea点击:

Automatic run gulp tasks via npm

node.jsnpmgulp

提问by Howard

I usually run gulp via npm, e.g. in my package.json

我通常通过 npm 运行 gulp,例如在我的 package.json 中

"scripts": {
    "test": "gulp test",
    "minify": "gulp minify"
}

Then I can run command such as

然后我可以运行命令,例如

npm run minify

Which is okay, but every time I've new tasks in my gulpfile, I need to add them to the package.json under the scripts section, is there any better way to do so?

这没关系,但是每次我的 gulpfile 中有新任务时,我都需要将它们添加到脚本部分下的 package.json 中,有没有更好的方法呢?

Reason: I only install npm globally to my pathso all other modules will not pollute my path, so I need to run them via npmscripts

原因:我只将 npm 全局安装到我的,path所以所有其他模块不会污染我的path,所以我需要通过npm脚本运行它们

采纳答案by Austin Pray

Have I got a treat for you: I went ahead and made you a simple npm module to handle this.

我有没有为您服务:我继续为您制作了一个简单的 npm 模块来处理这个问题。

gulp-npm-script-sync

gulp-npm-script-sync

Here is the gist of it:

这是它的要点:

  var file = fs.readFileSync(config.path || 'package.json', 'utf-8');
  var pkg = JSON.parse(file);
  var tasks = gulp.tasks;

  pkg.scripts = pkg.scripts || {};

  Object.keys(tasks).forEach(function (t) {
    pkg.scripts[t] = 'gulp '+tasks[t].name;
  });

The full sourcedoes stuff like write the package.json back with the same indention and stuff.

完整的源代码做这样的东西写有相同缩进和东西的package.json回来。

So yeah go ahead: npm install --save-dev gulp-npm-script-sync

所以是的,继续: npm install --save-dev gulp-npm-script-sync

Stick this in your gulpfile:

把它放在你的 gulpfile 中:

var gulp = require('gulp');
var sync = require('gulp-npm-script-sync');

// your gulpfile contents

sync(gulp);

Every time you update your gulpfile with a new task it will update your package.json.

每次你用一个新任务更新你的 gulpfile 时,它​​都会更新你的 package.json。

You can even throw it inside a gulp task:

你甚至可以把它扔到 gulp 任务中:

gulp.task('sync', function () {
  sync(gulp);
}

回答by Roman Boiko

Brian Brennan gave a fantastic suggestion: adding ./node_modules/.binto your $PATHvariable. Then you can run locally installed utilities as long as you're in the top directory of a project.

Brian Brennan 给出了一个绝妙的建议:添加 ./node_modules/.bin到您的$PATH变量中。然后,只要您位于项目的顶级目录中,就可以运行本地安装的实用程序。

Source: Keeping clean.

来源:保持清洁

No need to install a package globally to be able to invoke it easily. Just remember that the above path should be prepended.

无需全局安装软件包即可轻松调用它。请记住,应预先添加上述路径。



Alternatively, since version 2.0.0, npmprovides the ability to pass extra parameters to our run commands. These arguments will be simply appended to your script command.

或者,从 2.0.0 版本开始,npm提供了将额外参数传递给我们的运行命令的能力。这些参数将简单地附加到您的脚本命令中。

In your case, the following script from your package.json:

在您的情况下,您的以下脚本package.json

"scripts": {
  "gulp": "gulp"
},

can be invoked as npm run gulp -- test, or npm run gulp -- minify, etc.

可以作为npm run gulp -- test、 或npm run gulp -- minify等调用。