Created
February 24, 2016 16:11
-
-
Save pmuellr/eda1e01971be3aeed139 to your computer and use it in GitHub Desktop.
Handy little script to add a launch for a particular node script to the VS Code launch configuration, so you don't have to do it in Code itself. Should be run from the project's base directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict' | |
const fs = require('fs') | |
const path = require('path') | |
const LaunchFile = '.vscode/launch.json' | |
const LaunchDir = path.dirname(LaunchFile) | |
const script = process.argv[2] | |
if (!script) { | |
exit(1, 'expecting name of script') | |
} | |
if (!exists(script)) { | |
exit(1, `script does not exist: ${script}`) | |
} | |
const launchJSON = readLaunchJSON() | |
const newConfig = getNewConfig(script) | |
launchJSON.configurations.push(newConfig) | |
writeLaunchConfig(launchJSON) | |
function getNewConfig () { | |
return { | |
name: script, | |
type: "node", | |
request: "launch", | |
program: script, | |
stopOnEntry: false, | |
args: [], | |
cwd: "${workspaceRoot}", | |
runtimeExecutable: null, | |
runtimeArgs: [ "--nolazy" ], | |
env: { NODE_ENV: "development" }, | |
externalConsole: false, | |
sourceMaps: false, | |
outDir: null | |
} | |
} | |
function readLaunchJSON () { | |
const newLaunchJSON = { | |
version: "0.2.0", | |
configurations: [] | |
} | |
let jsonString | |
try { | |
jsonString = fs.readFileSync(LaunchFile) | |
} catch (err) { | |
jsonString = JSON.stringify(newLaunchJSON) | |
} | |
return JSON.parse(jsonString) | |
} | |
function writeLaunchConfig(json) { | |
const stats_vscode = exists(LaunchDir) | |
if (!stats_vscode) { | |
fs.mkdirSync(LaunchDir) | |
} | |
fs.writeFileSync(LaunchFile, JSON.stringify(json, null, 4)) | |
} | |
function exists(fileName) { | |
try { | |
return fs.statSync(fileName) | |
} catch (err) { | |
return false | |
} | |
} | |
function exit (code, message) { | |
console.log(message) | |
process.exit(code) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment