【问题标题】:Pass Arguments to Gulpfile Task from Tasks.json?从 Tasks.json 将参数传递给 Gulpfile 任务?
【发布时间】:2015-10-26 14:47:22
【问题描述】:

我定义了一个 gulpfile 任务,它使用 javascript 文件运行节点:

var exec = require('child_process').exec;

gulp.task('node-run', function (cb) {
  exec('node dest/index.js', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    cb(err);
  });
})

但是,我想将文件名 (dest/index.js) 作为参数传递给 tasks.json 中的任务,该任务代表 VS Code 中的选定文件。例如:

{
	"version": "0.1.0",
	"command": "gulp",
	"isShellCommand": true,
	"args": [
		"--no-color"
	],
	"tasks": [
		{
			"taskName": "node-run",
			"showOutput": "always",
			"args": ["${file}"]
		}
	]
}

但是,当我尝试执行任务时,我收到以下错误:

Using gulpfile gulpfile.js
Task '/Users/Tony/Source/HelloVsCode/.vscode/tasks.json' is not in your gulpfile
Please check the documentation for proper gulpfile formatting

所以我想知道是否可以将“${file}”参数传递给 gulpfile 任务?

【问题讨论】:

    标签: visual-studio-code


    【解决方案1】:

    尝试反转 ARG 和任务


    从 VSCode 0.9.1 开始 - 逆向 Arg 和任务破解

    {
        "version": "0.1.0",
        "command": "gulp",
        "isShellCommand": true,
        "args": [
            "--no-color"
            ],
        "tasks": [
            {
                "taskName": "${file}",
                "showOutput": "always",
                "args": ["node-run"]
        }
      ]
    }
    

    以下是上一个堆栈问题的示例。

    {
        "version": "0.1.0",
        "command": "gulp",
        "isShellCommand": true,
        "args": [
            "--no-color"
        ],
        "tasks": [
            {
                "taskName": "--verbose",
                "isBuildCommand": true,
                "showOutput": "always",
                "args": [
                    "vet"
                ],
                "problemMatcher": [
                    "$jshint",
                    "$jshint-stylish"
                ]
            },
            {
                "taskName": "vet",
                "isTestCommand": true,
                "showOutput": "always",
                "args": [],
                "problemMatcher": [
                    "$jshint",
                    "$jshint-stylish"
                ]
            }
        ]
    }
    

    注意相同的两个任务vet,但在一个实例中传递了一个 arg --verbose,这需要反转以便由于某种原因正确执行。我仍然将这个 hack 用于 VSCode。我在 v3.9.0、Node v0.12.7 和 Ubuntu GNU/Linux 12.04.* LTS 上运行 VSCode v0.9.1、Gulp CLI 和 Local。

    输出工作示例

    {
    "version": "0.1.0",
    "command": "gulp",
    "isShellCommand": true,
    "args": [
        "--no-color"
    ],
    "tasks": [
        {
            "taskName": "--verbose",
            "isBuildCommand": true,
            "isTestCommand": false,
            "showOutput": "always",
            "echoCommand": true,
            "args": [
                "serve-dev"
            ]
        },
        {
            "taskName": "serve-dev",
            "isBuildCommand": false,
            "isTestCommand": true,
            "showOutput": "always",
            "echoCommand": true,
            "args": []
        }
    ]
    }
    

    从 CTRL+SHFT+B 输出或从命令面板选择构建任务 - “running command$ gulp --no-color serve-dev --verbose”

    running command$ gulp --no-color serve-dev --verbose
    [07:29:35] Using gulpfile ~/Workspaces/FCC/Ziplines/Personal-   Portfolio-Webpage/gulpfile.js
    [07:29:35] Starting 'vet'...
    [07:29:36] ***Analyzing js with jshint and jscs..
    [gulp] app.js
    [gulp] script.js
    [07:29:40] Finished 'vet' after 4.61 s
    [07:29:40] Starting 'clean-dev'...
    [07:29:40] ***Cleaning ./styles.css
    [07:29:40] Finished 'clean-dev' after 28 ms
    [07:29:40] Starting 'css-prep'...
    [07:29:40] ***Compiling less to css...
    [07:29:44] Finished 'css-prep' after 3.86 s
    [07:29:44] Starting 'serve-dev'...
    [07:29:44] ***Start pre processes and node server...
    [07:29:44] Finished 'serve-dev' after 611 ms
    [07:29:44] [nodemon] v1.4.1
    [07:29:44] [nodemon] to restart at any time, enter `rs`
    [07:29:44] [nodemon] watching: *.*
    [07:29:44] [nodemon] starting `node ./app.js`
    [07:29:44] *** nodemon started
    [07:29:44] ***Starting browserSync on port 7203
    [BS] [debug] -> Starting Step: Finding an empty port
    [BS] [debug] Found a free port: 3099
    [BS] [debug] Setting Option: port - 3099
    [BS] [debug] +  Step Complete: Finding an empty port
    [BS] [debug] -> Starting Step: Getting an extra port for Proxy
    [BS] [debug] +  Step Complete: Getting an extra port for Proxy
    [BS] [debug] -> Starting Step: Checking online status
    [BS] [debug] Resolved www.google.com, setting online: true
    [BS] [debug] Setting Option: online - true
    [BS] [debug] +  Step Complete: Checking online status
    [BS] [debug] -> Starting Step: Resolve user plugins from options
    [BS] [debug] +  Step Complete: Resolve user plugins from options
    [BS] [debug] -> Starting Step: Set Urls and other options that rely on port/online status
    [BS] [debug] Setting multiple Options
    [BS] [debug] +  Step Complete: Set Urls and other options that rely on port/online status
    [BS] [debug] -> Starting Step: Setting Internal Events
    [BS] [debug] +  Step Complete: Setting Internal Events
    [BS] [debug] -> Starting Step: Setting file watchers
    [BS] [debug] +  Step Complete: Setting file watchers
    [BS] [debug] -> Starting Step: Merging middlewares from core + plugins
    [BS] [debug] Setting Option: middleware - List []
    [BS] [debug] +  Step Complete: Merging middlewares from core + plugins
    [BS] [debug] -> Starting Step: Setting the rewrite rules given in initial options
    [BS] [debug] +  Step Complete: Setting the rewrite rules given in initial options
    [BS] [debug] -> Starting Step: Setting the rewrite rules middleware for snippet
    [BS] [debug] +  Step Complete: Setting the rewrite rules middleware for snippet
    [BS] [debug] -> Starting Step: Starting the Server
    [BS] [debug] Proxy running, proxing: http://10.0.0.57:7203
    [BS] [debug] Running mode: PROXY
    [BS] [debug] +  Step Complete: Starting the Server
    [BS] [debug] -> Starting Step: Adding serve static middlewares
    [BS] [debug] +  Step Complete: Adding serve static middlewares
    [BS] [debug] -> Starting Step: Starting the HTTPS Tunnel
    [BS] [debug] +  Step Complete: Starting the HTTPS Tunnel
    [BS] [debug] -> Starting Step: Starting the web-socket server
    [BS] [debug] Setting Option: clientEvents - List [ "scroll", "scroll:element", "input:text", "input:toggles", "form:submit", "form:reset", "click" ]
    [BS] [debug] +  Step Complete: Starting the web-socket server
    [BS] [debug] -> Starting Step: Adding the HTTP protocol
    [BS] [debug] +  Step Complete: Adding the HTTP protocol
    [BS] [debug] -> Starting Step: Starting the UI
    [BS] [debug] Setting Option: session - 1446035388137
    [BS] [UI] Starting Step: Setting default plugins
    [BS] [UI] Step Complete: Setting default plugins
    [BS] [UI] Starting Step: Finding a free port
    [*** browserSync] [warn] Proxy address not reachable - is your server running?
    [BS] [UI] Step Complete: Finding a free port
    [BS] [UI] Starting Step: Setting options also relevant to UI from BS
    [BS] [UI] Step Complete: Setting options also relevant to UI from BS
    [BS] [UI] Starting Step: Setting available URLS for UI
    [BS] [debug] Getting option via path: urls
    [BS] [UI] Step Complete: Setting available URLS for UI
    [BS] [UI] Starting Step: Starting the Control Panel Server
    [BS] [UI] Using port 3001
    [BS] [UI] Step Complete: Starting the Control Panel Server
    [BS] [UI] Starting Step: Add element events
    [BS] [UI] Step Complete: Add element events
    [BS] [UI] Starting Step: Registering default plugins
    [BS] [debug] Getting option via path: urls,external
    [BS] [UI] Step Complete: Registering default plugins
    [BS] [UI] Starting Step: Add options setting event
    [BS] [UI] Step Complete: Add options setting event
    [BS] [debug] +  Step Complete: Starting the UI
    [BS] [debug] -> Starting Step: Merge UI settings
    [BS] [debug] Setting Option: urls - Map { "local": "http://localhost:3099", "external": "http://10.0.0.57:3099", "ui": "http://localhost:3001", "ui-external": "http://10.0.0.57:3001" }
    [BS] [debug] +  Step Complete: Merge UI settings
    [BS] [debug] -> Starting Step: Init user plugins
    [BS] [debug] Setting Option: userPlugins - 
    [BS] [debug] +  Step Complete: Init user plugins
    [*** browserSync] Proxying: http://10.0.0.57:7203
    [*** browserSync] Access URLs:
    ----------------------------------
    Local: http://localhost:3099
    External: http://10.0.0.57:3099
    ----------------------------------
          UI: http://localhost:3001
    UI External: http://10.0.0.57:3001
    ----------------------------------
    [*** browserSync] Watching files...
    About to crank up node
    PORT=7203
    NODE_ENV=dev
    ** DEV **
    Express server listening on port 7203
    env = dev
    __dirname = /home/gjsmith3rd/Workspaces/FCC/Ziplines/Personal-Portfolio-Webpage
    process.cwd = /home/gjsmith3rd/Workspaces/FCC/Ziplines/Personal-Portfolio-Webpage
    

    Stack Reference

    【讨论】:

    • reversal hack 似乎不适用于 gulp。在这种情况下,“args”只能有一个值,即 gulpfile 中相应任务的名称。在这种情况下,您还需要设置“suppressTaskName”:true。
    • 有趣。仍然适用于我早期使用 gulp 的项目之一。不过我只使用了一个 arg。
    猜你喜欢
    • 2015-04-16
    • 1970-01-01
    • 2019-03-10
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    相关资源
    最近更新 更多