Skip to content
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

fix(watch): pass down flags #142

Merged
merged 1 commit into from Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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