首页 > 解决方案 > 在 TFS 上安装 Visual Studio npm 获取最新

问题描述

当 TFS“获取最新”引入添加开发依赖项的 package.json 更改时,Visual Studio 2017 不会自动运行“npm install”。这会破坏依赖于新包的“监视”模式下的所有 gulp 任务。

我能看到导致 npm install 的唯一方法是手动触摸 package.json 或重新启动 VS。

有没有办法在“获取最新”时触发“npm install”?

标签: visual-studiotfsnpm-install

解决方案


这是我确定的解决方案的线框图。tl;dr - 在 Gulp 中实现我自己的 package.json 观察器来停止 Webpack、运行npm install和重新启动 Webpack。

const gulp = require('gulp');
const install = require('gulp-install');
const watch = require('glob-watcher');
const webpack = require('webpack');

gulp.task('default', ['webpack']);

gulp.task('webpack', function () {
    // Start the initial Webpack build.
    let webpackCompiler = webpack({
        // ...
        watch: true
        // ...
    });

    // Set up a watcher to kill Webpack, run 'npm install', and restart Webpack when package.json changes.
    let packageWatcher = watch('./package.json', { events: ['change'] }, () => {
        packageWatcher.close();
        console.log('Stopped package.json watcher.');

        webpackWatcher.close(() => {
            console.log('Stopped Webpack watcher.');

            gulp.start('npm-install-then-webpack');
        });
    });
    console.log('Started package.json watcher.');
});

gulp.task('npm-install', function (callback) {
    gulp.src('./package.json')
        .pipe(install(callback));
});

gulp.task('npm-install-then-webpack', ['npm-install'], function () {
    gulp.start('webpack');
});

推荐阅读