Skip to content

Commit 6c6d6ea

Browse files
feat(logger): add codemode name
1 parent 19feb4a commit 6c6d6ea

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

recipes/correct-ts-specifiers/src/workflow.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import module from 'node:module';
22

33
import { type Api, api } from '@codemod.com/workflow';
44
import type { Helpers } from '@codemod.com/workflow/dist/jsFam.d.ts';
5+
import { setCodemodName } from '@nodejs/codemod-utils/logger';
56

67
import { mapImports } from './map-imports.ts';
78
import type { FSAbsolutePath } from './index.d.ts';
89

910
import * as aliasLoader from '@nodejs-loaders/alias/alias.loader.mjs';
1011

12+
setCodemodName('correct-ts-specifiers');
13+
1114
module.registerHooks(aliasLoader);
1215

1316
export async function workflow({ contexts, files }: Api) {

recipes/import-assertions-to-attributes/src/re-work.ts

Whitespace-only changes.

utils/src/logger.test.snap.cjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
exports[`logger > should emit error entries to standard error, collated by source module 1`] = `
2-
" • sh*t happened\\n • maybe bad\\n • sh*t happened\\n • maybe other bad\\n[Codemod: correct-ts-specifiers]: migration incomplete!\\n"
2+
" • sh*t happened\\n • maybe bad\\n • sh*t happened\\n • maybe other bad\\n[Codemod: test-codemod]: migration incomplete!\\n"
33
`;
44

55
exports[`logger > should emit non-error entries to standard out, collated by source module 1`] = `
6-
"[Codemod: correct-ts-specifiers]: /tmp/foo.js\\n • maybe don’t\\n • maybe not that either\\n • still maybe don’t\\n • more maybe not\\n[Codemod: correct-ts-specifiers]: migration complete!\\n"
6+
"[Codemod: test-codemod]: /tmp/foo.js\\n • maybe don’t\\n • maybe not that either\\n • still maybe don’t\\n • more maybe not\\n[Codemod: test-codemod]: migration complete!\\n"
7+
`;
8+
9+
exports[`logger > should work without a codemod name 1`] = `
10+
"[Codemod: nodjs-codemod]: /tmp/foo.js\\n • maybe don’t\\n • maybe not that either\\n • still maybe don’t\\n • more maybe not\\n[Codemod: nodjs-codemod]: migration complete!\\n"
711
`;

utils/src/logger.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ describe('logger', { concurrency: true }, () => {
1313
'--experimental-strip-types',
1414
'-e',
1515
dedent`
16-
import { logger } from './logger.ts';
16+
import { logger, setCodemodName } from './logger.ts';
17+
18+
setCodemodName('test-codemod');
1719
1820
const source1 = '/tmp/foo.js';
1921
logger(source1, 'log', 'maybe don’t');
@@ -41,7 +43,9 @@ describe('logger', { concurrency: true }, () => {
4143
'--experimental-strip-types',
4244
'-e',
4345
dedent`
44-
import { logger } from './logger.ts';
46+
import { logger, setCodemodName } from './logger.ts';
47+
48+
setCodemodName('test-codemod');
4549
4650
const source1 = '/tmp/foo.js';
4751
logger(source1, 'error', 'sh*t happened');
@@ -60,4 +64,32 @@ describe('logger', { concurrency: true }, () => {
6064
t.assert.snapshot(stderr);
6165
assert.equal(code, 1);
6266
});
67+
68+
it('should work without a codemod name', async (t) => {
69+
const { code, stdout } = await spawnPromisified(
70+
execPath,
71+
[
72+
'--no-warnings',
73+
'--experimental-strip-types',
74+
'-e',
75+
dedent`
76+
import { logger } from './logger.ts';
77+
78+
const source1 = '/tmp/foo.js';
79+
logger(source1, 'log', 'maybe don’t');
80+
logger(source1, 'log', 'maybe not that either');
81+
82+
const source2 = '/tmp/foo.js';
83+
logger(source2, 'log', 'still maybe don’t');
84+
logger(source2, 'log', 'more maybe not');
85+
`,
86+
],
87+
{
88+
cwd: import.meta.dirname,
89+
},
90+
);
91+
92+
t.assert.snapshot(stdout);
93+
assert.equal(code, 0);
94+
});
6395
});

utils/src/logger.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ type LogType = 'error' | 'log' | 'warn';
55
type FileLog = { msg: LogMsg; type: LogType };
66
type Source = URL['pathname'];
77

8+
let codemodName = 'nodjs-codemod';
9+
10+
/**
11+
* Set the codemod name for logging output
12+
*/
13+
export const setCodemodName = (name: string) => {
14+
codemodName = name;
15+
};
16+
817
/**
918
* Collect log entries and report them at the end, collated by source module.
1019
*/
1120
export const logger = (source: Source, type: LogType, msg: LogMsg) => {
12-
const fileLog = new Set<FileLog>(logs.has(source) ? logs.get(source) : []);
21+
const fileLog = new Set<FileLog>(logs.has(source) ? logs.get(source) : []);
1322

14-
fileLog.add({ msg, type });
15-
logs.set(source, fileLog);
23+
fileLog.add({ msg, type });
24+
logs.set(source, fileLog);
1625
};
1726

1827
/**
@@ -23,21 +32,21 @@ const logs = new Map<Source, Set<FileLog>>();
2332
process.once('beforeExit', emitLogs);
2433

2534
function emitLogs() {
26-
let hasError = false;
27-
28-
for (const [sourceFile, fileLog] of logs.entries()) {
29-
console.log('[Codemod: correct-ts-specifiers]:', sourceFile);
30-
for (const { msg, type } of fileLog) {
31-
console[type](' •', msg);
32-
if (type === 'error') hasError = true;
33-
}
34-
}
35-
36-
if (hasError) {
37-
console.error('[Codemod: correct-ts-specifiers]: migration incomplete!');
38-
process.exitCode = 1;
39-
} else {
40-
process.exitCode = 0;
41-
console.log('[Codemod: correct-ts-specifiers]: migration complete!');
42-
}
35+
let hasError = false;
36+
37+
for (const [sourceFile, fileLog] of logs.entries()) {
38+
console.log(`[Codemod: ${codemodName}]:`, sourceFile);
39+
for (const { msg, type } of fileLog) {
40+
console[type](' •', msg);
41+
if (type === 'error') hasError = true;
42+
}
43+
}
44+
45+
if (hasError) {
46+
console.error(`[Codemod: ${codemodName}]: migration incomplete!`);
47+
process.exitCode = 1;
48+
} else {
49+
process.exitCode = 0;
50+
console.log(`[Codemod: ${codemodName}]: migration complete!`);
51+
}
4352
}

0 commit comments

Comments
 (0)