Skip to content

Commit 89f11a5

Browse files
authored
Add --preserveWatchOutput option (#288)
* add --preserveWatchOutput option * fix forgotten change in #286
1 parent 352f53c commit 89f11a5

8 files changed

Lines changed: 57 additions & 18 deletions

File tree

.changeset/gold-moles-lead.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@css-modules-kit/codegen': minor
3+
---
4+
5+
feat: add --preserveWatchOutput option

.vscode/launch.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "node",
77
"request": "launch",
88
"cwd": "${workspaceFolder}/examples/1-basic",
9-
"program": "${workspaceFolder}/packages/codegen/bin/cmk.mjs",
9+
"program": "${workspaceFolder}/packages/codegen/bin/cmk.js",
1010
"console": "integratedTerminal",
1111
"preLaunchTask": "npm: build - packages/codegen",
1212
"presentation": {
@@ -18,8 +18,8 @@
1818
"type": "node",
1919
"request": "launch",
2020
"cwd": "${workspaceFolder}/examples/1-basic",
21-
"program": "${workspaceFolder}/packages/codegen/bin/cmk.mjs",
22-
"args": ["--watch"],
21+
"program": "${workspaceFolder}/packages/codegen/bin/cmk.js",
22+
"args": ["--watch", "--preserveWatchOutput"],
2323
"console": "integratedTerminal",
2424
"preLaunchTask": "npm: build - packages/codegen",
2525
"presentation": {
@@ -31,7 +31,7 @@
3131
"type": "node",
3232
"request": "launch",
3333
"cwd": "${workspaceFolder}/examples/2-named-exports",
34-
"program": "${workspaceFolder}/packages/codegen/bin/cmk.mjs",
34+
"program": "${workspaceFolder}/packages/codegen/bin/cmk.js",
3535
"console": "integratedTerminal",
3636
"preLaunchTask": "npm: build - packages/codegen",
3737
"presentation": {
@@ -43,7 +43,7 @@
4343
"type": "node",
4444
"request": "launch",
4545
"cwd": "${workspaceFolder}/examples/3-import-alias",
46-
"program": "${workspaceFolder}/packages/codegen/bin/cmk.mjs",
46+
"program": "${workspaceFolder}/packages/codegen/bin/cmk.js",
4747
"console": "integratedTerminal",
4848
"preLaunchTask": "npm: build - packages/codegen",
4949
"presentation": {

packages/codegen/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ $ npx cmk --help
3636
Usage: cmk [options]
3737

3838
Options:
39-
--help, -h Show help information
40-
--version, -v Show version number
41-
--project, -p The path to its configuration file, or to a folder with a 'tsconfig.json'.
42-
--pretty Enable color and formatting in output to make errors easier to read.
43-
--clean Remove the output directory before generating files. [default: false]
44-
--watch, -w Watch for changes and regenerate files. [default: false]
39+
--help, -h Show help information
40+
--version, -v Show version number
41+
--project, -p The path to its configuration file, or to a folder with a 'tsconfig.json'.
42+
--pretty Enable color and formatting in output to make errors easier to read.
43+
--clean Remove the output directory before generating files. [default: false]
44+
--watch, -w Watch for changes and regenerate files. [default: false]
45+
--preserveWatchOutput Disable wiping the console in watch mode. [default: false]
4546
```
4647

4748
## Configuration

packages/codegen/src/cli.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('parseCLIArgs', () => {
1515
pretty: undefined,
1616
clean: false,
1717
watch: false,
18+
preserveWatchOutput: false,
1819
});
1920
});
2021
it('should parse --help option', () => {
@@ -47,6 +48,10 @@ describe('parseCLIArgs', () => {
4748
expect(parseCLIArgs(['--no-watch'], cwd).watch).toBe(false);
4849
expect(parseCLIArgs(['-w'], cwd).watch).toBe(true);
4950
});
51+
it('should parse --preserveWatchOutput option', () => {
52+
expect(parseCLIArgs(['--preserveWatchOutput'], cwd).preserveWatchOutput).toBe(true);
53+
expect(parseCLIArgs(['--no-preserveWatchOutput'], cwd).preserveWatchOutput).toBe(false);
54+
});
5055
it('should throw ParseCLIArgsError for invalid options', () => {
5156
expect(() => parseCLIArgs(['--invalid-option'], cwd)).toThrow(ParseCLIArgsError);
5257
});

packages/codegen/src/cli.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import { resolve } from '@css-modules-kit/core';
33
import packageJson from '../package.json' with { type: 'json' };
44
import { ParseCLIArgsError } from './error.js';
55

6+
// NOTE: Keep this help text in sync with the one in packages/codegen/README.md.
67
const helpText = `
78
Usage: cmk [options]
89
910
Options:
10-
--help, -h Show help information
11-
--version, -v Show version number
12-
--project, -p The path to its configuration file, or to a folder with a 'tsconfig.json'.
13-
--pretty Enable color and formatting in output to make errors easier to read.
14-
--clean Remove the output directory before generating files. [default: false]
15-
--watch, -w Watch for changes and regenerate files. [default: false]
11+
--help, -h Show help information
12+
--version, -v Show version number
13+
--project, -p The path to its configuration file, or to a folder with a 'tsconfig.json'.
14+
--pretty Enable color and formatting in output to make errors easier to read.
15+
--clean Remove the output directory before generating files. [default: false]
16+
--watch, -w Watch for changes and regenerate files. [default: false]
17+
--preserveWatchOutput Disable wiping the console in watch mode. [default: false]
1618
`;
1719

1820
export function printHelpText(): void {
@@ -32,6 +34,7 @@ export interface ParsedArgs {
3234
pretty: boolean | undefined;
3335
clean: boolean;
3436
watch: boolean;
37+
preserveWatchOutput: boolean;
3538
}
3639

3740
/**
@@ -49,6 +52,7 @@ export function parseCLIArgs(args: string[], cwd: string): ParsedArgs {
4952
pretty: { type: 'boolean' },
5053
clean: { type: 'boolean', default: false },
5154
watch: { type: 'boolean', short: 'w', default: false },
55+
preserveWatchOutput: { type: 'boolean', default: false },
5256
},
5357
allowNegative: true,
5458
});
@@ -59,6 +63,7 @@ export function parseCLIArgs(args: string[], cwd: string): ParsedArgs {
5963
pretty: values.pretty,
6064
clean: values.clean,
6165
watch: values.watch,
66+
preserveWatchOutput: values.preserveWatchOutput,
6267
};
6368
} catch (cause) {
6469
throw new ParseCLIArgsError(cause);

packages/codegen/src/runner.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,20 @@ describe('runCMKInWatchMode', () => {
289289
expect(formatDiagnostics(loggerSpy.logDiagnostics.mock.calls[0]![0], iff.rootDir)).length(3);
290290
});
291291
});
292+
test('does not clear screen when preserveWatchOutput is true', async () => {
293+
const iff = await createIFF({
294+
'tsconfig.json': '{}',
295+
'src/a.module.css': '.a_1 { color: red; }',
296+
});
297+
298+
const loggerSpy1 = createLoggerSpy();
299+
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir, preserveWatchOutput: false }), loggerSpy1);
300+
expect(loggerSpy1.clearScreen).toHaveBeenCalledTimes(1);
301+
await watcher.close();
302+
303+
const loggerSpy2 = createLoggerSpy();
304+
// eslint-disable-next-line require-atomic-updates
305+
watcher = await runCMKInWatchMode(fakeParsedArgs({ project: iff.rootDir, preserveWatchOutput: true }), loggerSpy2);
306+
expect(loggerSpy2.clearScreen).toHaveBeenCalledTimes(0);
307+
});
292308
});

packages/codegen/src/runner.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createProject, type Project } from './project.js';
77
interface RunnerArgs {
88
project: string;
99
clean: boolean;
10+
preserveWatchOutput: boolean;
1011
}
1112

1213
export interface Watcher {
@@ -129,7 +130,9 @@ export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promi
129130
* @throws {WriteDtsFileError}
130131
*/
131132
async function emitAndReportDiagnostics() {
132-
logger.clearScreen();
133+
if (!args.preserveWatchOutput) {
134+
logger.clearScreen();
135+
}
133136
await project.emitDtsFiles();
134137
const diagnostics = project.getDiagnostics();
135138
if (diagnostics.length > 0) {
@@ -139,6 +142,9 @@ export async function runCMKInWatchMode(args: RunnerArgs, logger: Logger): Promi
139142
`Found ${diagnostics.length} error${diagnostics.length === 1 ? '' : 's'}. Watching for file changes.`,
140143
{ time: true },
141144
);
145+
if (args.preserveWatchOutput) {
146+
logger.logMessage('');
147+
}
142148
}
143149

144150
async function close() {

packages/codegen/src/test/faker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function fakeParsedArgs(args?: Partial<ParsedArgs>): ParsedArgs {
88
pretty: undefined,
99
clean: false,
1010
watch: false,
11+
preserveWatchOutput: false,
1112
...args,
1213
};
1314
}

0 commit comments

Comments
 (0)