Skip to content

Commit 07965c3

Browse files
WIP
1 parent 855dd86 commit 07965c3

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

recipes/cjs-to-esm/codemod.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ keywords:
2020
registry:
2121
access: public
2222
visibility: public
23+
24+
capabilities:
25+
- fs
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import {
4+
getOrSetStepOutput,
5+
setStepOutput,
6+
} from '@codemod.com/jssg-types/workflow';
7+
import type { SgRoot } from '@codemod.com/jssg-types/main';
8+
import type JS from '@codemod.com/jssg-types/langs/javascript';
9+
10+
const STEP_ID = 'change-extensions';
11+
const OUTPUT_NAME = 'extension_changes';
12+
13+
export default async function transform(
14+
root: SgRoot<JS>,
15+
): Promise<string | null> {
16+
const sourcePath = root.filename();
17+
if (!sourcePath || sourcePath === 'anonymous') return null;
18+
19+
if (!sourcePath.endsWith('.cjs') && !sourcePath.endsWith('.mjs')) return null;
20+
21+
const targetPath = sourcePath.replace(/\.(c|m)js$/i, '.js');
22+
if (targetPath === sourcePath) return null;
23+
24+
fs.renameSync(sourcePath, targetPath);
25+
26+
const from = path.normalize(path.resolve(sourcePath));
27+
const to = path.normalize(path.resolve(targetPath));
28+
29+
const existingRaw = getOrSetStepOutput(STEP_ID, OUTPUT_NAME, '[]');
30+
let mappings: Array<{ from: string; to: string }> = [];
31+
32+
try {
33+
const parsed = JSON.parse(existingRaw);
34+
if (Array.isArray(parsed)) mappings = parsed;
35+
} catch (error) {
36+
console.warn(
37+
'Failed to parse existing extension changes; resetting list',
38+
error,
39+
);
40+
}
41+
42+
mappings.push({ from, to });
43+
setStepOutput(OUTPUT_NAME, JSON.stringify(mappings));
44+
45+
return null;
46+
}

recipes/cjs-to-esm/workflow.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
version: "1"
22

3+
state:
4+
schema:
5+
extension_changes:
6+
description: Tracks files renamed from .cjs/.mjs to .js for downstream specifier fixes
7+
type: array
8+
items:
9+
type: object
10+
properties:
11+
from:
12+
type: string
13+
to:
14+
type: string
15+
316
nodes:
17+
- id: normalize-extensions
18+
name: Normalize file extensions
19+
type: automatic
20+
runtime:
21+
type: direct
22+
steps:
23+
- id: change-extensions
24+
name: Rename .cjs/.mjs files to .js and record mapping
25+
js-ast-grep:
26+
js_file: src/extension-change.ts
27+
base_path: .
28+
include:
29+
- "**/*.cjs"
30+
- "**/*.mjs"
31+
exclude:
32+
- "**/node_modules/**"
33+
language: typescript
34+
435
- id: apply-transforms
536
name: Apply AST transformations
37+
depends_on:
38+
- normalize-extensions
639
type: automatic
740
runtime:
841
type: direct

0 commit comments

Comments
 (0)