2

I have a gulpfile task defined which runs node with a javascript file:

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);
  });
})

However, I would like to pass the file name (dest/index.js) as an argument for a task in tasks.json that represents a selected file in VS Code. For example:

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

However, when I try executing the task, I receive the following error:

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

So I'm wondering if it is possible to pass the "${file}" argument to a gulpfile task?

Anthony Sneed
  • 703
  • 6
  • 19

1 Answers1

0

TRY REVERSING ARG AND TASK


As of VSCode 0.9.1 - Reversing Arg and Task Hack

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

Following is an example from a previous stack question.

{
    "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"
            ]
        }
    ]
}

Notice the same two tasks vet but in one instance an arg is passed --verbose which requires the reversal in order to execute properly for some reason. I still use this hack for VSCode. I'm running VSCode v0.9.1, Gulp CLI and Local on v3.9.0, Node v0.12.7 and Ubuntu GNU/Linux 12.04.* LTS.

Working Example with Output

{
"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": []
    }
]
}

Output From CTRL+SHFT+B or Selecting Build Task From Command Palette - "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

Community
  • 1
  • 1
GJSmith3rd
  • 806
  • 5
  • 7
  • The reversal hack does not seem to work with gulp. In that case, "args" can have only one value, which is the name of the corresponding task in the gulpfile. In this case, you also need to set "suppressTaskName": true. – Anthony Sneed Oct 28 '15 at 11:08
  • Interesting. Still works for one of my earlier projects using gulp. I only used one arg though. – GJSmith3rd Oct 28 '15 at 12:31