Skip to content

Commit

Permalink
fix(watch): pass down flags (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 12, 2022
1 parent eae2663 commit cb89f95
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
17 changes: 11 additions & 6 deletions src/remove-argv-flags.ts
Expand Up @@ -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;
}
};
};
Expand Down
14 changes: 12 additions & 2 deletions src/watch/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 27 additions & 29 deletions tests/specs/watch.ts
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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 () => {
Expand All @@ -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<string>((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<string>((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 }) => {
Expand Down

0 comments on commit cb89f95

Please sign in to comment.