Skip to content

Commit a670b32

Browse files
committed
feat(build): add lb-env script to set up env vars
1 parent 62d5f4b commit a670b32

File tree

4 files changed

+155
-1
lines changed

4 files changed

+155
-1
lines changed

packages/build/bin/run-env.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
3+
// Node module: @loopback/build
4+
// This file is licensed under the MIT License.
5+
// License text available at https://opensource.org/licenses/MIT
6+
7+
/*
8+
========
9+
10+
Usage:
11+
node ./bin/run-env [VAR1=<val1> VAR2=<var2> --file <env.json>] command [arguments...]
12+
13+
The script will scan all arguments matching `VAR=val` and set them into
14+
environment variables to be inherited by the command.
15+
16+
Then the provided command is executed with the optional arguments.
17+
18+
Example usage:
19+
20+
node ./bin/run-env DEBUG=loopback:* npm test
21+
22+
========
23+
*/
24+
25+
'use strict';
26+
27+
const debug = require('debug')('loopback:build:env');
28+
const util = require('util');
29+
const fs = require('fs-extra');
30+
31+
function loadEnvFile(f) {
32+
debug('Loading environment variables from %s', f);
33+
return fs.readJsonSync(f);
34+
}
35+
36+
function run(argv, options) {
37+
const utils = require('./utils');
38+
39+
let args = argv.slice(2);
40+
const envVars = {};
41+
let files = [];
42+
let i;
43+
for (i = 0; i < args.length; i++) {
44+
const arg = args[i];
45+
if (arg === '--file') {
46+
const json = loadEnvFile(args[++i]);
47+
Object.assign(envVars, json);
48+
continue;
49+
}
50+
const index = arg.indexOf('=');
51+
if (index === -1) {
52+
break;
53+
} else {
54+
const key = arg.substring(0, index).trim();
55+
const val = arg.substring(index + 1).trim();
56+
envVars[key] = val;
57+
}
58+
}
59+
60+
for (const p in envVars) {
61+
debug('Setting environment variable: %s=%s', p, envVars[p]);
62+
process.env[p] = envVars[p];
63+
}
64+
65+
args = args.slice(i);
66+
let command = args.shift();
67+
68+
if (!command) {
69+
// Command is not present
70+
const msg = util.format(
71+
'Usage: %s command <options-and-args>',
72+
argv.join(' ')
73+
);
74+
process.exitCode = 1;
75+
return msg;
76+
}
77+
78+
if (options === true || (options && options.dryRun)) {
79+
const envArgs = [];
80+
for (const p in envVars) {
81+
envArgs.push(`${p}=${envVars[p]}`);
82+
}
83+
envArgs.push(command);
84+
command = envArgs.join(' ');
85+
}
86+
return utils.runShell(command, args, options);
87+
}
88+
89+
module.exports = run;
90+
if (require.main === module) run(process.argv);

packages/build/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"lb-nyc": "./bin/run-nyc.js",
3636
"lb-dist": "./bin/select-dist.js",
3737
"lb-apidocs": "./bin/generate-apidocs.js",
38-
"lb-clean": "./bin/run-clean.js"
38+
"lb-clean": "./bin/run-clean.js",
39+
"lb-env": "./bin/run-env.js"
3940
},
4041
"scripts": {
4142
"lint": "npm run prettier:check",

packages/build/test/fixtures/env.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"VAR1": 1,
3+
"VAR2": 2
4+
}

packages/build/test/integration/scripts.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,63 @@ describe('build', function() {
294294
);
295295
assert(command.indexOf('rm -rf ./dist') !== -1);
296296
});
297+
298+
it('sets env vars with run-env', () => {
299+
var run = require('../../bin/run-env');
300+
var command = run(
301+
[
302+
'node',
303+
'bin/run-env',
304+
'VAR1=1',
305+
'VAR2=a',
306+
'node',
307+
'-e',
308+
'console.log(process.env.VAR1);',
309+
],
310+
true
311+
);
312+
assert.equal(
313+
command,
314+
'VAR1=1 VAR2=a node "-e" "console.log(process.env.VAR1);"'
315+
);
316+
});
317+
318+
it('sets env vars from files with run-env', () => {
319+
var run = require('../../bin/run-env');
320+
const envFile = path.join(__dirname, '../fixtures/env.json');
321+
var command = run(
322+
[
323+
'node',
324+
'bin/run-env',
325+
'--file',
326+
envFile,
327+
'node',
328+
'-e',
329+
'console.log(process.env.VAR1);',
330+
],
331+
true
332+
);
333+
assert.equal(
334+
command,
335+
'VAR1=1 VAR2=2 node "-e" "console.log(process.env.VAR1);"'
336+
);
337+
});
338+
339+
it('allows no env vars with run-env', () => {
340+
var run = require('../../bin/run-env');
341+
var command = run(
342+
['node', 'bin/run-env', 'node', '-e', 'console.log(process.env.VAR1);'],
343+
true
344+
);
345+
assert.equal(command, 'node "-e" "console.log(process.env.VAR1);"');
346+
});
347+
348+
it('reports error if no command is provided with run-env', () => {
349+
var run = require('../../bin/run-env');
350+
var command = run(['node', 'bin/run-env', 'VAR1=1'], true);
351+
assert.equal(
352+
command,
353+
'Usage: node bin/run-env VAR1=1 command <options-and-args>'
354+
);
355+
});
297356
});

0 commit comments

Comments
 (0)