forked from babel/babel
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rename-proposal-to-transform.js
executable file
·112 lines (94 loc) · 3.29 KB
/
rename-proposal-to-transform.js
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
109
110
111
112
#!/usr/bin/env node
import path from "node:path";
import fs from "node:fs";
import { Glob } from "glob";
import { execSync } from "node:child_process";
const oldName = process.argv[2].match(/proposal-[\w-]+/)[0];
const newName = process.argv[3] || oldName.replace("proposal-", "transform-");
const oldPackageFolder = path.resolve(process.cwd(), process.argv[2]);
const oldPackageJSONPath = path.join(oldPackageFolder, "package.json");
console.info("Updating package.json...");
{
const pkgJSON = JSON.parse(fs.readFileSync(oldPackageJSONPath, "utf8"));
pkgJSON.name = pkgJSON.name.replace(oldName, newName);
pkgJSON.description = pkgJSON.description.replace(
/the ([a-z-]+?)s? proposal/gi,
"$1s"
);
pkgJSON.repository.directory = pkgJSON.repository.directory.replace(
oldName,
newName
);
pkgJSON.homepage = pkgJSON.homepage.replace(oldName, newName);
fs.writeFileSync(oldPackageJSONPath, JSON.stringify(pkgJSON, null, 2) + "\n");
}
console.info("Updating the plugin name...");
pluginName: {
const indexPath = path.join(oldPackageFolder, "src", "index.ts");
let indexContents;
try {
indexContents = fs.readFileSync(indexPath, "utf8");
} catch (error) {
console.warn("Could not find src/index.ts, skipping...");
break pluginName;
}
const newContents = indexContents.replace(
new RegExp(`(?<=name:\\s*")${oldName}`),
newName
);
if (newContents === indexContents) {
console.warn("Could not find name in src/index.ts, skipping...");
break pluginName;
}
fs.writeFileSync(indexPath, newContents);
}
console.info("Updating the README...");
readme: {
const readmePath = path.join(oldPackageFolder, "README.md");
let indexContents;
try {
indexContents = fs.readFileSync(readmePath, "utf8");
} catch (error) {
console.warn("Could not find README.md, skipping...");
break readme;
}
const newContents = indexContents.replaceAll(oldName, newName);
if (newContents === indexContents) {
console.warn("Could not find name in README.md, skipping...");
break readme;
}
fs.writeFileSync(readmePath, newContents);
}
console.info("Renaming the folder...");
{
const newPackageFolder = oldPackageFolder.replace(oldName, newName);
if (fs.existsSync(newPackageFolder)) {
fs.rmSync(newPackageFolder, { recursive: true });
}
fs.renameSync(oldPackageFolder, newPackageFolder);
}
console.info("Renaming all usages in tests...");
await globRename(
"./{packages,eslint,codemods}/*/test/fixtures/**/options.json"
);
console.info("Renaming all usages in package.json files...");
await globRename("./{packages,eslint,codemods}/*/package.json");
console.info("Renaming other well-known usages...");
await globRename([
"packages/babel-core/src/parser/util/missing-plugin-helper.ts",
"packages/babel-preset-env/src/available-plugins.ts",
"packages/babel-standalone/scripts/pluginConfig.json",
]);
async function globRename(pattern) {
for await (const filename of new Glob(pattern, {})) {
const oldContents = fs.readFileSync(filename, "utf8");
const newContents = oldContents.replaceAll(oldName, newName);
if (oldContents !== newContents) {
fs.writeFileSync(filename, newContents);
}
}
}
if (!process.env.SKIP_BOOTSTRAP) {
console.info("Running `make bootstrap`...");
execSync("make bootstrap", { stdio: "inherit" });
}