Javascript 从 gulp 任务运行 npm 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36619472/
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
Run a npm script from gulp task
提问by lbrahim
How to run a npm script command from inside a gulp task?
如何从 gulp 任务内部运行 npm 脚本命令?
package.json
包.json
"scripts":
{
"tsc": "tsc -w"
}
gulpfile.js
gulpfile.js
gulp.task('compile:app', function(){
return gulp.src('src/**/*.ts')
.pipe(/*npm run tsc*/)
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});
I want to do this because running npm run tsc
does not give me any error but if I use gulp-typescript
to compile .ts
then I get bunch of errors.
我想这样做是因为运行npm run tsc
不会给我任何错误,但是如果我gulp-typescript
用来编译,.ts
那么我会得到一堆错误。
采纳答案by BrunoLM
You can get the equivalent using gulp-typescript
您可以使用gulp-typescript获得等效项
var gulp = require('gulp');
var ts = require('gulp-typescript');
gulp.task('default', function () {
var tsProject = ts.createProject('tsconfig.json');
var result = tsProject.src().pipe(ts(tsProject));
return result.js.pipe(gulp.dest('release'));
});
gulp.task('watch', ['default'], function() {
gulp.watch('src/*.ts', ['default']);
});
Then on your package.json
然后在你的 package.json
"scripts": {
"gulp": "gulp",
"gulp-watch": "gulp watch"
}
Then run
然后运行
npm run gulp-watch
Alternatively using shell
或者使用外壳
var gulp = require('gulp');
var shell = require('gulp-shell');
gulp.task('default', function () {
return gulp.src('src/**/*.ts')
.pipe(shell('npm run tsc'))
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});
var gulp = require('gulp');
var shell = require('gulp-shell');
gulp.task('default', function () {
return gulp.src('src/**/*.ts')
.pipe(shell('npm run tsc'))
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});
gulp-shell
has been blacklisted you can see why here
gulp-shell
已被列入黑名单你可以在这里看到原因
Another alternative would be setting up webpack
.
另一种选择是设置webpack
.
回答by LazyDeveloper
You can try to implement it using childprecess node package or
您可以尝试使用 childprecess 节点包或
use https://www.npmjs.com/package/gulp-run
使用https://www.npmjs.com/package/gulp-run
var run = require('gulp-run');
gulp.task('compile:app', function(){
return gulp.src(['src/**/*.js','src/**/*.map'])
.pipe(run('npm run tsc'))
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});
回答by Manohar Reddy Poreddy
Wasted about 1 hour on this simple thing, looking for a ~complete answer, so adding another here:
在这个简单的事情上浪费了大约 1 个小时,寻找一个完整的答案,所以在这里添加另一个:
If you question is only on typescript (tsc), see https://stackoverflow.com/a/36633318/984471
Else, see below for a generic answer.
如果您的问题仅在打字稿 (tsc) 上,请参阅https://stackoverflow.com/a/36633318/984471
其他,请参阅下面的通用答案。
The question title is generic, so a generic example is given below first, then the answer.
问题标题是通用的,所以下面先给出一个通用的例子,然后是答案。
Generic example:
通用示例:
Install nodejs, if you haven't, preferably LTS version, from here: https://nodejs.org/
Install below:
npm install --save-dev gulp
npm install --save-dev gulp-run
File
package.json
has below contents (other contents can be there):
安装 nodejs,如果你还没有,最好是 LTS 版本,从这里:https://nodejs.org/
安装如下:
npm install --save-dev gulp
npm install --save-dev gulp-run
文件
package.json
具有以下内容(其他内容可以在那里):
{
"name": "myproject",
"scripts": {
"cmd1": "echo \"yay! cmd1 command is run.\" && exit 1",
}
}
{
"name": "myproject",
"scripts": {
"cmd1": "echo \"yay! cmd1 command is run.\" && exit 1",
}
}
- Create a file
gulpfile.js
with below contents:
- 创建一个
gulpfile.js
包含以下内容的文件:
`
`
var gulp = require('gulp');
var run = require('gulp-run');
gulp.task('mywatchtask1', function () {
// watch for javascript file (*.js) changes, in current directory (./)
gulp.watch('./*.js', function () {
// run an npm command called `test`, when above js file changes
return run('npm run cmd1').exec();
// uncomment below, and comment above, if you have problems
// return run('echo Hello World').exec();
});
});
`
`
Run the task
mywatchtask1
usinggulp
?gulp mywatchtask1
运行任务
mywatchtask1
使用gulp
?gulp mywatchtask1
Now, gulp is its watching for js file changes in the current directory
if any changes happen then the npm command cmd1
is run, it will print yay! cmd1 command is run.
everytime the one of the js file changes.
现在,gulp 正在监视当前目录中的 js 文件更改,
如果发生任何更改,则运行 npm 命令cmd1
,它会在yay! cmd1 command is run.
每次 js 文件更改时打印。
For this question:as another example:
对于这个问题:作为另一个例子:
a) package.json
will have
a)package.json
会有
"tsc": "tsc -w",
instead of the below:
而不是以下:
"cmd1": "echo \"yay! cmd1 command is run.\" && exit 1",
b) and, gulpfile.js
will have:
b) 并且,gulpfile.js
将有:
return run('npm run tsc').exec();
instead of below:
而不是下面:
return run('npm run cmd1').exec();
Hope that helps.
希望有帮助。