nalloc 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +300 -180
- package/build/codemod-cli.cjs +153 -0
- package/build/codemod-cli.cjs.map +1 -0
- package/build/codemod-cli.d.ts +2 -0
- package/build/codemod-cli.js +103 -0
- package/build/codemod-cli.js.map +1 -0
- package/build/codemod.cjs +652 -0
- package/build/codemod.cjs.map +1 -0
- package/build/codemod.d.ts +29 -0
- package/build/codemod.js +634 -0
- package/build/codemod.js.map +1 -0
- package/build/eslint.cjs +221 -0
- package/build/eslint.cjs.map +1 -0
- package/build/eslint.d.ts +36 -0
- package/build/eslint.js +198 -0
- package/build/eslint.js.map +1 -0
- package/build/http.cjs +31 -0
- package/build/http.cjs.map +1 -0
- package/build/http.d.ts +31 -0
- package/build/http.js +13 -0
- package/build/http.js.map +1 -0
- package/build/result.cjs +0 -11
- package/build/result.cjs.map +1 -1
- package/build/result.d.ts +0 -32
- package/build/result.js +0 -8
- package/build/result.js.map +1 -1
- package/build/safe.cjs +4 -0
- package/build/safe.cjs.map +1 -1
- package/build/safe.d.ts +1 -0
- package/build/safe.js +1 -0
- package/build/safe.js.map +1 -1
- package/build/schema.cjs +32 -0
- package/build/schema.cjs.map +1 -0
- package/build/schema.d.ts +44 -0
- package/build/schema.js +14 -0
- package/build/schema.js.map +1 -0
- package/package.json +54 -5
- package/src/__tests__/codemod.ts +211 -0
- package/src/__tests__/eslint.ts +99 -0
- package/src/__tests__/fixtures/tsconfig.json +10 -0
- package/src/__tests__/http.ts +64 -0
- package/src/__tests__/result.ts +74 -125
- package/src/__tests__/schema.ts +58 -0
- package/src/codemod-cli.ts +108 -0
- package/src/codemod.ts +623 -0
- package/src/eslint.ts +145 -0
- package/src/http.ts +42 -0
- package/src/result.ts +0 -37
- package/src/safe.ts +1 -0
- package/src/schema.ts +52 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
const _nodefs = require("node:fs");
|
|
7
|
+
const _nodepath = require("node:path");
|
|
8
|
+
const _nodeprocess = /*#__PURE__*/ _interop_require_default(require("node:process"));
|
|
9
|
+
function _interop_require_default(obj) {
|
|
10
|
+
return obj && obj.__esModule ? obj : {
|
|
11
|
+
default: obj
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
15
|
+
if (typeof WeakMap !== "function") return null;
|
|
16
|
+
var cacheBabelInterop = new WeakMap();
|
|
17
|
+
var cacheNodeInterop = new WeakMap();
|
|
18
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
19
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
20
|
+
})(nodeInterop);
|
|
21
|
+
}
|
|
22
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
23
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
24
|
+
return obj;
|
|
25
|
+
}
|
|
26
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
27
|
+
return {
|
|
28
|
+
default: obj
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
32
|
+
if (cache && cache.has(obj)) {
|
|
33
|
+
return cache.get(obj);
|
|
34
|
+
}
|
|
35
|
+
var newObj = {
|
|
36
|
+
__proto__: null
|
|
37
|
+
};
|
|
38
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
39
|
+
for(var key in obj){
|
|
40
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
41
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
42
|
+
if (desc && (desc.get || desc.set)) {
|
|
43
|
+
Object.defineProperty(newObj, key, desc);
|
|
44
|
+
} else {
|
|
45
|
+
newObj[key] = obj[key];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
newObj.default = obj;
|
|
50
|
+
if (cache) {
|
|
51
|
+
cache.set(obj, newObj);
|
|
52
|
+
}
|
|
53
|
+
return newObj;
|
|
54
|
+
}
|
|
55
|
+
const HELP = `nalloc-codemod - migrate a project from neverthrow to nalloc
|
|
56
|
+
|
|
57
|
+
Usage:
|
|
58
|
+
nalloc-codemod <paths...> [--report <file>] [--dry]
|
|
59
|
+
|
|
60
|
+
Arguments:
|
|
61
|
+
paths Files or directories to migrate (.ts/.tsx/.mts/.cts, .d.ts and node_modules skipped)
|
|
62
|
+
|
|
63
|
+
Options:
|
|
64
|
+
--report <file> Write a markdown report of sites needing manual review
|
|
65
|
+
--dry Analyze and report without writing any files
|
|
66
|
+
--help Show this help
|
|
67
|
+
|
|
68
|
+
Migrate the whole project in one run: converted call sites assume their Result
|
|
69
|
+
values are nalloc values, which only holds once constructors are converted too.
|
|
70
|
+
`;
|
|
71
|
+
const SOURCE_EXT = /\.(ts|tsx|mts|cts)$/;
|
|
72
|
+
function collectFiles(path, files) {
|
|
73
|
+
const stats = (0, _nodefs.statSync)(path);
|
|
74
|
+
if (stats.isDirectory()) {
|
|
75
|
+
for (const entry of (0, _nodefs.readdirSync)(path)){
|
|
76
|
+
if (entry === 'node_modules' || entry.startsWith('.')) continue;
|
|
77
|
+
collectFiles((0, _nodepath.join)(path, entry), files);
|
|
78
|
+
}
|
|
79
|
+
} else if (SOURCE_EXT.test(path) && !path.endsWith('.d.ts')) {
|
|
80
|
+
files.push(path);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const paths = [];
|
|
84
|
+
let reportPath;
|
|
85
|
+
let dry = false;
|
|
86
|
+
const args = _nodeprocess.default.argv.slice(2);
|
|
87
|
+
for(let i = 0; i < args.length; i++){
|
|
88
|
+
const arg = args[i];
|
|
89
|
+
if (arg === '--help') {
|
|
90
|
+
console.log(HELP);
|
|
91
|
+
_nodeprocess.default.exit(0);
|
|
92
|
+
} else if (arg === '--dry') {
|
|
93
|
+
dry = true;
|
|
94
|
+
} else if (arg === '--report') {
|
|
95
|
+
reportPath = args[++i];
|
|
96
|
+
if (reportPath === undefined) {
|
|
97
|
+
console.error('--report requires a file path');
|
|
98
|
+
_nodeprocess.default.exit(1);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
paths.push(arg);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (paths.length === 0) {
|
|
105
|
+
console.log(HELP);
|
|
106
|
+
_nodeprocess.default.exit(1);
|
|
107
|
+
}
|
|
108
|
+
const codemod = await (async ()=>{
|
|
109
|
+
try {
|
|
110
|
+
return await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("./codemod.js")));
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error.code === 'ERR_MODULE_NOT_FOUND' && String(error).includes('oxc-parser')) {
|
|
113
|
+
console.error('nalloc-codemod requires the optional peer oxc-parser. Install it first:\n\n npm install -D oxc-parser\n');
|
|
114
|
+
_nodeprocess.default.exit(1);
|
|
115
|
+
}
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
})();
|
|
119
|
+
const files = [];
|
|
120
|
+
for (const path of paths){
|
|
121
|
+
collectFiles(path, files);
|
|
122
|
+
}
|
|
123
|
+
let filesChanged = 0;
|
|
124
|
+
let converted = 0;
|
|
125
|
+
const skipped = [];
|
|
126
|
+
for (const file of files){
|
|
127
|
+
const source = (0, _nodefs.readFileSync)(file, 'utf8');
|
|
128
|
+
const result = codemod.migrateSource(source, file);
|
|
129
|
+
converted += result.converted;
|
|
130
|
+
skipped.push(...result.skipped);
|
|
131
|
+
if (result.changed) {
|
|
132
|
+
filesChanged++;
|
|
133
|
+
if (!dry) {
|
|
134
|
+
(0, _nodefs.writeFileSync)(file, result.output);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const report = {
|
|
139
|
+
filesChanged,
|
|
140
|
+
converted,
|
|
141
|
+
skipped
|
|
142
|
+
};
|
|
143
|
+
if (reportPath !== undefined) {
|
|
144
|
+
(0, _nodefs.writeFileSync)(reportPath, codemod.renderReport(report));
|
|
145
|
+
}
|
|
146
|
+
console.log(`${dry ? '[dry run] ' : ''}${files.length} files scanned, ${filesChanged} changed, ${converted} sites converted, ${skipped.length} need manual review${reportPath !== undefined ? ` (see ${reportPath})` : ''}`);
|
|
147
|
+
if (skipped.length > 0 && reportPath === undefined) {
|
|
148
|
+
for (const site of skipped){
|
|
149
|
+
console.log(` ${site.file}:${site.line} [${site.reason}] ${site.text}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//# sourceMappingURL=codemod-cli.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/codemod-cli.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';\nimport { join } from 'node:path';\nimport process from 'node:process';\n\nconst HELP = `nalloc-codemod - migrate a project from neverthrow to nalloc\n\nUsage:\n nalloc-codemod <paths...> [--report <file>] [--dry]\n\nArguments:\n paths Files or directories to migrate (.ts/.tsx/.mts/.cts, .d.ts and node_modules skipped)\n\nOptions:\n --report <file> Write a markdown report of sites needing manual review\n --dry Analyze and report without writing any files\n --help Show this help\n\nMigrate the whole project in one run: converted call sites assume their Result\nvalues are nalloc values, which only holds once constructors are converted too.\n`;\n\nconst SOURCE_EXT = /\\.(ts|tsx|mts|cts)$/;\n\nfunction collectFiles(path: string, files: string[]): void {\n const stats = statSync(path);\n if (stats.isDirectory()) {\n for (const entry of readdirSync(path)) {\n if (entry === 'node_modules' || entry.startsWith('.')) continue;\n collectFiles(join(path, entry), files);\n }\n } else if (SOURCE_EXT.test(path) && !path.endsWith('.d.ts')) {\n files.push(path);\n }\n}\n\nconst paths: string[] = [];\nlet reportPath: string | undefined;\nlet dry = false;\nconst args = process.argv.slice(2);\nfor (let i = 0; i < args.length; i++) {\n const arg = args[i];\n if (arg === '--help') {\n console.log(HELP);\n process.exit(0);\n } else if (arg === '--dry') {\n dry = true;\n } else if (arg === '--report') {\n reportPath = args[++i];\n if (reportPath === undefined) {\n console.error('--report requires a file path');\n process.exit(1);\n }\n } else {\n paths.push(arg);\n }\n}\n\nif (paths.length === 0) {\n console.log(HELP);\n process.exit(1);\n}\n\nconst codemod = await (async () => {\n try {\n return await import('./codemod.js');\n } catch (error) {\n if ((error as { code?: string }).code === 'ERR_MODULE_NOT_FOUND' && String(error).includes('oxc-parser')) {\n console.error('nalloc-codemod requires the optional peer oxc-parser. Install it first:\\n\\n npm install -D oxc-parser\\n');\n process.exit(1);\n }\n throw error;\n }\n})();\n\nconst files: string[] = [];\nfor (const path of paths) {\n collectFiles(path, files);\n}\n\nlet filesChanged = 0;\nlet converted = 0;\nconst skipped = [];\nfor (const file of files) {\n const source = readFileSync(file, 'utf8');\n const result = codemod.migrateSource(source, file);\n converted += result.converted;\n skipped.push(...result.skipped);\n if (result.changed) {\n filesChanged++;\n if (!dry) {\n writeFileSync(file, result.output);\n }\n }\n}\n\nconst report = { filesChanged, converted, skipped };\nif (reportPath !== undefined) {\n writeFileSync(reportPath, codemod.renderReport(report));\n}\nconsole.log(\n `${dry ? '[dry run] ' : ''}${files.length} files scanned, ${filesChanged} changed, ${converted} sites converted, ${skipped.length} need manual review${reportPath !== undefined ? ` (see ${reportPath})` : ''}`,\n);\nif (skipped.length > 0 && reportPath === undefined) {\n for (const site of skipped) {\n console.log(` ${site.file}:${site.line} [${site.reason}] ${site.text}`);\n }\n}\n"],"names":["HELP","SOURCE_EXT","collectFiles","path","files","stats","statSync","isDirectory","entry","readdirSync","startsWith","join","test","endsWith","push","paths","reportPath","dry","args","process","argv","slice","i","length","arg","console","log","exit","undefined","error","codemod","code","String","includes","filesChanged","converted","skipped","file","source","readFileSync","result","migrateSource","changed","writeFileSync","output","report","renderReport","site","line","reason","text"],"mappings":";;;;;wBACmE;0BAC9C;oEACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEpB,MAAMA,OAAO,CAAC;;;;;;;;;;;;;;;AAed,CAAC;AAED,MAAMC,aAAa;AAEnB,SAASC,aAAaC,IAAY,EAAEC,KAAe;IACjD,MAAMC,QAAQC,IAAAA,gBAAQ,EAACH;IACvB,IAAIE,MAAME,WAAW,IAAI;QACvB,KAAK,MAAMC,SAASC,IAAAA,mBAAW,EAACN,MAAO;YACrC,IAAIK,UAAU,kBAAkBA,MAAME,UAAU,CAAC,MAAM;YACvDR,aAAaS,IAAAA,cAAI,EAACR,MAAMK,QAAQJ;QAClC;IACF,OAAO,IAAIH,WAAWW,IAAI,CAACT,SAAS,CAACA,KAAKU,QAAQ,CAAC,UAAU;QAC3DT,MAAMU,IAAI,CAACX;IACb;AACF;AAEA,MAAMY,QAAkB,EAAE;AAC1B,IAAIC;AACJ,IAAIC,MAAM;AACV,MAAMC,OAAOC,oBAAO,CAACC,IAAI,CAACC,KAAK,CAAC;AAChC,IAAK,IAAIC,IAAI,GAAGA,IAAIJ,KAAKK,MAAM,EAAED,IAAK;IACpC,MAAME,MAAMN,IAAI,CAACI,EAAE;IACnB,IAAIE,QAAQ,UAAU;QACpBC,QAAQC,GAAG,CAAC1B;QACZmB,oBAAO,CAACQ,IAAI,CAAC;IACf,OAAO,IAAIH,QAAQ,SAAS;QAC1BP,MAAM;IACR,OAAO,IAAIO,QAAQ,YAAY;QAC7BR,aAAaE,IAAI,CAAC,EAAEI,EAAE;QACtB,IAAIN,eAAeY,WAAW;YAC5BH,QAAQI,KAAK,CAAC;YACdV,oBAAO,CAACQ,IAAI,CAAC;QACf;IACF,OAAO;QACLZ,MAAMD,IAAI,CAACU;IACb;AACF;AAEA,IAAIT,MAAMQ,MAAM,KAAK,GAAG;IACtBE,QAAQC,GAAG,CAAC1B;IACZmB,oBAA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X,QAAS;QAC1BX,QAAQC,GAAG,CAAC,CAAC,EAAE,EAAEqB,KAAKV,IAAI,CAAC,CAAC,EAAEU,KAAKC,IAAI,CAAC,EAAE,EAAED,KAAKE,MAAM,CAAC,EAAE,EAAEF,KAAKG,IAAI,EAAE;IACzE;AACF"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
const HELP = `nalloc-codemod - migrate a project from neverthrow to nalloc
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
nalloc-codemod <paths...> [--report <file>] [--dry]
|
|
9
|
+
|
|
10
|
+
Arguments:
|
|
11
|
+
paths Files or directories to migrate (.ts/.tsx/.mts/.cts, .d.ts and node_modules skipped)
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
--report <file> Write a markdown report of sites needing manual review
|
|
15
|
+
--dry Analyze and report without writing any files
|
|
16
|
+
--help Show this help
|
|
17
|
+
|
|
18
|
+
Migrate the whole project in one run: converted call sites assume their Result
|
|
19
|
+
values are nalloc values, which only holds once constructors are converted too.
|
|
20
|
+
`;
|
|
21
|
+
const SOURCE_EXT = /\.(ts|tsx|mts|cts)$/;
|
|
22
|
+
function collectFiles(path, files) {
|
|
23
|
+
const stats = statSync(path);
|
|
24
|
+
if (stats.isDirectory()) {
|
|
25
|
+
for (const entry of readdirSync(path)){
|
|
26
|
+
if (entry === 'node_modules' || entry.startsWith('.')) continue;
|
|
27
|
+
collectFiles(join(path, entry), files);
|
|
28
|
+
}
|
|
29
|
+
} else if (SOURCE_EXT.test(path) && !path.endsWith('.d.ts')) {
|
|
30
|
+
files.push(path);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const paths = [];
|
|
34
|
+
let reportPath;
|
|
35
|
+
let dry = false;
|
|
36
|
+
const args = process.argv.slice(2);
|
|
37
|
+
for(let i = 0; i < args.length; i++){
|
|
38
|
+
const arg = args[i];
|
|
39
|
+
if (arg === '--help') {
|
|
40
|
+
console.log(HELP);
|
|
41
|
+
process.exit(0);
|
|
42
|
+
} else if (arg === '--dry') {
|
|
43
|
+
dry = true;
|
|
44
|
+
} else if (arg === '--report') {
|
|
45
|
+
reportPath = args[++i];
|
|
46
|
+
if (reportPath === undefined) {
|
|
47
|
+
console.error('--report requires a file path');
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
paths.push(arg);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (paths.length === 0) {
|
|
55
|
+
console.log(HELP);
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
const codemod = await (async ()=>{
|
|
59
|
+
try {
|
|
60
|
+
return await import('./codemod.js');
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error.code === 'ERR_MODULE_NOT_FOUND' && String(error).includes('oxc-parser')) {
|
|
63
|
+
console.error('nalloc-codemod requires the optional peer oxc-parser. Install it first:\n\n npm install -D oxc-parser\n');
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
})();
|
|
69
|
+
const files = [];
|
|
70
|
+
for (const path of paths){
|
|
71
|
+
collectFiles(path, files);
|
|
72
|
+
}
|
|
73
|
+
let filesChanged = 0;
|
|
74
|
+
let converted = 0;
|
|
75
|
+
const skipped = [];
|
|
76
|
+
for (const file of files){
|
|
77
|
+
const source = readFileSync(file, 'utf8');
|
|
78
|
+
const result = codemod.migrateSource(source, file);
|
|
79
|
+
converted += result.converted;
|
|
80
|
+
skipped.push(...result.skipped);
|
|
81
|
+
if (result.changed) {
|
|
82
|
+
filesChanged++;
|
|
83
|
+
if (!dry) {
|
|
84
|
+
writeFileSync(file, result.output);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const report = {
|
|
89
|
+
filesChanged,
|
|
90
|
+
converted,
|
|
91
|
+
skipped
|
|
92
|
+
};
|
|
93
|
+
if (reportPath !== undefined) {
|
|
94
|
+
writeFileSync(reportPath, codemod.renderReport(report));
|
|
95
|
+
}
|
|
96
|
+
console.log(`${dry ? '[dry run] ' : ''}${files.length} files scanned, ${filesChanged} changed, ${converted} sites converted, ${skipped.length} need manual review${reportPath !== undefined ? ` (see ${reportPath})` : ''}`);
|
|
97
|
+
if (skipped.length > 0 && reportPath === undefined) {
|
|
98
|
+
for (const site of skipped){
|
|
99
|
+
console.log(` ${site.file}:${site.line} [${site.reason}] ${site.text}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
//# sourceMappingURL=codemod-cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/codemod-cli.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';\nimport { join } from 'node:path';\nimport process from 'node:process';\n\nconst HELP = `nalloc-codemod - migrate a project from neverthrow to nalloc\n\nUsage:\n nalloc-codemod <paths...> [--report <file>] [--dry]\n\nArguments:\n paths Files or directories to migrate (.ts/.tsx/.mts/.cts, .d.ts and node_modules skipped)\n\nOptions:\n --report <file> Write a markdown report of sites needing manual review\n --dry Analyze and report without writing any files\n --help Show this help\n\nMigrate the whole project in one run: converted call sites assume their Result\nvalues are nalloc values, which only holds once constructors are converted too.\n`;\n\nconst SOURCE_EXT = /\\.(ts|tsx|mts|cts)$/;\n\nfunction collectFiles(path: string, files: string[]): void {\n const stats = statSync(path);\n if (stats.isDirectory()) {\n for (const entry of readdirSync(path)) {\n if (entry === 'node_modules' || entry.startsWith('.')) continue;\n collectFiles(join(path, entry), files);\n }\n } else if (SOURCE_EXT.test(path) && !path.endsWith('.d.ts')) {\n files.push(path);\n }\n}\n\nconst paths: string[] = [];\nlet reportPath: string | undefined;\nlet dry = false;\nconst args = process.argv.slice(2);\nfor (let i = 0; i < args.length; i++) {\n const arg = args[i];\n if (arg === '--help') {\n console.log(HELP);\n process.exit(0);\n } else if (arg === '--dry') {\n dry = true;\n } else if (arg === '--report') {\n reportPath = args[++i];\n if (reportPath === undefined) {\n console.error('--report requires a file path');\n process.exit(1);\n }\n } else {\n paths.push(arg);\n }\n}\n\nif (paths.length === 0) {\n console.log(HELP);\n process.exit(1);\n}\n\nconst codemod = await (async () => {\n try {\n return await import('./codemod.js');\n } catch (error) {\n if ((error as { code?: string }).code === 'ERR_MODULE_NOT_FOUND' && String(error).includes('oxc-parser')) {\n console.error('nalloc-codemod requires the optional peer oxc-parser. Install it first:\\n\\n npm install -D oxc-parser\\n');\n process.exit(1);\n }\n throw error;\n }\n})();\n\nconst files: string[] = [];\nfor (const path of paths) {\n collectFiles(path, files);\n}\n\nlet filesChanged = 0;\nlet converted = 0;\nconst skipped = [];\nfor (const file of files) {\n const source = readFileSync(file, 'utf8');\n const result = codemod.migrateSource(source, file);\n converted += result.converted;\n skipped.push(...result.skipped);\n if (result.changed) {\n filesChanged++;\n if (!dry) {\n writeFileSync(file, result.output);\n }\n }\n}\n\nconst report = { filesChanged, converted, skipped };\nif (reportPath !== undefined) {\n writeFileSync(reportPath, codemod.renderReport(report));\n}\nconsole.log(\n `${dry ? '[dry run] ' : ''}${files.length} files scanned, ${filesChanged} changed, ${converted} sites converted, ${skipped.length} need manual review${reportPath !== undefined ? ` (see ${reportPath})` : ''}`,\n);\nif (skipped.length > 0 && reportPath === undefined) {\n for (const site of skipped) {\n console.log(` ${site.file}:${site.line} [${site.reason}] ${site.text}`);\n }\n}\n"],"names":["readFileSync","writeFileSync","readdirSync","statSync","join","process","HELP","SOURCE_EXT","collectFiles","path","files","stats","isDirectory","entry","startsWith","test","endsWith","push","paths","reportPath","dry","args","argv","slice","i","length","arg","console","log","exit","undefined","error","codemod","code","String","includes","filesChanged","converted","skipped","file","source","result","migrateSource","changed","output","report","renderReport","site","line","reason","text"],"mappings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