gulp command to take parameters(获取参数的 gulp 命令)
问题描述
我的 package.json 有这样的脚本
my package.json has scripts like this
  {
   "scripts": {
         "pretest": "npm run tsc",
          "test": "gulp e2e",
         }
    }
我们使用 typescript 和 webdriverIO 来实现自动化.我想使用 gulp 以便我可以将参数传递给我的测试框架.示例:
we use typescript and webdriverIO for automation. I want to use gulp so that i can pass parameters to my test framework. Example:
       npm test --suite HomePageTests
那么与主页相关的规范必须运行.
then the specs related to Home page must run.
我有这样的 gulp 文件
I have the gulp file like this
      // gulpfile.js
      const gulp = require('gulp');
       const Launcher = require('webdriverio/build/lib/launcher');
       const wdio = new Launcher(path.join(__dirname, 
                                      'src/config/conf.ts'));
        // fetch command line arguments
        const arg = (argList => {
           let arg = {}, a, opt, thisOpt, curOpt;
           for (a = 0; a < argList.length; a++) {
                thisOpt = argList[a].trim();
                opt = thisOpt.replace(/^-+/, '');
                 if (opt === thisOpt) {
                       // argument value
                       if (curOpt) arg[curOpt] = opt;
                                 curOpt = null;
                  }else {
                    // argument name
                    curOpt = opt;
                    arg[curOpt] = true;
                  }
                }
               console.log("arg", arg)
               return arg;
               })(process.argv);
              gulp.task('e2e', () => {
                  return wdio.run(code => {
                     process.exit(code);
                  }, error => {
                  console.error('Launcher failed to start the test',error.stacktrace);
                 process.exit(1);
               });
            });
所以当我像直接调用 gulp 时
So when I call gulp directly like
          gulp e2e --suite HomePageTests
它被打印为
           suite: HomePageTests
但是如果我使用
            npm test --suite HomePageTests
它在打印 gulp e2e HomePageTests
问题
- 如何从 npm 传递这些值以使 gulp 理解
 如果我传递给另一个值,比如 gulp e2e --server staging,并且想在我的规范文件中使用变量staging",比如
- How do I pass these values from npm to make gulp understand
 If I am pass to another value like gulp e2e --server staging and would like to use the variable "staging" in my spec file like
如果服务器=== 暂存{//做这个} 别的 {//去做}
if server=== staging{
         // do this
  } else {
        // do that
  }
我应该如何将它们从 gulp 文件传递到我的规范文件?
How should I pass them from gulp file to my spec file?
谢谢!!
推荐答案
你可以使用 yargs 依赖
var argv = require('yargs').argv;
gulp.task('test', function(){
   console.log(argv.arg);
});
那么如果你在一个 gulp 上运行一个命令,像这样传递 arg
then if you run a command on a gulp passing the arg like this
gulp test --arg HomePageTests
它将在控制台输出HomePageTests
这篇关于获取参数的 gulp 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取参数的 gulp 命令
				
        
 
            
        基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
 - jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
 - Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
 - 如何使用 CSS 显示和隐藏 div? 2022-01-01
 - Node.js 有没有好的索引/搜索引擎? 2022-01-01
 - 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
 - 什么是不使用 jQuery 的经验技术原因? 2022-01-01
 - WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
 - 每次设置弹出窗口的焦点 2022-01-01
 - 如何在特定日期之前获取消息? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				