Skip to content

Commit

Permalink
feat(env): better developer support through env variables
Browse files Browse the repository at this point in the history
fixes #14
  • Loading branch information
tripodsan authored Apr 8, 2019
1 parent e81ea70 commit 8f82f00
Show file tree
Hide file tree
Showing 26 changed files with 479 additions and 206 deletions.
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
},
"dependencies": {
"@adobe/fastly-native-promises": "^1.3.2",
"@adobe/helix-shared": "0.10.0",
"@adobe/helix-pipeline": "1.2.2",
"@adobe/helix-shared": "0.10.0",
"@adobe/helix-simulator": "2.11.0",
"@adobe/parcel-plugin-htl": "2.1.0",
"@parcel/logger": "1.11.0",
"@snyk/nodejs-runtime-agent": "1.42.1",
"ajv": "^6.10.0",
"archiver": "3.0.0",
"camelcase": "^5.3.1",
"chalk": "2.4.2",
"chokidar": "^2.1.5",
"decompress": "4.2.0",
Expand Down
2 changes: 2 additions & 0 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ module.exports = function auth() {
desc: 'Authenticate against 3rd party systems for development and deployment',
builder: (yargs) => {
yargs
.env('NO_HLX_ENV_SUPPORT_FOR_NOW')
.option('github', {
boolean: true,
default: true,
describe: 'Run authentication wizard for GitHub.',
})
.group(['github'/* , 'fastly', 'wsk' */], 'Services')
.strict()
.help();
},
handler: async (argv) => {
Expand Down
4 changes: 2 additions & 2 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/* eslint global-require: off */

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

module.exports = function build() {
Expand All @@ -26,7 +26,7 @@ module.exports = function build() {
command: 'build [files..]',
desc: 'Compile the template functions and build package',
builder: (yargs) => {
defaultArgs(yargs);
yargsBuild(yargs);
yargs.help();
},
handler: async (argv) => {
Expand Down
3 changes: 1 addition & 2 deletions src/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ module.exports = function demo() {
alias: 'o',
default: '.hlx/build',
describe: 'Target directory for compiled JS',
})
.strict();
});
},
handler: async (argv) => {
if (!executor) {
Expand Down
52 changes: 48 additions & 4 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const yargs = require('yargs');
const camelcase = require('camelcase');

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

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

function envAwareStrict(args, aliases) {
const specialKeys = ['$0', '--', '_'];
const illegalEnv = ['saveConfig', 'add', 'default'];

const hlxEnv = {};
Object
.keys(process.env)
.filter(key => key.startsWith('HLX_'))
.forEach((key) => {
hlxEnv[camelcase(key.substring(4))] = key;
});

illegalEnv.forEach((key) => {
if (key in hlxEnv) {
throw new Error(`${hlxEnv[key]} is not allowed in environment.`);
}
});

const unknown = [];
Object.keys(args).forEach((key) => {
if (specialKeys.indexOf(key) === -1 && !(key in hlxEnv) && !(key in aliases)) {
unknown.push(key);
}
});

if (unknown.length > 0) {
return unknown.length === 1 ? `Unknown argument: ${unknown[0]}` : `Unknown arguments: ${unknown.join(', ')}`;
}
return true;
}

/**
* Adds the default logging options.
* @param argv Yargs
Expand Down Expand Up @@ -61,10 +93,10 @@ class CLI {
auth: require('./auth.js')(),
};
this._failFn = (message, err, argv) => {
const msg = err ? err.message : message;
const msg = err && err.message ? err.message : message;
console.error(msg);
if (msg === MIN_MSG || /.*Unknown argument.*/.test(msg) || /.*Not enough non-option arguments:.*/.test(msg)) {
console.error('\nUsage: %s', argv.help());
console.error('\n%s', argv.help());
}
process.exit(1);
};
Expand All @@ -84,16 +116,28 @@ class CLI {
const argv = yargs();
Object.values(this._commands).forEach(cmd => argv.command(cmd));

return logArgs(argv)
const ret = logArgs(argv)
.scriptName('hlx')
.usage('Usage: $0 <command> [options]')
.parserConfiguration({ 'camel-case-expansion': false })
.env('HLX')
.check(envAwareStrict)
.showHelpOnFail(true)
.fail(this._failFn)
.exitProcess(args.indexOf('--get-yargs-completions') > -1)
.strict()
.demandCommand(1, MIN_MSG)
.epilogue('for more information, find our manual at https://github.com/adobe/helix-cli')
.help()
.completion()
.parse(args);

// hack to check if command is valid in non-strict mode
const cmd = ret._[0];
if (cmd && !(cmd in this._commands)) {
console.error('Unknown command: %s\n', cmd);
argv.showHelp();
process.exit(1);
}
}
}

Expand Down
35 changes: 0 additions & 35 deletions src/defaults.js

This file was deleted.

1 change: 1 addition & 0 deletions src/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = function demo() {
describe: 'Parent directory of new project',
default: '.',
})
.env('NO_HLX_ENV_SUPPORT_FOR_DEMO')
.strict();
},
handler: async (argv) => {
Expand Down
24 changes: 13 additions & 11 deletions src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

'use strict';

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

module.exports = function deploy() {
Expand All @@ -25,34 +26,35 @@ module.exports = function deploy() {
command: 'deploy',
desc: 'Deploy packaged functions to Adobe I/O runtime',
builder: (yargs) => {
deployCommon(yargs);
yargsOpenwhisk(yargs);
yargsFastly(yargs);
yargs
.option('auto', {
describe: 'Enable auto-deployment',
type: 'boolean',
default: false,
demandOption: true,
})
.option('dry-run', {
alias: 'dryRun',
describe: 'List the actions that would be created, but do not actually deploy',
type: 'boolean',
default: false,
})
.option('loggly-host', {
alias: 'logglyHost',
describe: 'API Host for Log Appender',
type: 'string',
default: 'trieloff.loggly.com',
})
.option('loggly-auth', {
alias: 'logglyAuth',
describe: 'API Key for Log Appender ($HLX_LOGGLY_AUTH)',
type: 'string',
default: '',
})
.option('fastly-namespace', {
describe: 'CDN Namespace (e.g. Fastly Service ID)',
type: 'string',
})
.option('fastly-auth', {
describe: 'API Key for Fastly API ($HLX_FASTLY_AUTH)',
type: 'string',
default: '',
})
.option('circleci-auth', {
alias: 'circleciAuth',
describe: 'API Key for CircleCI API ($HLX_CIRCLECI_AUTH)',
type: 'string',
default: '',
Expand Down
8 changes: 2 additions & 6 deletions src/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

'use strict';

/* eslint no-console: off */
// TODO: remove the following line
/* eslint no-unused-vars: off */

const { makeLogger } = require('./log-common.js');

module.exports = function perf() {
Expand All @@ -29,14 +25,14 @@ module.exports = function perf() {
desc: 'Test performance',
builder: (yargs) => {
yargs
.env('HLX')
.strict(false)
.option('fastly-namespace', {
alias: 'fastlyNamespace',
describe: 'CDN Namespace (e.g. Fastly Service ID)',
type: 'string',

})
.option('fastly-auth', {
alias: 'fastlyAuth',
describe: 'API Key for Fastly API ($HLX_FASTLY_AUTH)',
type: 'string',
})
Expand Down
16 changes: 6 additions & 10 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

'use strict';

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

module.exports = function strain() {
Expand All @@ -25,17 +26,11 @@ module.exports = function strain() {
command: ['publish'],
desc: 'Activate strains in the Fastly CDN and publish the site',
builder: (yargs) => {
deployCommon(yargs);
yargsOpenwhisk(yargs);
yargsFastly(yargs);
yargs
.option('fastly-namespace', {
describe: 'CDN Namespace (e.g. Fastly Service ID)',
type: 'string',
})
.option('fastly-auth', {
describe: 'API Key for Fastly API ($HLX_FASTLY_AUTH)',
type: 'string',
})
.option('dry-run', {
alias: 'dryRun',
describe: 'List the actions that would be created, but do not actually deploy',
type: 'boolean',
default: false,
Expand All @@ -46,6 +41,7 @@ module.exports = function strain() {
default: true,
})
.option('api-publish', {
alias: 'apiPublish',
describe: 'API URL for helix-publish service',
type: 'string',
default: 'https://adobeioruntime.net/api/v1/web/helix/default/publish',
Expand Down
5 changes: 3 additions & 2 deletions src/up.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/* eslint global-require: off */

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

module.exports = function up() {
Expand All @@ -26,7 +26,7 @@ module.exports = function up() {
command: 'up [files...]',
description: 'Run a Helix development server',
builder: (yargs) => {
defaultArgs(yargs);
yargsBuild(yargs);
yargs
.option('open', {
describe: 'Open a browser window',
Expand All @@ -38,6 +38,7 @@ module.exports = function up() {
type: 'string',
})
.option('save-config', {
alias: 'saveConfig',
describe: 'Saves the default config.',
type: 'boolean',
default: false,
Expand Down
Loading

0 comments on commit 8f82f00

Please sign in to comment.