-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start re-factoring the CLI codebase #177
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 async function handler(): Promise<never> { | ||
let exitStatus: number = 0; | ||
for (const verification of verifications) { | ||
if (!await verification()) { | ||
exitStatus = -1; | ||
} | ||
} | ||
return process.exit(exitStatus); | ||
} | ||
|
||
const verifications: Array<() => boolean | Promise<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('../lib/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('../lib/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(); | ||
} | ||
} | ||
} | ||
}); |
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,56 @@ | ||
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; | ||
} | ||
|
||
module.exports = testCase({ | ||
'`cdk doctor`': { | ||
'setUp'(cb: ICallbackFunction) { | ||
exitCalled = false; | ||
exitStatus = undefined; | ||
mockery.registerMock('../../lib/logging', { | ||
print: () => undefined | ||
}); | ||
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/package.json', { version: 'x.y.z' }); | ||
|
||
try { | ||
await require('../lib/commands/doctor').handler(); | ||
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('../lib/commands/doctor').handler(); | ||
test.ok(exitCalled, 'process.exit() was called'); | ||
test.notEqual(exitStatus, 0, 'exit status was non-0'); | ||
} catch (e) { | ||
test.ifError(e); | ||
} finally { | ||
test.done(); | ||
} | ||
} | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a note that this must be required first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be required whenever... But I guess if you want to use it, you have to not
require
the stuff you want it to change how they arerequire
d before you've configured it... Which feels kind of obvious...