1
0
mirror of synced 2025-11-08 12:57:47 +00:00

refactor(CLI): handle new options

This commit is contained in:
Jason Dreyzehner
2018-03-11 16:24:25 -04:00
parent ee930cc8e9
commit 583915a99e
4 changed files with 79 additions and 32 deletions

37
.vscode/launch.json vendored
View File

@@ -4,10 +4,13 @@
{
"type": "node",
"request": "launch",
"name": "Debug CLI",
"name": "Debug Project",
// we test in `build` to make cleanup fast and easy
"cwd": "${workspaceFolder}/build",
// Replace this with your project root. If there are multiple, you can
// automatically run the currently visible file with: "program": ${file}"
"program": "${workspaceFolder}/src/cli/cli.ts",
// "args": ["--no-install"],
"outFiles": ["${workspaceFolder}/build/main/**/*.js"],
"skipFiles": [
"<node_internals>/**/*.js",
@@ -28,7 +31,7 @@
// the debugger will open the source *.spec.ts file for debugging.
"type": "node",
"request": "launch",
"name": "Debug Compiled Test File",
"name": "Debug Visible Compiled Spec",
"program": "${workspaceFolder}/node_modules/ava/profile.js",
"args": [
"${file}"
@@ -44,13 +47,14 @@
// "preLaunchTask": "npm: build",
// "smartStep": true,
"runtimeArgs": ["--nolazy"]
}
},
// --- cut here ---
// TODO: Simpler test debugging option. Discussion:
// https://github.com/avajs/ava/issues/1505#issuecomment-370654427
// {
// "type": "node",
// "request": "launch",
// "name": "Debug Current Test File",
// "name": "Debug Visible Compiled Spec",
// "program": "${file}",
// "outFiles": ["${workspaceFolder}/build/main/**/*.js"],
// "skipFiles": ["<node_internals>/**/*.js"],
@@ -63,5 +67,30 @@
// "AVA_DEBUG_MODE": "1"
// }
// }
// CLI:
{
"type": "node",
"request": "launch",
"name": "Debug CLI Unit Tests",
"program": "${workspaceFolder}/node_modules/ava/profile.js",
"args": ["${workspaceFolder}/build/main/cli/tests/cli.unit.spec.js"],
"skipFiles": ["<node_internals>/**/*.js"],
// "preLaunchTask": "npm: build",
// "smartStep": true,
"runtimeArgs": ["--nolazy"]
},
{
"type": "node",
"request": "launch",
"name": "Debug CLI Integration Tests",
"program": "${workspaceFolder}/node_modules/ava/profile.js",
"args": [
"${workspaceFolder}/build/main/cli/tests/cli.integration.spec.js"
],
"skipFiles": ["<node_internals>/**/*.js"],
// "preLaunchTask": "npm: build",
// "smartStep": true,
"runtimeArgs": ["--nolazy"]
}
]
}

View File

