Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/cli/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ await yargs(hideBin(process.argv))
const closeHandlers: (() => Promise<void>)[] = [];
const resolvedDir = path.resolve(args.dir);

await bundleSource(resolvedDir, logger);

const handleClose = async (): Promise<void> => {
await Promise.all(
closeHandlers.map(async (close) => withTimeout(close(), 400)),
Expand Down
21 changes: 11 additions & 10 deletions packages/cli/src/commands/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { unlink } from 'fs/promises';
import { describe, it, expect, vi, beforeEach } from 'vitest';

import { bundleFile } from './bundle.ts';
import { watchDir, makeWatchEvents, filterOnlyJsFiles } from './watch.ts';
import { watchDir, makeWatchEvents, shouldIgnore } from './watch.ts';

vi.mock('fs/promises', () => ({
unlink: vi.fn(async () => new Promise<void>(() => undefined)),
Expand Down Expand Up @@ -46,21 +46,22 @@ describe('watchDir', () => {
});
});

describe('filterOnlyJsFiles', () => {
describe('shouldIgnore', () => {
const missingStats = undefined as unknown as Stats;
const fileStats = { isFile: () => true } as unknown as Stats;
const nonFileStats = { isFile: () => false } as unknown as Stats;

it.each`
description | file | stats | expectation
${'a file with missing stats'} | ${'test.js'} | ${missingStats} | ${false}
${'a non-file'} | ${'test.js'} | ${nonFileStats} | ${false}
${'a .ts file'} | ${'test.ts'} | ${fileStats} | ${false}
${'a .bundle file'} | ${'test.bundle'} | ${fileStats} | ${false}
${'a .txt file'} | ${'test.txt'} | ${nonFileStats} | ${false}
${'a .js file'} | ${'test.js'} | ${fileStats} | ${true}
description | file | stats | expectation
${'a file with missing stats'} | ${'test.js'} | ${missingStats} | ${false}
${'a non-file'} | ${'test.js'} | ${nonFileStats} | ${false}
${'a .js file'} | ${'test.js'} | ${fileStats} | ${false}
${'a .ts file'} | ${'test.ts'} | ${fileStats} | ${true}
${'a .bundle file'} | ${'test.bundle'} | ${fileStats} | ${true}
${'a .txt file'} | ${'test.txt'} | ${fileStats} | ${true}
${'a .js file in node_modules'} | ${'node_modules/test.js'} | ${fileStats} | ${true}
`('returns $expectation for $description', ({ file, expectation, stats }) => {
expect(filterOnlyJsFiles(file, stats)).toBe(expectation);
expect(shouldIgnore(file, stats)).toBe(expectation);
});
});

Expand Down
17 changes: 7 additions & 10 deletions packages/cli/src/commands/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ export const makeWatchEvents = (
error: (error: Error) => throwError(error),
});

export const filterOnlyJsFiles: MatchFunction = (file, stats) =>
(stats?.isFile() ?? false) && file.endsWith('.js');
export const shouldIgnore: MatchFunction = (file, stats): boolean =>
// Ignore files and directories in `node_modules`.
file.includes('node_modules') ||
// Watch non-files, but ignore files that are not JavaScript.
((stats?.isFile() ?? false) && !file.endsWith('.js'));

/**
* Start a watcher that bundles `.js` files in the target dir.
Expand All @@ -77,14 +80,8 @@ export function watchDir(dir: string, logger: Logger): WatchDirReturn {
const { reject: throwError, promise: errorPromise } = makePromiseKit<never>();

let watcher = watch(resolvedDir, {
ignoreInitial: true,
ignored: [
'**/node_modules/**',
'**/*.test.js',
'**/*.test.ts',
'**/*.bundle',
filterOnlyJsFiles,
],
ignoreInitial: false,
ignored: shouldIgnore,
});

const events = makeWatchEvents(watcher, readyResolve, throwError, logger);
Expand Down
Loading