在 *one* gulp 任务中使用 yargs 会在 *all* 任务中应用它:为什么?怎么修?

Using yargs in *one* gulp task applies it in *all* tasks: Why? How to fix?

我有一个 UI 项目,其自动化工作流程基于 generator-gulp-angular。我添加了 gulp-ng-config,以便根据环境变量执行不同的构建。我使用包 'yargs' 提取环境标志,并使其可用于该任务。但是即使任务应该被封装,我使用 yargs 为它创建需求现在在我整个项目中的所有 gulp 任务上都是活跃的。

这是 ngconfig 的 gulp 任务:

var gulp = require('gulp');
var path = require('path');
var conf = require('./conf');
var gulpNgConfig = require('gulp-ng-config');
var argv = require('yargs')
  .usage('This `build` or `serve` task includes an ngConfig task, whose requirements have not been met via arguments. \n LONG USAGE: <command> --environment <"production" or "sit" or "local">.\n SHORT USAGE <command> -e <"production" or "sit" or "local">')
  .epilogue('For more information, see the iJoin client README.')
  .demand(['e'])
  .alias('e', 'environment')
  .argv;

gulp.task('ngconfig', function() {
  // default config:
  var thisConfig = {
    environment: argv.environment,
    wrap: "(function () { \n 'use strict'; \n return <%= module %> \n })();"
  };

  gulp.src('gulp/server-config.json')
    .pipe(gulpNgConfig('ijoin.apiConfig', thisConfig))
    .pipe(gulp.dest(path.join(conf.paths.src, '/app/prebuild')));

});

它作为 build 的一部分被调用,此处:

gulp.task('build', ['ngconfig', 'html', 'fonts', 'other']);

当我们想要使用我们的环境变量执行构建时,我们执行

gulp build -e local

而且,一切正常!但它正在蔓延到我的其他任务中。例如,当我启动本地 API 模拟服务器时,使用:

gulp stubby

它抱怨我没有包含必要的参数:

This `build` or `serve` task includes an ngConfig task, whose requirements have not 
been met via arguments.
 LONG USAGE: <command> --environment <"production" or "sit" or "local">.
 SHORT USAGE <command> -e <"production" or "sit" or "local">

Options:
  -e, --environment                                                   [required]

For more information, see the iJoin client README.

Missing required arguments: e

但我的意图是那些必要的参数仅在 ngconfig 任务中需要。 (ngconfig 绝对不是 stubby 的依赖项。)那么,为什么会溢出到其他任务中,我该如何解决?

由于 argv 变量被添加到 ngconfig 的范围之外,它也会影响其他任务的执行。

请将它移到 ngconfig 任务中