forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtransformer.mjs
More file actions
108 lines (91 loc) · 2.62 KB
/
transformer.mjs
File metadata and controls
108 lines (91 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import process from 'node:process';
import * as assert from 'node:assert';
import { parseArgs } from 'node:util';
import { parse, print, types } from 'recast';
import parser from 'recast/parsers/acorn.js';
const { values, positionals } = parseArgs({
allowPositionals: true,
options: {
root: {
type: 'string',
},
clean: {
type: 'boolean',
default: false,
},
module: {
type: 'string',
default: 'esm',
},
},
});
const files = positionals.map(file => path.join(values.root, file));
const INTERESTING_NODE_TYPES = new Set([
'ImportDeclaration',
'ExportNamedDeclaration',
'ExportAllDeclaration',
'ExportDefaultDeclaration',
]);
const JS_EXT = /\.js$/;
const TARGET_EXT = values.module === 'esm' ? '.mjs' : '.cjs';
await Promise.all(files.map(transform));
process.exit(0);
async function transform(file) {
const source = await fs.readFile(file, 'utf8');
const { code } = print(
rewrite(
source,
path.join(values.root, file),
parse(source, {
parser: {
parse(source) {
return parser.parse(source, {
ecmaVersion: 2022,
});
},
},
}),
),
);
if (values.clean) {
await fs.writeFile(file, code);
await fs.rename(file, file.replace(JS_EXT, TARGET_EXT));
} else {
await fs.writeFile(file.replace(JS_EXT, TARGET_EXT), code);
}
}
function rewrite(source, moduleSrc, tree) {
const mayHaveDynamicImport = values.module === 'esm' && /import\s*\(/.test(source);
for (const node of tree.program.body) {
if (!INTERESTING_NODE_TYPES.has(node.type)) continue;
if (node.source === null) continue;
remapSource(moduleSrc, node.source);
}
if (mayHaveDynamicImport || values.module === 'cjs') {
types.visit(tree.program.body, {
visitImportExpression(node) {
remapSource(moduleSrc, node.value.source);
return false;
},
visitCallExpression(node) {
if (node.value.callee.type === 'Identifier' && node.value.callee.name === 'require') {
remapSource(moduleSrc, node.value.arguments[0]);
}
return false;
},
});
}
return tree;
}
function remapSource(moduleSrc, source) {
assert.ok(source.type === 'Literal');
if (isInProjectScope(moduleSrc, source.value)) {
source.value = source.value.replace(JS_EXT, TARGET_EXT);
}
}
function isInProjectScope(moduleSrc, importSrc) {
const absolutePath = path.isAbsolute(importSrc) ? importSrc : path.join(moduleSrc, importSrc);
return absolutePath.startsWith(values.root);
}