Skip to content

Commit

Permalink
Switching to yargs-parser rather than yargs (#356)
Browse files Browse the repository at this point in the history
* totally changing how args are parsed with yargs-parser

* fix up initCommand with new yargs types

* Change files

* fixing up resolve

* making the watch script to be consistent

* fixing the mocks with yargs init
  • Loading branch information
kenotron committed Apr 10, 2020
1 parent b57d6ac commit df664ff
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 212 deletions.
8 changes: 8 additions & 0 deletions change/create-just-2020-04-09-21-31-52-yargs-parser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "none",
"comment": "fix up initCommand with new yargs types",
"packageName": "create-just",
"email": "kchau@microsoft.com",
"commit": "565e98494de5bae050cdc04365dee0a0aef638d8",
"date": "2020-04-10T04:31:08.848Z"
}
8 changes: 8 additions & 0 deletions change/just-scripts-2020-04-09-21-31-52-yargs-parser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "patch",
"comment": "Jest now can take a positional arg to run a certain test pattern",
"packageName": "just-scripts",
"email": "kchau@microsoft.com",
"commit": "565e98494de5bae050cdc04365dee0a0aef638d8",
"date": "2020-04-10T04:31:34.381Z"
}
8 changes: 8 additions & 0 deletions change/just-task-2020-04-09-21-31-52-yargs-parser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "minor",
"comment": "Replace yargs with yargs-parser, taking over the command parsing",
"packageName": "just-task",
"email": "kchau@microsoft.com",
"commit": "565e98494de5bae050cdc04365dee0a0aef638d8",
"date": "2020-04-10T04:31:52.506Z"
}
2 changes: 1 addition & 1 deletion packages/create-just/src/commands/initCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function getStackPath(pathName: string, registry?: string) {
* 4. git init and commit
* 5. yarn install
*/
export async function initCommand(argv: yargs.Arguments) {
export async function initCommand(argv: yargs.Arguments<{ [key: string]: string }>) {
// TODO: autosuggest just-stack-* packages from npmjs.org
if (!argv.stack) {
const { stack } = await prompts({
Expand Down
10 changes: 9 additions & 1 deletion packages/just-scripts/src/tasks/jestTask.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve, logger, resolveCwd, TaskFunction } from 'just-task';
import { resolve, logger, resolveCwd, TaskFunction, argv } from 'just-task';
import { spawn, encodeArgs } from 'just-scripts-utils';
import { existsSync } from 'fs';
import supportsColor from 'supports-color';
Expand All @@ -11,6 +11,8 @@ export interface JestTaskOptions {
watch?: boolean;
colors?: boolean;
passWithNoTests?: boolean;
testPathPattern?: string;
testNamePattern?: string;
u?: boolean;
_?: string[];

Expand All @@ -35,6 +37,10 @@ export function jestTask(options: JestTaskOptions = {}): TaskFunction {
if (configFile && jestCmd && existsSync(configFile)) {
logger.info(`Running Jest`);
const cmd = process.execPath;

const positional = argv()._.slice(1);
options = { ...options, ...{ ...argv(), _: positional } };

const args = [
...(options.nodeArgs || []),
jestCmd,
Expand All @@ -45,6 +51,8 @@ export function jestTask(options: JestTaskOptions = {}): TaskFunction {
...(options.runInBand ? ['--runInBand'] : []),
...(options.coverage ? ['--coverage'] : []),
...(options.watch ? ['--watch'] : []),
...(options.testPathPattern ? ['--testPathPattern', options.testPathPattern] : []),
...(options.testNamePattern ? ['--testNamePattern', options.testNamePattern] : []),
...(options.u || options.updateSnapshot ? ['--updateSnapshot'] : ['']),
...(options._ || [])
].filter(arg => !!arg) as Array<string>;
Expand Down
9 changes: 5 additions & 4 deletions packages/just-task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@
},
"scripts": {
"build": "tsc",
"dev": "tsc -w --preserveWatchOutput",
"start": "tsc -w --preserveWatchOutput",
"start-test": "jest --watch",
"test": "jest"
},
"dependencies": {
"@microsoft/package-deps-hash": "^2.2.153",
"bach": "^1.2.0",
"chalk": "^2.4.1",
"fs-extra": "^7.0.1",
"just-task-logger": ">=0.3.0 <1.0.0",
"resolve": "^1.8.1",
"undertaker": "^1.2.0",
"undertaker": "^1.2.1",
"undertaker-registry": "^1.0.1",
"yargs": "^12.0.5"
"yargs-parser": "^18.1.2"
},
"devDependencies": {
"@types/fs-extra": "^5.0.4",
Expand All @@ -37,7 +38,7 @@
"@types/resolve": "^0.0.8",
"@types/undertaker": "^1.2.0",
"@types/undertaker-registry": "^1.0.1",
"@types/yargs": "12.0.1",
"@types/yargs-parser": "^15.0.0",
"jest": "^24.0.0",
"mock-fs": "^4.8.0",
"ts-jest": "^24.0.1",
Expand Down
115 changes: 0 additions & 115 deletions packages/just-task/src/JustTaskRegistry.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/just-task/src/__tests__/__mocks__/yargs.ts

This file was deleted.

21 changes: 0 additions & 21 deletions packages/just-task/src/__tests__/__mocks__/yargs/yargs.ts

This file was deleted.

27 changes: 15 additions & 12 deletions packages/just-task/src/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import mockfs from 'mock-fs';
import path from 'path';
import yargsMock from './__mocks__/yargs/yargs';
import {
_isFileNameLike,
_tryResolve,
resetResolvePaths,
resolveCwd,
addResolvePath,
resolve
} from '../resolve';
import { _isFileNameLike, _tryResolve, resetResolvePaths, resolveCwd, addResolvePath, resolve } from '../resolve';

import * as option from '../option';

describe('_isFileNameLike', () => {
it('returns false for empty input', () => {
Expand Down Expand Up @@ -83,6 +77,10 @@ describe('_tryResolve', () => {
});

describe('resolveCwd', () => {
beforeEach(() => {
jest.spyOn(option, 'argv').mockImplementation(() => ({ config: undefined } as any));
});

afterEach(() => {
mockfs.restore();
resetResolvePaths();
Expand Down Expand Up @@ -117,9 +115,10 @@ describe('resolveCwd', () => {
});

describe('resolve', () => {
jest.spyOn(option, 'argv').mockImplementation(() => ({ config: undefined } as any));

afterEach(() => {
mockfs.restore();
yargsMock.argv.config = undefined;
resetResolvePaths();
});

Expand All @@ -145,7 +144,9 @@ describe('resolve', () => {
mockfs({
a: { 'b.txt': '' }
});
yargsMock.argv.config = 'a/just-task.js';

jest.spyOn(option, 'argv').mockImplementation(() => ({ config: 'a/just-task.js' } as any));

expect(resolve('b.txt')).toContain(path.join('a', 'b.txt'));
});

Expand All @@ -166,7 +167,9 @@ describe('resolve', () => {
d: { 'b.txt': '' }, // wrong
'b.txt': '' // wrong
});
yargsMock.argv.config = 'a/just-task.js';

jest.spyOn(option, 'argv').mockImplementation(() => ({ config: 'a/just-task.js' } as any));

addResolvePath('c');
expect(resolve('b.txt', 'd')).toContain(path.join('d', 'b.txt'));
});
Expand Down
12 changes: 7 additions & 5 deletions packages/just-task/src/__tests__/task.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { task } from '../task';
import { parallel, undertaker } from '../undertaker';
import { JustTaskRegistry } from '../JustTaskRegistry';
import UndertakerRegistry from 'undertaker-registry';

import { logger } from '../logger';
import yargsMock from './__mocks__/yargs';
import path from 'path';

import * as option from '../option';

describe('task', () => {
beforeAll(() => {
yargsMock.argv.config = path.resolve(__dirname, '__mocks__/just-task.js');
jest.spyOn(option, 'argv').mockImplementation(() => ({ config: path.resolve(__dirname, '__mocks__/just-task.js') } as any));
jest.spyOn(logger, 'info').mockImplementation(() => undefined);
});

beforeEach(() => {
undertaker.registry(new JustTaskRegistry());
undertaker.registry(new UndertakerRegistry());
});

afterAll(() => {
yargsMock.argv.config = undefined;
jest.spyOn(option, 'argv').mockImplementation(() => ({ config: 'a/just-task.js' } as any));
jest.restoreAllMocks();
});

Expand Down
42 changes: 31 additions & 11 deletions packages/just-task/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { undertaker } from './undertaker';
import { JustTaskRegistry } from './JustTaskRegistry';
import yargs from 'yargs';
import { option, parseCommand } from './option';
import { logger } from 'just-task-logger';
import { TaskFunction } from './interfaces';
import { readConfig } from './config';

const originalEmitWarning = process.emitWarning;

Expand All @@ -17,15 +19,33 @@ const originalEmitWarning = process.emitWarning;
return originalEmitWarning.apply(this, arguments);
};

yargs
.option({ config: { describe: 'path to a just-task.js file (includes the file name)' } })
.usage('$0 <cmd> [options]')
.updateStrings({
'Commands:': 'Tasks:\n'
});
function showHelp() {
const tasks = undertaker.registry().tasks();

const registry = new JustTaskRegistry();
console.log('All the tasks that are available to just:');

undertaker.registry(registry);
for (const [name, wrappedTask] of Object.entries(tasks)) {
const unwrapped = (wrappedTask as any).unwrap ? (wrappedTask as any).unwrap() : (wrappedTask as TaskFunction);
const description = (unwrapped as TaskFunction).description;
console.log(` ${name}${description ? `: ${description}` : ''}`);
}
}

// Define a built-in option of "config" so users can specify which path to choose for configurations
option('config', { describe: 'path to a just configuration file (includes the file name, e.g. /path/to/just.config.ts)' });

readConfig();

yargs.parse();
const registry = undertaker.registry();

const command = parseCommand();

if (command) {
if (registry.get(command)) {
undertaker.series(registry.get(command))(() => {});
} else {
logger.error(`Command not defined: ${command}`);
}
} else {
showHelp();
}

0 comments on commit df664ff

Please sign in to comment.