-
Notifications
You must be signed in to change notification settings - Fork 57
/
migrate.ts
72 lines (58 loc) · 2.42 KB
/
migrate.ts
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
import fs from "fs";
import path from "path";
// Also need to find and replace `declare module ../../registry` and stand alone imports like `import "../../registry";`
const rootDir = (): string => path.join(__dirname);
updateImportsInWorkspaces();
function updateImportsInWorkspaces() {
const dir = rootDir();
const rootPackageJsonPath = path.join(dir, "package.json");
const rootPackageJson = require(rootPackageJsonPath);
rootPackageJson.workspaces.forEach((workspaceDir: string) => {
const workspacePackageDir = path.join(dir, workspaceDir);
console.log("Updating imports in", workspacePackageDir);
updateTsFilesInDirectory(workspacePackageDir);
});
}
function updateTsFilesInDirectory(workspaceDir: string) {
findTsFiles(workspaceDir).forEach((file) => {
const content = fs.readFileSync(file, "utf8");
const updatedContent = updateImportPaths(file, content);
fs.writeFileSync(file, updatedContent, "utf8");
});
}
function findTsFiles(dir: string): string[] {
const contents = fs.readdirSync(dir).map((file) => path.join(dir, file));
const dirs = contents.filter((filePath) => fs.statSync(filePath).isDirectory());
// Recurse first, then look for imports in the current directory
const nestedFiles = dirs.flatMap((dir) => findTsFiles(dir));
const currentFiles = contents.filter(
(filePath) => fs.statSync(filePath).isFile() && path.basename(filePath).endsWith(".ts"),
)!;
return [...nestedFiles, ...currentFiles];
}
// Function to modify the import/export statements
function updateImportPaths(filePath: string, fileContent: string) {
return fileContent.replace(/from\s+['"]([^'"]*)['"]/g, (match, p1) => {
// Check if the path is relative
if (!p1.startsWith(".") && !p1.startsWith("/")) {
return match; // Skip if the import is not a relative path
}
// TODO: clobber flag?
// Check if the path already has a file extension
if (path.extname(p1)) {
return match; // Skip if the import statement has an extension
}
const maybeDir = path.join(path.dirname(filePath), p1);
try {
if (fs.statSync(maybeDir).isDirectory()) {
// path.join strips ./ from the beginning of the path
p1 = "./" + path.join(p1, "index");
}
} catch (e) {
console.error("failed to stat", maybeDir);
}
if (p1 === ".") p1 = "./";
if (p1.endsWith("/")) p1 += "index";
return `from '${p1}.js'`; // Append .mjs to the import path
});
}