@beinformed/codemod 0.1.1-beta.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/LICENSE.md +1 -0
- package/dist/chunk-VJQ72WQS.js +11 -0
- package/dist/chunk-VJQ72WQS.js.map +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +53 -0
- package/dist/cli.js.map +1 -0
- package/dist/generate_mapping.js +208 -0
- package/dist/mapping.json +691 -0
- package/dist/transforms/migrate-imports.d.ts +26 -0
- package/dist/transforms/migrate-imports.js +189 -0
- package/dist/transforms/migrate-imports.js.map +1 -0
- package/package.json +39 -0
- package/usage.md +43 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright 2021 Be Informed B.V. - All rights reserved.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// ../../node_modules/.pnpm/tsup@8.5.1_postcss@8.5.6_typescript@5.9.3_yaml@2.8.0/node_modules/tsup/assets/esm_shims.js
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
5
|
+
var getDirname = () => path.dirname(getFilename());
|
|
6
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
__dirname
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=chunk-VJQ72WQS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../node_modules/.pnpm/tsup@8.5.1_postcss@8.5.6_typescript@5.9.3_yaml@2.8.0/node_modules/tsup/assets/esm_shims.js"],"sourcesContent":["// Shim globals in esm bundle\nimport path from 'node:path'\nimport { fileURLToPath } from 'node:url'\n\nconst getFilename = () => fileURLToPath(import.meta.url)\nconst getDirname = () => path.dirname(getFilename())\n\nexport const __dirname = /* @__PURE__ */ getDirname()\nexport const __filename = /* @__PURE__ */ getFilename()\n"],"mappings":";AACA,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAE9B,IAAM,cAAc,MAAM,cAAc,YAAY,GAAG;AACvD,IAAM,aAAa,MAAM,KAAK,QAAQ,YAAY,CAAC;AAE5C,IAAM,YAA4B,2BAAW;","names":[]}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import "./chunk-VJQ72WQS.js";
|
|
3
|
+
|
|
4
|
+
// src/cli.ts
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import { fileURLToPath } from "url";
|
|
8
|
+
import { run } from "jscodeshift/src/Runner";
|
|
9
|
+
import yargs from "yargs";
|
|
10
|
+
import { hideBin } from "yargs/helpers";
|
|
11
|
+
var __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
var __dirname = path.dirname(__filename);
|
|
13
|
+
async function main() {
|
|
14
|
+
const argv = await yargs(hideBin(process.argv)).usage("Usage: $0 [options] <path>").option("dry", {
|
|
15
|
+
alias: "d",
|
|
16
|
+
type: "boolean",
|
|
17
|
+
description: "Dry run (no changes made)",
|
|
18
|
+
default: false
|
|
19
|
+
}).option("print", {
|
|
20
|
+
alias: "p",
|
|
21
|
+
type: "boolean",
|
|
22
|
+
description: "Print transformed files",
|
|
23
|
+
default: false
|
|
24
|
+
}).help().alias("h", "help").parse();
|
|
25
|
+
const paths = argv._;
|
|
26
|
+
if (paths.length === 0) {
|
|
27
|
+
console.error("Please provide a path to run the codemod on.");
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
const transformPath = path.resolve(
|
|
31
|
+
__dirname,
|
|
32
|
+
"./transforms/migrate-imports.js"
|
|
33
|
+
);
|
|
34
|
+
if (!fs.existsSync(transformPath)) {
|
|
35
|
+
console.error(`Transform not found at ${transformPath}`);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
const options = {
|
|
39
|
+
dry: argv.dry,
|
|
40
|
+
print: argv.print,
|
|
41
|
+
babel: true,
|
|
42
|
+
extensions: "js,ts,jsx,tsx",
|
|
43
|
+
ignorePattern: "**/node_modules/**"
|
|
44
|
+
};
|
|
45
|
+
try {
|
|
46
|
+
await run(transformPath, paths, options);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error("Codemod failed:", error);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
await main();
|
|
53
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env node\nimport fs from \"node:fs\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\nimport { run } from \"jscodeshift/src/Runner\";\nimport yargs from \"yargs\";\nimport { hideBin } from \"yargs/helpers\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nasync function main() {\n const argv = await yargs(hideBin(process.argv))\n .usage(\"Usage: $0 [options] <path>\")\n .option(\"dry\", {\n alias: \"d\",\n type: \"boolean\",\n description: \"Dry run (no changes made)\",\n default: false,\n })\n .option(\"print\", {\n alias: \"p\",\n type: \"boolean\",\n description: \"Print transformed files\",\n default: false,\n })\n .help()\n .alias(\"h\", \"help\")\n .parse();\n\n const paths = argv._ as string[];\n if (paths.length === 0) {\n console.error(\"Please provide a path to run the codemod on.\");\n process.exit(1);\n }\n\n const transformPath = path.resolve(\n __dirname,\n \"./transforms/migrate-imports.js\",\n );\n if (!fs.existsSync(transformPath)) {\n // Fallback for different build structures if necessary\n console.error(`Transform not found at ${transformPath}`);\n process.exit(1);\n }\n\n const options = {\n dry: argv.dry,\n print: argv.print,\n babel: true,\n extensions: \"js,ts,jsx,tsx\",\n ignorePattern: \"**/node_modules/**\",\n };\n\n try {\n await run(transformPath, paths, options);\n } catch (error) {\n console.error(\"Codemod failed:\", error);\n process.exit(1);\n }\n}\n\nawait main();\n"],"mappings":";;;;AACA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAE9B,SAAS,WAAW;AACpB,OAAO,WAAW;AAClB,SAAS,eAAe;AAExB,IAAM,aAAa,cAAc,YAAY,GAAG;AAChD,IAAM,YAAY,KAAK,QAAQ,UAAU;AAEzC,eAAe,OAAO;AACpB,QAAM,OAAO,MAAM,MAAM,QAAQ,QAAQ,IAAI,CAAC,EAC3C,MAAM,4BAA4B,EAClC,OAAO,OAAO;AAAA,IACb,OAAO;AAAA,IACP,MAAM;AAAA,IACN,aAAa;AAAA,IACb,SAAS;AAAA,EACX,CAAC,EACA,OAAO,SAAS;AAAA,IACf,OAAO;AAAA,IACP,MAAM;AAAA,IACN,aAAa;AAAA,IACb,SAAS;AAAA,EACX,CAAC,EACA,KAAK,EACL,MAAM,KAAK,MAAM,EACjB,MAAM;AAET,QAAM,QAAQ,KAAK;AACnB,MAAI,MAAM,WAAW,GAAG;AACtB,YAAQ,MAAM,8CAA8C;AAC5D,YAAQ,KAAK,CAAC;AAAA,EAChB;AAEA,QAAM,gBAAgB,KAAK;AAAA,IACzB;AAAA,IACA;AAAA,EACF;AACA,MAAI,CAAC,GAAG,WAAW,aAAa,GAAG;AAEjC,YAAQ,MAAM,0BAA0B,aAAa,EAAE;AACvD,YAAQ,KAAK,CAAC;AAAA,EAChB;AAEA,QAAM,UAAU;AAAA,IACd,KAAK,KAAK;AAAA,IACV,OAAO,KAAK;AAAA,IACZ,OAAO;AAAA,IACP,YAAY;AAAA,IACZ,eAAe;AAAA,EACjB;AAEA,MAAI;AACF,UAAM,IAAI,eAAe,OAAO,OAAO;AAAA,EACzC,SAAS,OAAO;AACd,YAAQ,MAAM,mBAAmB,KAAK;AACtC,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;AAEA,MAAM,KAAK;","names":[]}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
const projectRoot = process.cwd();
|
|
5
|
+
const mappingFile = path.join(
|
|
6
|
+
projectRoot,
|
|
7
|
+
"packages",
|
|
8
|
+
"codemod",
|
|
9
|
+
"src",
|
|
10
|
+
"mapping.json",
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const packageDirs = [
|
|
14
|
+
{ name: "core", prefix: "@beinformed/ui" },
|
|
15
|
+
{ name: "react", prefix: "@beinformed/react" },
|
|
16
|
+
{ name: "builder", prefix: "@beinformed/builder" },
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const mapping = {};
|
|
20
|
+
|
|
21
|
+
function getExports(filePath) {
|
|
22
|
+
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory())
|
|
23
|
+
return [];
|
|
24
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
25
|
+
const exports = [];
|
|
26
|
+
|
|
27
|
+
// Named exports: export const/function/class/type/interface/enum Name
|
|
28
|
+
const namedExportRegex =
|
|
29
|
+
/export\s+(?:abstract\s+)?(?:const|let|var|function|class|type|interface|enum)\s+([a-zA-Z0-9_]+)/g;
|
|
30
|
+
let match;
|
|
31
|
+
while ((match = namedExportRegex.exec(content)) !== null) {
|
|
32
|
+
if (
|
|
33
|
+
![
|
|
34
|
+
"const",
|
|
35
|
+
"let",
|
|
36
|
+
"var",
|
|
37
|
+
"function",
|
|
38
|
+
"class",
|
|
39
|
+
"type",
|
|
40
|
+
"interface",
|
|
41
|
+
"enum",
|
|
42
|
+
"abstract",
|
|
43
|
+
].includes(match[1])
|
|
44
|
+
) {
|
|
45
|
+
exports.push(match[1]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// export { name, other as alias }
|
|
50
|
+
const curlyExportRegex =
|
|
51
|
+
/export\s+\{([^}]+)\}(?:\s+from\s+['"]([^'"]+)['"])?/g;
|
|
52
|
+
while ((match = curlyExportRegex.exec(content)) !== null) {
|
|
53
|
+
const items = match[1].split(",").map((item) => {
|
|
54
|
+
const parts = item.trim().split(/\s+as\s+/);
|
|
55
|
+
return parts[parts.length - 1]; // Return the name or the alias
|
|
56
|
+
});
|
|
57
|
+
exports.push(...items);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Default exports
|
|
61
|
+
const defaultExportRegex =
|
|
62
|
+
/export\s+default\s+(?:function|class)?\s*([a-zA-Z0-9_]+)/g;
|
|
63
|
+
while ((match = defaultExportRegex.exec(content)) !== null) {
|
|
64
|
+
if (!["function", "class"].includes(match[1])) {
|
|
65
|
+
exports.push(match[1]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return exports.filter((e) => e && e !== "default" && e !== "type");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function getReExports(filePath) {
|
|
73
|
+
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory())
|
|
74
|
+
return [];
|
|
75
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
76
|
+
const reExports = [];
|
|
77
|
+
|
|
78
|
+
// export * from './path'
|
|
79
|
+
const exportStarRegex = /export\s+\*\s+from\s+['"]([^'"]+)['"]/g;
|
|
80
|
+
let match;
|
|
81
|
+
while ((match = exportStarRegex.exec(content)) !== null) {
|
|
82
|
+
reExports.push(match[1]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// export type * from './path'
|
|
86
|
+
const exportStarTypeRegex = /export\s+type\s+\*\s+from\s+['"]([^'"]+)['"]/g;
|
|
87
|
+
while ((match = exportStarTypeRegex.exec(content)) !== null) {
|
|
88
|
+
reExports.push(match[1]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return reExports;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const processedFiles = new Set();
|
|
95
|
+
|
|
96
|
+
function processFile(filePath, packagePrefix, currentPackagePath) {
|
|
97
|
+
let resolvedPath = filePath;
|
|
98
|
+
const exts = [
|
|
99
|
+
"",
|
|
100
|
+
".ts",
|
|
101
|
+
".tsx",
|
|
102
|
+
".d.ts",
|
|
103
|
+
"/index.ts",
|
|
104
|
+
"/index.tsx",
|
|
105
|
+
"/index.d.ts",
|
|
106
|
+
];
|
|
107
|
+
let found = false;
|
|
108
|
+
for (const ext of exts) {
|
|
109
|
+
const altPath = filePath + ext;
|
|
110
|
+
if (fs.existsSync(altPath) && !fs.statSync(altPath).isDirectory()) {
|
|
111
|
+
resolvedPath = altPath;
|
|
112
|
+
found = true;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!found) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
resolvedPath = fs.realpathSync(resolvedPath);
|
|
122
|
+
if (processedFiles.has(resolvedPath)) return;
|
|
123
|
+
processedFiles.add(resolvedPath);
|
|
124
|
+
|
|
125
|
+
const exports = getExports(resolvedPath);
|
|
126
|
+
exports.forEach((exp) => {
|
|
127
|
+
mapping[exp] = currentPackagePath;
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const reExports = getReExports(resolvedPath);
|
|
131
|
+
reExports.forEach((rePath) => {
|
|
132
|
+
const fullPath = path.resolve(path.dirname(resolvedPath), rePath);
|
|
133
|
+
processFile(fullPath, packagePrefix, currentPackagePath);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
packageDirs.forEach((pkg) => {
|
|
138
|
+
const pkgPath = path.join(projectRoot, "packages", pkg.name);
|
|
139
|
+
const packageJsonPath = path.join(pkgPath, "package.json");
|
|
140
|
+
if (!fs.existsSync(packageJsonPath)) return;
|
|
141
|
+
|
|
142
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
|
|
143
|
+
const packageExports = packageJson.exports;
|
|
144
|
+
|
|
145
|
+
if (packageExports) {
|
|
146
|
+
Object.entries(packageExports).forEach(([entry, paths]) => {
|
|
147
|
+
if (entry === "./package.json") return;
|
|
148
|
+
|
|
149
|
+
let relativeEntryPoint;
|
|
150
|
+
if (entry === ".") {
|
|
151
|
+
relativeEntryPoint = "./src/index.ts";
|
|
152
|
+
} else {
|
|
153
|
+
// Try several common patterns for entry points
|
|
154
|
+
const subPath = entry.replace("./", "");
|
|
155
|
+
const possiblePaths = [
|
|
156
|
+
`./src/${subPath}/index.ts`,
|
|
157
|
+
`./src/${subPath}/index.tsx`,
|
|
158
|
+
`./src/${subPath}.ts`,
|
|
159
|
+
`./src/${subPath}.tsx`,
|
|
160
|
+
];
|
|
161
|
+
for (const p of possiblePaths) {
|
|
162
|
+
if (fs.existsSync(path.join(pkgPath, p))) {
|
|
163
|
+
relativeEntryPoint = p;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!relativeEntryPoint) return;
|
|
170
|
+
|
|
171
|
+
const fullEntryPoint = path.join(pkgPath, relativeEntryPoint);
|
|
172
|
+
|
|
173
|
+
let currentPackagePath =
|
|
174
|
+
entry === "." ? pkg.prefix : `${pkg.prefix}/${entry.replace("./", "")}`;
|
|
175
|
+
|
|
176
|
+
// Limit path depth to 3 segments
|
|
177
|
+
const segments = currentPackagePath.split("/");
|
|
178
|
+
if (segments.length > 3) {
|
|
179
|
+
currentPackagePath = segments.slice(0, 3).join("/");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
processFile(fullEntryPoint, pkg.prefix, currentPackagePath);
|
|
183
|
+
});
|
|
184
|
+
} else {
|
|
185
|
+
// Fallback to src/index.ts if no exports defined
|
|
186
|
+
const fullEntryPoint = path.join(pkgPath, "src", "index.ts");
|
|
187
|
+
if (fs.existsSync(fullEntryPoint)) {
|
|
188
|
+
processFile(fullEntryPoint, pkg.prefix, pkg.prefix);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Manual overrides
|
|
194
|
+
if (mapping["getSetting"]) {
|
|
195
|
+
mapping["getSetting"] = "@beinformed/ui/settings";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const sortedMapping = Object.keys(mapping)
|
|
199
|
+
.sort()
|
|
200
|
+
.reduce((acc, key) => {
|
|
201
|
+
acc[key] = mapping[key];
|
|
202
|
+
return acc;
|
|
203
|
+
}, {});
|
|
204
|
+
|
|
205
|
+
fs.writeFileSync(mappingFile, JSON.stringify(sortedMapping, null, 2));
|
|
206
|
+
console.log(
|
|
207
|
+
`Generated mapping.json with ${Object.keys(mapping).length} symbols.`,
|
|
208
|
+
);
|