-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking down the monolithic CLI infrastructure by using `yarg`'s `commandDir` directive (see #176). This allows modelling each command in a separate module for a cleaner interface. Migrated the `docs` command, and created a prototype of a `doctor` command (see #154). The new commands also have basic unit tests that verify the handlers honor their promises.
- Loading branch information
1 parent
2059899
commit c09ad3f
Showing
7 changed files
with
264 additions
and
39 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { exec } from 'child_process'; | ||
import { green } from 'colors/safe'; | ||
import * as process from 'process'; | ||
import * as yargs from 'yargs'; | ||
import { debug, error, warning } from '../../lib/logging'; | ||
|
||
export const command = 'docs'; | ||
export const describe = 'Opens the documentation in a browser'; | ||
export const aliases = ['doc']; | ||
export const builder = { | ||
browser: { | ||
alias: 'b', | ||
desc: 'the command to use to open the browser, using %u as a placeholder for the path of the file to open', | ||
type: 'string', | ||
default: process.platform === 'win32' ? 'start %u' : 'open %u' | ||
} | ||
}; | ||
|
||
export interface Arguments extends yargs.Arguments { | ||
browser: string | ||
} | ||
|
||
export async function handler(argv: Arguments) { | ||
let documentationIndexPath: string; | ||
try { | ||
// tslint:disable-next-line:no-var-require Taking an un-declared dep on aws-cdk-docs, to avoid a dependency circle | ||
const docs = require('aws-cdk-docs'); | ||
documentationIndexPath = docs.documentationIndexPath; | ||
} catch (err) { | ||
error('Unable to open CDK documentation - the aws-cdk-docs package appears to be missing. Please run `npm install -g aws-cdk-docs`'); | ||
process.exit(-1); | ||
return; | ||
} | ||
|
||
const browserCommand = argv.browser.replace(/%u/g, documentationIndexPath); | ||
debug(`Opening documentation ${green(browserCommand)}`); | ||
process.exit(await new Promise<number>((resolve, reject) => { | ||
exec(browserCommand, (err, stdout, stderr) => { | ||
if (err) { return reject(err); } | ||
if (stdout) { debug(stdout); } | ||
if (stderr) { warning(stderr); } | ||
resolve(0); | ||
}); | ||
})); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { blue, green } from 'colors/safe'; | ||
import * as process from 'process'; | ||
import { print } from '../../lib/logging'; | ||
import { VERSION } from '../../lib/version'; | ||
|
||
export const command = 'doctor'; | ||
export const describe = 'Check your set-up for potential problems'; | ||
export const builder = {}; | ||
|
||
export function handler() { | ||
let exitStatus: number = 0; | ||
for (const verification of verifications) { | ||
if (!verification()) { | ||
exitStatus = -1; | ||
} | ||
} | ||
process.exit(exitStatus); | ||
} | ||
|
||
const verifications: Array<() => boolean> = [ | ||
displayVersionInformation, | ||
displayAwsEnvironmentVariables, | ||
checkDocumentationIsAvailable | ||
]; | ||
|
||
// ### Verifications ### | ||
|
||
function displayVersionInformation() { | ||
print(`ℹ️ CDK Version: ${green(VERSION)}`); | ||
return true; | ||
} | ||
|
||
function displayAwsEnvironmentVariables() { | ||
const keys = Object.keys(process.env).filter(s => s.startsWith('AWS_')); | ||
if (keys.length === 0) { | ||
print('ℹ️ No AWS environment variables'); | ||
return true; | ||
} | ||
print('ℹ️ AWS environment variables:'); | ||
for (const key of keys) { | ||
print(` - ${blue(key)} = ${green(process.env[key]!)}`); | ||
} | ||
return true; | ||
} | ||
|
||
function checkDocumentationIsAvailable() { | ||
try { | ||
const version = require('aws-cdk-docs/package.json').version; | ||
print(`✅ AWS CDK Documentation: ${version}`); | ||
return true; | ||
} catch (e) { | ||
print(`❌ AWS CDK Documentation: install using ${green('y-npm install --global aws-cdk-docs')}`); | ||
return false; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import * as mockery from 'mockery'; | ||
import { ICallbackFunction, Test, testCase } from 'nodeunit'; | ||
|
||
let exitCalled: boolean = false; | ||
let exitStatus: undefined | number; | ||
function fakeExit(status?: number) { | ||
exitCalled = true; | ||
exitStatus = status; | ||
} | ||
|
||
const argv = { browser: 'echo %u' }; | ||
|
||
module.exports = testCase({ | ||
'`cdk docs`': { | ||
'setUp'(cb: ICallbackFunction) { | ||
exitCalled = false; | ||
exitStatus = undefined; | ||
mockery.registerMock('../../lib/logging', { | ||
debug() { return; }, | ||
error() { return; }, | ||
warning() { return; } | ||
}); | ||
mockery.enable({ useCleanCache: true, warnOnReplace: true, warnOnUnregistered: false }); | ||
cb(); | ||
}, | ||
'tearDown'(cb: ICallbackFunction) { | ||
mockery.disable(); | ||
mockery.deregisterAll(); | ||
|
||
cb(); | ||
}, | ||
async 'exits with 0 when everything is OK'(test: Test) { | ||
mockery.registerMock('process', { ...process, exit: fakeExit }); | ||
mockery.registerMock('aws-cdk-docs', { documentationIndexPath: 'index.html' }); | ||
|
||
try { | ||
await require('../bin/commands/docs').handler(argv); | ||
test.ok(exitCalled, 'process.exit() was called'); | ||
test.equal(exitStatus, 0, 'exit status was 0'); | ||
} catch (e) { | ||
test.ifError(e); | ||
} finally { | ||
test.done(); | ||
} | ||
}, | ||
async 'exits with non-0 when documentation is missing'(test: Test) { | ||
mockery.registerMock('process', { ...process, exit: fakeExit }); | ||
|
||
try { | ||
await require('../bin/commands/docs').handler(argv); | ||
test.ok(exitCalled, 'process.exit() was called'); | ||
test.notEqual(exitStatus, 0, 'exit status was non-0'); | ||
} catch (e) { | ||
test.ifError(e); | ||
} finally { | ||
test.done(); | ||
} | ||
} | ||
} | ||
}); |
Oops, something went wrong.