3

我想在调试我的生成器或脱机工作时添加一个选项,该选项将从缓存中下载 npm 和 bower 内容(分别使用--cache-min 999999--offline)。

目前,这是我的代码(安装依赖项和调用grunt bower):

CallumGenerator.prototype.installDeps = function () {
    var cb = this.async();

    this.installDependencies({
        skipInstall: this.options['skip-install'],
        callback: function () {
            this.spawnCommand('grunt', ['bower'])
                .on('close', function () {
                    cb();
                });
        }.bind(this)
    });
};

看起来我很可能必须手动调用.npmInstall()才能.bowerInstall()指定选项(我认为?),但我不知道如何指定任何选项。为了澄清,这就是我在控制台中的做法:

npm install --cache-min 999999 --save-dev grunt-contrib-less
bower install --offline --save jquery#1.10.2
4

2 回答 2

2

您不能直接从这里指定选项#installDependencieshttps ://github.com/yeoman/generator/blob/master/lib/actions/install.js#L44-L69

#npmInstall您可以同时为https://github.com/yeoman/generator/blob/master/lib/actions/install.js#L121-L143指定它们bowerInstall

你传递的options是对象哈希的形式,将被dargs节点模块解析,所以你应该遵循模块约定来声明选项

于 2013-12-21T22:29:10.663 回答
0

我使用的代码,任何人都可以使用(不过,您可能想摆脱最终的回调):

CallumGenerator.prototype.installDeps = function () {
    var cb = this.async();

    this.npmInstall(null, {
        skipInstall: this.options['skip-install'],
        cacheMin: this.cachedDeps ? 999999 : 0
    }, function () {
        this.bowerInstall(null, {
            skipInstall: this.options['skip-install'],
            offline: this.cachedDeps
        }, function () {
            this.spawnCommand('grunt', ['bower'])
                .on('close', function () {
                    cb();
                });
        }.bind(this));
    }.bind(this));
};

它工作正常。this.cachedDeps将定义是否使用缓存。

于 2013-12-22T21:21:47.077 回答