From cb89f95ba5274833fcea033c8c818c829c5e02be Mon Sep 17 00:00:00 2001 From: hiroki osame Date: Sat, 12 Nov 2022 08:12:38 -0500 Subject: [PATCH] fix(watch): pass down flags (#142) --- src/remove-argv-flags.ts | 17 +++++++----- src/watch/index.ts | 14 ++++++++-- tests/specs/watch.ts | 56 +++++++++++++++++++--------------------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/remove-argv-flags.ts b/src/remove-argv-flags.ts index 0278fd6e..6626b21e 100644 --- a/src/remove-argv-flags.ts +++ b/src/remove-argv-flags.ts @@ -4,17 +4,22 @@ import { type TypeFlagOptions, } from 'type-flag'; -export const ignoreAfterArgument = (): TypeFlagOptions['ignore'] => { +export const ignoreAfterArgument = ( + ignoreFirstArgument = true, +): TypeFlagOptions['ignore'] => { let ignore = false; return (type) => { - if (ignore) { + if ( + ignore + || type === 'unknown-flag' + ) { return true; } - const isArgument = type === 'argument'; - if (isArgument || type === 'unknown-flag') { - ignore = isArgument; - return true; + + if (type === 'argument') { + ignore = true; + return ignoreFirstArgument; } }; }; diff --git a/src/watch/index.ts b/src/watch/index.ts index 0be68172..7c48f15e 100644 --- a/src/watch/index.ts +++ b/src/watch/index.ts @@ -5,7 +5,10 @@ import path from 'path'; import { command } from 'cleye'; import { watch } from 'chokidar'; import { run } from '../run'; -import { removeArgvFlags } from '../remove-argv-flags'; +import { + removeArgvFlags, + ignoreAfterArgument, +} from '../remove-argv-flags'; import { clearScreen, debounce, @@ -41,9 +44,16 @@ export const watchCommand = command({ help: { description: 'Run the script and watch for changes', }, + + /** + * ignoreAfterArgument needs to parse the first argument + * because cleye will error on missing arguments + * + * Remove once cleye supports error callbacks on missing arguments + */ + ignoreArgv: ignoreAfterArgument(false), }, (argv) => { const rawArgvs = removeArgvFlags(flags, process.argv.slice(3)); - const options = { noCache: argv.flags.noCache, tsconfigPath: argv.flags.tsconfig, diff --git a/tests/specs/watch.ts b/tests/specs/watch.ts index 088d4c67..9a146afc 100644 --- a/tests/specs/watch.ts +++ b/tests/specs/watch.ts @@ -41,7 +41,7 @@ export default testSuite(async ({ describe }, fixturePath: string) => { tsxProcess.stdout!.on('data', onStdOut); tsxProcess.stderr!.on('data', onStdOut); }); - }, 5000); + }, 10_000); test('suppresses warnings & clear screen', async () => { const tsxProcess = tsx({ @@ -77,7 +77,7 @@ export default testSuite(async ({ describe }, fixturePath: string) => { expect(stdout).not.toMatch('Warning'); expect(stdout).toMatch('\u001Bc'); - }, 5000); + }, 10_000); test('passes flags', async () => { const tsxProcess = tsx({ @@ -102,7 +102,7 @@ export default testSuite(async ({ describe }, fixturePath: string) => { expect(stdout).toMatch('"--some-flag"'); await tsxProcess; - }, 5000); + }, 10_000); describe('help', ({ test }) => { test('shows help', async () => { @@ -115,32 +115,30 @@ export default testSuite(async ({ describe }, fixturePath: string) => { expect(tsxProcess.stderr).toBe(''); }); - // BUGL Currently doesn't pass through `--help - // test('doesn\'t show help with file', async () => { - // const tsxProcess = tsx({ - // args: [ - // 'watch', - // path.join(fixturePath, 'log-argv.ts'), - // '--help', - // ], - // }); - - // const stdout = await new Promise((resolve) => { - // tsxProcess.stdout!.on('data', (chunk) => { - // const chunkString = chunk.toString(); - // if (chunkString.startsWith('[')) { - // resolve(chunkString); - // } - // }); - // }); - - // tsxProcess.kill(); - - // console.log(stdout); - // expect(stdout).toMatch('"--help"'); - - // await tsxProcess; - // }, 2000); + test('passes down --help to file', async () => { + const tsxProcess = tsx({ + args: [ + 'watch', + path.join(fixturePath, 'log-argv.ts'), + '--help', + ], + }); + + const stdout = await new Promise((resolve) => { + tsxProcess.stdout!.on('data', (chunk) => { + const chunkString = chunk.toString(); + if (chunkString.startsWith('[')) { + resolve(chunkString); + } + }); + }); + + tsxProcess.kill(); + + expect(stdout).toMatch('"--help"'); + + await tsxProcess; + }, 5000); }); describe('ignore', ({ test }) => {