从 grunt 中更新 NPM 模块
Update NPM modules from within grunt
我正在尝试自动更新新节点模块,但是 npm update 似乎不想 运行 从 g运行t 中正确地更新 package.json 文件与新版本。无论 package.json 文件中指定的版本是什么,我都想这样做。
到目前为止我找到的是节点模块:npm-check-updates (https://www.npmjs.com/package/npm-check-updates)
问题是我无法让它与 npm-shell 或 npm-exec 等其他模块一起工作。
我试过直接使用 npm update -D,但也失败了。
我在问是否可以完成。
这是我使用的:
grunt.registerTask('update', 'Update npm modules', function() {
var exec = require('child_process').exec;
var cb = this.async();
exec('npm update -D', {}, function(err, stdout) {
console.log(stdout);
cb();
});
});
如果我是对的,您是在尝试更新 package.json 文件中的所有 npm 包吗?我会推荐使用这个包。
安装包。
npm install grunt-auto-install --save-dev
将其添加到您的 grunt 任务中。
grunt.loadNpmTasks('grunt-auto-install');
然后将配置添加到您的gruntfile.js
grunt.initConfig({
auto_install: {
local: {},
subdir: {
options: {
cwd: 'subdir',
stdout: true,
stderr: true,
failOnError: true,
npm: '--production'
}
}
},
});
这里是参考:
https://www.npmjs.com/package/grunt-auto-install
在您更新软件包之后
使用 grunt 任务自动更新您的 devDependencies 和依赖项。
安装 npm 模块以更新开发依赖项对象中的包。
npm install --save-dev grunt-dev-update
将其添加到您的 grunt 任务中。
grunt.loadNpmTasks('grunt-dev-update');
将您的配置添加到您的 gruntfile。
devUpdate: {
main: {
options: {
//task options go here
}
}
}
我找到了使用 npm-check-update (ncu) 和 grunt.util.spawn 的解决方案:
// Install NPM Updates
grunt.registerTask('update-npm', 'Update package.json and update npm modules', function() {
grunt.log.writeln('If you get an error here, run "npm install -g npm-check-updates".');
grunt.task.run('npm-write-new');
grunt.task.run('npm-update');
});
// Check for npm module updates
grunt.registerTask('npm-check', 'Check for npm modules updates', function() {
var done = this.async();
grunt.log.writeln('Checking for npm modules updates ...');
grunt.util.spawn({
cmd: 'ncu',
args: '',
opts: {
stdio: 'inherit',
}
}, function () {
grunt.log.writeln('No files were modified.');
done();
});
});
// Write new versions to packages.json
grunt.registerTask('npm-write-new', 'Write new versions to package.json', function() {
var done = this.async();
grunt.log.writeln('Checking for npm modules updates ...');
grunt.util.spawn({
cmd: 'ncu',
args: ['-u'],
opts: {
stdio: 'inherit',
}
}, function () {
grunt.log.writeln('New versions were written to "package.json".');
done();
});
});
// Update npm modules
grunt.registerTask('npm-update', 'Update npm modules', function() {
var done = this.async();
grunt.log.writeln('Installing npm modules updates ...');
grunt.util.spawn({
cmd: 'npm',
args: ['update','--loglevel','warn'],
opts: {
stdio: 'inherit',
}
}, function () {
grunt.log.writeln('NPM modules were updated.');
done();
});
});
我正在尝试自动更新新节点模块,但是 npm update 似乎不想 运行 从 g运行t 中正确地更新 package.json 文件与新版本。无论 package.json 文件中指定的版本是什么,我都想这样做。
到目前为止我找到的是节点模块:npm-check-updates (https://www.npmjs.com/package/npm-check-updates)
问题是我无法让它与 npm-shell 或 npm-exec 等其他模块一起工作。
我试过直接使用 npm update -D,但也失败了。
我在问是否可以完成。
这是我使用的:
grunt.registerTask('update', 'Update npm modules', function() {
var exec = require('child_process').exec;
var cb = this.async();
exec('npm update -D', {}, function(err, stdout) {
console.log(stdout);
cb();
});
});
如果我是对的,您是在尝试更新 package.json 文件中的所有 npm 包吗?我会推荐使用这个包。
安装包。
npm install grunt-auto-install --save-dev
将其添加到您的 grunt 任务中。
grunt.loadNpmTasks('grunt-auto-install');
然后将配置添加到您的gruntfile.js
grunt.initConfig({
auto_install: {
local: {},
subdir: {
options: {
cwd: 'subdir',
stdout: true,
stderr: true,
failOnError: true,
npm: '--production'
}
}
},
});
这里是参考:
https://www.npmjs.com/package/grunt-auto-install
在您更新软件包之后 使用 grunt 任务自动更新您的 devDependencies 和依赖项。 安装 npm 模块以更新开发依赖项对象中的包。
npm install --save-dev grunt-dev-update
将其添加到您的 grunt 任务中。
grunt.loadNpmTasks('grunt-dev-update');
将您的配置添加到您的 gruntfile。
devUpdate: {
main: {
options: {
//task options go here
}
}
}
我找到了使用 npm-check-update (ncu) 和 grunt.util.spawn 的解决方案:
// Install NPM Updates
grunt.registerTask('update-npm', 'Update package.json and update npm modules', function() {
grunt.log.writeln('If you get an error here, run "npm install -g npm-check-updates".');
grunt.task.run('npm-write-new');
grunt.task.run('npm-update');
});
// Check for npm module updates
grunt.registerTask('npm-check', 'Check for npm modules updates', function() {
var done = this.async();
grunt.log.writeln('Checking for npm modules updates ...');
grunt.util.spawn({
cmd: 'ncu',
args: '',
opts: {
stdio: 'inherit',
}
}, function () {
grunt.log.writeln('No files were modified.');
done();
});
});
// Write new versions to packages.json
grunt.registerTask('npm-write-new', 'Write new versions to package.json', function() {
var done = this.async();
grunt.log.writeln('Checking for npm modules updates ...');
grunt.util.spawn({
cmd: 'ncu',
args: ['-u'],
opts: {
stdio: 'inherit',
}
}, function () {
grunt.log.writeln('New versions were written to "package.json".');
done();
});
});
// Update npm modules
grunt.registerTask('npm-update', 'Update npm modules', function() {
var done = this.async();
grunt.log.writeln('Installing npm modules updates ...');
grunt.util.spawn({
cmd: 'npm',
args: ['update','--loglevel','warn'],
opts: {
stdio: 'inherit',
}
}, function () {
grunt.log.writeln('NPM modules were updated.');
done();
});
});