Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Fix #59 - implement 'envFile'
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Jan 20, 2017
1 parent 2cc5b38 commit b3e2e8f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@
"type": "array",
"description": "%node.skipFiles.description%",
"default": []
},
"envFile": {
"type": "string",
"description": "%node.launch.envFile.description%",
"default": "${workspaceRoot}/.env"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"node.launch.runtimeExecutable.description": "Runtime to use. Either an absolute path or the name of a runtime available on the PATH. If ommitted 'node' is assumed.",
"node.launch.runtimeArgs.description": "Optional arguments passed to the runtime executable.",
"node.launch.env.description": "Environment variables passed to the program.",
"node.launch.envFile.description": "Absolute path to a file containing environment variable definitions.",

"node.launch.config.name": "Launch",

Expand Down
10 changes: 9 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ export function cannotLaunchBecauseOutFiles(programPath: string): DebugProtocol.
format: localize('VSND2003', "Cannot launch program '{0}'; setting the '{1}' attribute might help.", '{path}', 'outDir or outFiles'),
variables: { path: programPath }
};
}
}

export function cannotLoadEnvVarsFromFile(error: string): DebugProtocol.Message {
return {
id: 2029,
format: localize('VSND2029', "Can't load environment variables from file ({0}).", '{_error}'),
variables: { _error: error }
};
}
30 changes: 28 additions & 2 deletions src/nodeDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,20 @@ export class NodeDebugAdapter extends ChromeDebugAdapter {

launchArgs = launchArgs.concat(runtimeArgs, program ? [program] : [], programArgs);

const envArgs = this.collectEnvFileArgs(args) || args.env;
let launchP: Promise<void>;
if (args.console === 'integratedTerminal' || args.console === 'externalTerminal') {
const termArgs: DebugProtocol.RunInTerminalRequestArguments = {
kind: args.console === 'integratedTerminal' ? 'integrated' : 'external',
title: localize('node.console.title', "Node Debug Console"),
cwd,
args: launchArgs,
env: args.env
env: envArgs
};
launchP = this.launchInTerminal(termArgs);
} else if (!args.console || args.console === 'internalConsole') {
// merge environment variables into a copy of the process.env
const env = Object.assign({}, process.env, args.env);
const env = Object.assign({}, process.env, envArgs);
launchP = this.launchInInternalConsole(runtimeExecutable, launchArgs.slice(1), { cwd, env });
} else {
return Promise.reject(errors.unknownConsoleType(args.console));
Expand Down Expand Up @@ -255,6 +256,31 @@ export class NodeDebugAdapter extends ChromeDebugAdapter {
});
}

private collectEnvFileArgs(args: ILaunchRequestArguments): any {
// read env from disk and merge into envVars
if (args.envFile) {
try {
const env = {};
const buffer = fs.readFileSync(args.envFile, 'utf8');
buffer.split('\n').forEach(line => {
const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (r !== null) {
let value = r[2] || '';
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
value = value.replace(/\\n/gm, '\n');
}

env[r[1]] = value.replace(/(^['"]|['"]$)/g, '');
}
});

return utils.extendObject(env, args.env); // launch config env vars overwrite .env vars
} catch (e) {
throw errors.cannotLoadEnvVarsFromFile(e.message);
}
}
}

/**
* Override so that -core's call on attach will be ignored, and we can wait until the first break when ready to set BPs.
*/
Expand Down
1 change: 1 addition & 0 deletions src/nodeDebugInterfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ILaunchRequestArguments extends Core.ILaunchRequestArgs, ICommo
runtimeArgs?: string[];
/** Optional environment variables to pass to the debuggee. The string valued properties of the 'environmentVariables' are used as key/value pairs. */
env?: { [key: string]: string; };
envFile?: string;
/** Where to launch the debug target. */
console?: ConsoleType;
/** Manually selected debugging port */
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,13 @@ export function isOnPath(program: string): boolean {
export function trimLastNewline(msg: string): string {
return msg.replace(/(\n|\r\n)$/, '');
}

export function extendObject<T>(toObject: T, fromObject: T): T {
for (let key in fromObject) {
if (fromObject.hasOwnProperty(key)) {
toObject[key] = fromObject[key];
}
}

return toObject;
}

0 comments on commit b3e2e8f

Please sign in to comment.