@@ -20,7 +20,10 @@ export async function checkArgs(): Promise<
--yarn use yarn (default: npm)
--node include node.js type definitions
--dom include DOM type definitions
--noinstall skip yarn/npm install
--no-install skip yarn/npm install
--strict Enable stricter type-checking
--no-immutable Don't enable tslint-immutable
--no-vscode Don't include VS Code debugging config
Non-Interactive Example
$ typescript-starter my-library -d 'do something, better'
@@ -36,14 +39,26 @@ export async function checkArgs(): Promise<
default: false,
type: 'boolean'
},
immutable: {
default: true,
type: 'boolean'
},
install: {
default: true,
type: 'boolean'
},
node: {
default: false,
type: 'boolean'
},
noinstall: {
strict: {
default: false,
type: 'boolean'
},
vscode: {
default: true,
type: 'boolean'
},
yarn: {
default: false,
type: 'boolean'
@@ -72,10 +87,10 @@ export async function checkArgs(): Promise<
const input = cli.input[0];
if (!input) {
// no project-name provided, return to collect options in interactive mode
// note: we always return `install`, so --noinstall always works
// note: we always return `install`, so --no-install always works
// (important for test performance)
return {
install: !cli.flags.noinstall
install: cli.flags.install
};
}
const validOrMsg = await validateName(input);
@@ -86,9 +101,12 @@ export async function checkArgs(): Promise<
return {
description: cli.flags.description,
domDefinitions: cli.flags.dom,
install: !cli.flags.noinstall,
immutable: cli.flags.immutable,
install: cli.flags.install,
nodeDefinitions: cli.flags.node,
projectName: input,
runner: cli.flags.yarn ? Runner.Yarn : Runner.Npm
runner: cli.flags.yarn ? Runner.Yarn : Runner.Npm,
strict: cli.flags.strict,
vscode: cli.flags.vscode
};
}

View File

@@ -128,7 +128,7 @@ test(`${
`${TestDirectories.one}`,
// (user entered `-d='example description 1'`)
`-d=${description}`,
'--noinstall'
'--no-install'
],
{
cwd: buildDir,
@@ -166,7 +166,7 @@ test(`${
'--yarn',
'--node',
'--dom',
'--noinstall'
'--no-install'
],
{
cwd: buildDir,
@@ -206,7 +206,7 @@ async function testInteractive(
entry: ReadonlyArray<string | ReadonlyArray<string>>
): Promise<execa.ExecaReturns> {
const typeDefs = entry[3] !== '';
const proc = execa(`../bin/typescript-starter`, ['--noinstall'], {
const proc = execa(`../bin/typescript-starter`, ['--no-install'], {
cwd: buildDir,
env: {
TYPESCRIPT_STARTER_REPO_URL: repoURL
@@ -349,6 +349,14 @@ const sandboxOptions = {
workingDirectory: buildDir
};
const silenceConsole = (console: any) => {
// tslint:disable-next-line:no-object-mutation
console.log = () => {
// mock console.log to silence it
return;
};
};
test(`${
TestDirectories.five
}: Sandboxed: npm install, initial commit`, async t => {
@@ -366,15 +374,8 @@ test(`${
strict: true,
vscode: false
};
const log = console.log;
// tslint:disable-next-line:no-object-mutation
console.log = () => {
// mock console.log to silence it
return;
};
silenceConsole(console);
await typescriptStarter(options, sandboxTasks(t, true, true));
// tslint:disable-next-line:no-object-mutation
console.log = log; // and put it back
const map = await hashAllTheThings(TestDirectories.five, true);
t.deepEqual(map, {
'test-5/LICENSE': 'd11b4dba04062af8bd80b052066daf1c',
@@ -406,15 +407,8 @@ test(`${TestDirectories.six}: Sandboxed: yarn, no initial commit`, async t => {
strict: false,
vscode: true
};
const log = console.log;
// tslint:disable-next-line:no-object-mutation
console.log = () => {
// mock console.log to silence it
return;
};
silenceConsole(console);
await typescriptStarter(options, sandboxTasks(t, false, true));
// tslint:disable-next-line:no-object-mutation
console.log = log; // and put it back
const map = await hashAllTheThings(TestDirectories.six, true);
t.deepEqual(map, {
'test-6/LICENSE': '1dfe8c78c6af40fc14ea3b40133f1fa5',

View File

@@ -74,20 +74,26 @@ test('checkArgs returns the right options', async t => {
'--yarn',
'--node',
'--dom',
'--noinstall'
'--no-install',
'--strict',
'--no-immutable',
'--no-vscode'
];
const opts = await checkArgs();
t.deepEqual(opts, {
description: '',
domDefinitions: true,
immutable: false,
install: false,
nodeDefinitions: true,
projectName: 'example-project',
runner: Runner.Yarn
runner: Runner.Yarn,
strict: true,
vscode: false
});
});
test('checkArgs always returns { install } (so --noinstall works in interactive mode)', async t => {
test('checkArgs always returns { install } (so --no-install works in interactive mode)', async t => {
passUpdateNotifier('1.0.0');
// tslint:disable-next-line:no-object-mutation
process.argv = ['path/to/node', 'path/to/typescript-starter'];