Skip to content

Commit 8f82f00

Browse files
authored
feat(env): better developer support through env variables
fixes #14
1 parent e81ea70 commit 8f82f00

26 files changed

+479
-206
lines changed

package-lock.json

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
},
3434
"dependencies": {
3535
"@adobe/fastly-native-promises": "^1.3.2",
36-
"@adobe/helix-shared": "0.10.0",
3736
"@adobe/helix-pipeline": "1.2.2",
37+
"@adobe/helix-shared": "0.10.0",
3838
"@adobe/helix-simulator": "2.11.0",
3939
"@adobe/parcel-plugin-htl": "2.1.0",
4040
"@parcel/logger": "1.11.0",
4141
"@snyk/nodejs-runtime-agent": "1.42.1",
4242
"ajv": "^6.10.0",
4343
"archiver": "3.0.0",
44+
"camelcase": "^5.3.1",
4445
"chalk": "2.4.2",
4546
"chokidar": "^2.1.5",
4647
"decompress": "4.2.0",

src/auth.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ module.exports = function auth() {
2626
desc: 'Authenticate against 3rd party systems for development and deployment',
2727
builder: (yargs) => {
2828
yargs
29+
.env('NO_HLX_ENV_SUPPORT_FOR_NOW')
2930
.option('github', {
3031
boolean: true,
3132
default: true,
3233
describe: 'Run authentication wizard for GitHub.',
3334
})
3435
.group(['github'/* , 'fastly', 'wsk' */], 'Services')
36+
.strict()
3537
.help();
3638
},
3739
handler: async (argv) => {

src/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/* eslint global-require: off */
1616

17-
const { defaultArgs } = require('./defaults.js');
17+
const yargsBuild = require('./yargs-build.js');
1818
const { makeLogger } = require('./log-common.js');
1919

2020
module.exports = function build() {
@@ -26,7 +26,7 @@ module.exports = function build() {
2626
command: 'build [files..]',
2727
desc: 'Compile the template functions and build package',
2828
builder: (yargs) => {
29-
defaultArgs(yargs);
29+
yargsBuild(yargs);
3030
yargs.help();
3131
},
3232
handler: async (argv) => {

src/clean.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ module.exports = function demo() {
2929
alias: 'o',
3030
default: '.hlx/build',
3131
describe: 'Target directory for compiled JS',
32-
})
33-
.strict();
32+
});
3433
},
3534
handler: async (argv) => {
3635
if (!executor) {

src/cli.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'use strict';
1616

1717
const yargs = require('yargs');
18+
const camelcase = require('camelcase');
1819

1920
const MIN_MSG = 'You need at least one command.';
2021

@@ -26,6 +27,37 @@ if (process.env.NODE_OPTIONS) {
2627
.join(' ');
2728
}
2829

30+
function envAwareStrict(args, aliases) {
31+
const specialKeys = ['$0', '--', '_'];
32+
const illegalEnv = ['saveConfig', 'add', 'default'];
33+
34+
const hlxEnv = {};
35+
Object
36+
.keys(process.env)
37+
.filter(key => key.startsWith('HLX_'))
38+
.forEach((key) => {
39+
hlxEnv[camelcase(key.substring(4))] = key;
40+
});
41+
42+
illegalEnv.forEach((key) => {
43+
if (key in hlxEnv) {
44+
throw new Error(`${hlxEnv[key]} is not allowed in environment.`);
45+
}
46+
});
47+
48+
const unknown = [];
49+
Object.keys(args).forEach((key) => {
50+
if (specialKeys.indexOf(key) === -1 && !(key in hlxEnv) && !(key in aliases)) {
51+
unknown.push(key);
52+
}
53+
});
54+
55+
if (unknown.length > 0) {
56+
return unknown.length === 1 ? `Unknown argument: ${unknown[0]}` : `Unknown arguments: ${unknown.join(', ')}`;
57+
}
58+
return true;
59+
}
60+
2961
/**
3062
* Adds the default logging options.
3163
* @param argv Yargs
@@ -61,10 +93,10 @@ class CLI {
6193
auth: require('./auth.js')(),
6294
};
6395
this._failFn = (message, err, argv) => {
64-
const msg = err ? err.message : message;
96+
const msg = err && err.message ? err.message : message;
6597
console.error(msg);
6698
if (msg === MIN_MSG || /.*Unknown argument.*/.test(msg) || /.*Not enough non-option arguments:.*/.test(msg)) {
67-
console.error('\nUsage: %s', argv.help());
99+
console.error('\n%s', argv.help());
68100
}
69101
process.exit(1);
70102
};
@@ -84,16 +116,28 @@ class CLI {
84116
const argv = yargs();
85117
Object.values(this._commands).forEach(cmd => argv.command(cmd));
86118

87-
return logArgs(argv)
119+
const ret = logArgs(argv)
88120
.scriptName('hlx')
121+
.usage('Usage: $0 <command> [options]')
122+
.parserConfiguration({ 'camel-case-expansion': false })
123+
.env('HLX')
124+
.check(envAwareStrict)
125+
.showHelpOnFail(true)
89126
.fail(this._failFn)
90127
.exitProcess(args.indexOf('--get-yargs-completions') > -1)
91-
.strict()
92128
.demandCommand(1, MIN_MSG)
93129
.epilogue('for more information, find our manual at https://github.com/adobe/helix-cli')
94130
.help()
95131
.completion()
96132
.parse(args);
133+
134+
// hack to check if command is valid in non-strict mode
135+
const cmd = ret._[0];
136+
if (cmd && !(cmd in this._commands)) {
137+
console.error('Unknown command: %s\n', cmd);
138+
argv.showHelp();
139+
process.exit(1);
140+
}
97141
}
98142
}
99143

src/defaults.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/demo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = function demo() {
4040
describe: 'Parent directory of new project',
4141
default: '.',
4242
})
43+
.env('NO_HLX_ENV_SUPPORT_FOR_DEMO')
4344
.strict();
4445
},
4546
handler: async (argv) => {

src/deploy.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
'use strict';
1414

15-
const deployCommon = require('./deploy-common');
15+
const yargsOpenwhisk = require('./yargs-openwhisk.js');
16+
const yargsFastly = require('./yargs-fastly.js');
1617
const { makeLogger } = require('./log-common.js');
1718

1819
module.exports = function deploy() {
@@ -25,34 +26,35 @@ module.exports = function deploy() {
2526
command: 'deploy',
2627
desc: 'Deploy packaged functions to Adobe I/O runtime',
2728
builder: (yargs) => {
28-
deployCommon(yargs);
29+
yargsOpenwhisk(yargs);
30+
yargsFastly(yargs);
2931
yargs
3032
.option('auto', {
3133
describe: 'Enable auto-deployment',
3234
type: 'boolean',
3335
default: false,
3436
demandOption: true,
3537
})
38+
.option('dry-run', {
39+
alias: 'dryRun',
40+
describe: 'List the actions that would be created, but do not actually deploy',
41+
type: 'boolean',
42+
default: false,
43+
})
3644
.option('loggly-host', {
45+
alias: 'logglyHost',
3746
describe: 'API Host for Log Appender',
3847
type: 'string',
3948
default: 'trieloff.loggly.com',
4049
})
4150
.option('loggly-auth', {
51+
alias: 'logglyAuth',
4252
describe: 'API Key for Log Appender ($HLX_LOGGLY_AUTH)',
4353
type: 'string',
4454
default: '',
4555
})
46-
.option('fastly-namespace', {
47-
describe: 'CDN Namespace (e.g. Fastly Service ID)',
48-
type: 'string',
49-
})
50-
.option('fastly-auth', {
51-
describe: 'API Key for Fastly API ($HLX_FASTLY_AUTH)',
52-
type: 'string',
53-
default: '',
54-
})
5556
.option('circleci-auth', {
57+
alias: 'circleciAuth',
5658
describe: 'API Key for CircleCI API ($HLX_CIRCLECI_AUTH)',
5759
type: 'string',
5860
default: '',

src/perf.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212

1313
'use strict';
1414

15-
/* eslint no-console: off */
16-
// TODO: remove the following line
17-
/* eslint no-unused-vars: off */
18-
1915
const { makeLogger } = require('./log-common.js');
2016

2117
module.exports = function perf() {
@@ -29,14 +25,14 @@ module.exports = function perf() {
2925
desc: 'Test performance',
3026
builder: (yargs) => {
3127
yargs
32-
.env('HLX')
33-
.strict(false)
3428
.option('fastly-namespace', {
29+
alias: 'fastlyNamespace',
3530
describe: 'CDN Namespace (e.g. Fastly Service ID)',
3631
type: 'string',
3732

3833
})
3934
.option('fastly-auth', {
35+
alias: 'fastlyAuth',
4036
describe: 'API Key for Fastly API ($HLX_FASTLY_AUTH)',
4137
type: 'string',
4238
})

0 commit comments

Comments
 (0)