codemod 1.1.2 → 1.2.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/codemod +167 -1
- package/package.json +6 -10
- package/postinstall.js +0 -51
package/codemod
CHANGED
|
@@ -1 +1,167 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { spawn } = require("node:child_process");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
|
|
7
|
+
const binaryName = process.platform === "win32" ? "codemod.exe" : "codemod";
|
|
8
|
+
|
|
9
|
+
function detectPackageName() {
|
|
10
|
+
const { platform, arch } = process;
|
|
11
|
+
|
|
12
|
+
switch (platform) {
|
|
13
|
+
case "darwin":
|
|
14
|
+
if (arch === "arm64") {
|
|
15
|
+
return "@codemod.com/cli-darwin-arm64";
|
|
16
|
+
}
|
|
17
|
+
if (arch === "x64") {
|
|
18
|
+
return "@codemod.com/cli-darwin-x64";
|
|
19
|
+
}
|
|
20
|
+
break;
|
|
21
|
+
case "linux": {
|
|
22
|
+
const { family, MUSL } = require("detect-libc");
|
|
23
|
+
const libcSuffix =
|
|
24
|
+
family === MUSL ? "musl" : arch === "arm" ? "gnueabihf" : "gnu";
|
|
25
|
+
if (arch === "x64") {
|
|
26
|
+
return `@codemod.com/cli-linux-x64-${libcSuffix}`;
|
|
27
|
+
}
|
|
28
|
+
if (arch === "arm64") {
|
|
29
|
+
return `@codemod.com/cli-linux-arm64-${libcSuffix}`;
|
|
30
|
+
}
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
case "win32":
|
|
34
|
+
if (arch === "x64") {
|
|
35
|
+
return "@codemod.com/cli-win32-x64-msvc";
|
|
36
|
+
}
|
|
37
|
+
if (arch === "arm64") {
|
|
38
|
+
return "@codemod.com/cli-win32-arm64-msvc";
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function failToLocateBinary(pkgName, originalError) {
|
|
49
|
+
const messages = ["Failed to locate the codemod native binary."];
|
|
50
|
+
|
|
51
|
+
if (pkgName) {
|
|
52
|
+
messages.push(`Expected package: ${pkgName}`);
|
|
53
|
+
} else {
|
|
54
|
+
messages.push(
|
|
55
|
+
"This platform does not have a published prebuilt codemod binary.",
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (originalError) {
|
|
60
|
+
messages.push(`Underlying error: ${originalError.message}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
messages.push(
|
|
64
|
+
"If you are installing from npm, ensure optional dependencies are enabled (e.g. do not use --no-optional).",
|
|
65
|
+
);
|
|
66
|
+
messages.push(
|
|
67
|
+
"For local development, run `cargo build --package codemod-cli --release` before invoking this script.",
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
console.error(messages.join("\n"));
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolveBinaryPath() {
|
|
75
|
+
const pkgName = detectPackageName();
|
|
76
|
+
let moduleError = null;
|
|
77
|
+
|
|
78
|
+
if (pkgName) {
|
|
79
|
+
try {
|
|
80
|
+
const pkgDir = path.dirname(
|
|
81
|
+
require.resolve(`${pkgName}/package.json`, { paths: [__dirname] }),
|
|
82
|
+
);
|
|
83
|
+
const candidate = path.join(pkgDir, binaryName);
|
|
84
|
+
if (fs.existsSync(candidate)) {
|
|
85
|
+
return { binaryPath: candidate, pkgName };
|
|
86
|
+
}
|
|
87
|
+
} catch (err) {
|
|
88
|
+
if (err.code !== "MODULE_NOT_FOUND") {
|
|
89
|
+
moduleError = err;
|
|
90
|
+
} else {
|
|
91
|
+
moduleError = err;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
console.log("pkgName", pkgName);
|
|
97
|
+
|
|
98
|
+
const releaseCandidate = path.join(
|
|
99
|
+
__dirname,
|
|
100
|
+
"..",
|
|
101
|
+
"..",
|
|
102
|
+
"..",
|
|
103
|
+
"target",
|
|
104
|
+
"release",
|
|
105
|
+
binaryName,
|
|
106
|
+
);
|
|
107
|
+
if (fs.existsSync(releaseCandidate)) {
|
|
108
|
+
return { binaryPath: releaseCandidate, pkgName: null };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const debugCandidate = path.join(
|
|
112
|
+
__dirname,
|
|
113
|
+
"..",
|
|
114
|
+
"..",
|
|
115
|
+
"..",
|
|
116
|
+
"target",
|
|
117
|
+
"debug",
|
|
118
|
+
binaryName,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
if (fs.existsSync(debugCandidate)) {
|
|
122
|
+
return { binaryPath: debugCandidate, pkgName: null };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
failToLocateBinary(pkgName, moduleError);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const { binaryPath } = resolveBinaryPath();
|
|
129
|
+
|
|
130
|
+
const child = spawn(binaryPath, process.argv.slice(2), {
|
|
131
|
+
stdio: "inherit",
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
child.on("error", (error) => {
|
|
135
|
+
console.error("Failed to execute codemod binary.");
|
|
136
|
+
console.error(error);
|
|
137
|
+
process.exit(1);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const signals = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
141
|
+
|
|
142
|
+
const forwarders = signals.map((signal) => {
|
|
143
|
+
const handler = () => {
|
|
144
|
+
if (child.killed) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
child.kill(signal);
|
|
149
|
+
} catch (_) {
|
|
150
|
+
// Ignore
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
process.on(signal, handler);
|
|
154
|
+
return { signal, handler };
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
child.on("exit", (code, signal) => {
|
|
158
|
+
forwarders.forEach(({ signal, handler }) => {
|
|
159
|
+
process.removeListener(signal, handler);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
if (signal) {
|
|
163
|
+
process.kill(process.pid, signal);
|
|
164
|
+
} else {
|
|
165
|
+
process.exit(code == null ? 1 : code);
|
|
166
|
+
}
|
|
167
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codemod",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -19,21 +19,17 @@
|
|
|
19
19
|
],
|
|
20
20
|
"files": [
|
|
21
21
|
"codemod",
|
|
22
|
-
"postinstall.js",
|
|
23
22
|
"README.md"
|
|
24
23
|
],
|
|
25
24
|
"dependencies": {
|
|
26
25
|
"detect-libc": "^2.0.3"
|
|
27
26
|
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"postinstall": "node postinstall.js"
|
|
30
|
-
},
|
|
31
27
|
"optionalDependencies": {
|
|
32
|
-
"@codemod.com/cli-darwin-arm64": "1.
|
|
33
|
-
"@codemod.com/cli-darwin-x64": "1.
|
|
34
|
-
"@codemod.com/cli-linux-arm64-gnu": "1.
|
|
35
|
-
"@codemod.com/cli-linux-x64-gnu": "1.
|
|
36
|
-
"@codemod.com/cli-win32-x64-msvc": "1.
|
|
28
|
+
"@codemod.com/cli-darwin-arm64": "1.2.0",
|
|
29
|
+
"@codemod.com/cli-darwin-x64": "1.2.0",
|
|
30
|
+
"@codemod.com/cli-linux-arm64-gnu": "1.2.0",
|
|
31
|
+
"@codemod.com/cli-linux-x64-gnu": "1.2.0",
|
|
32
|
+
"@codemod.com/cli-win32-x64-msvc": "1.2.0"
|
|
37
33
|
},
|
|
38
34
|
"bin": {
|
|
39
35
|
"codemod": "codemod"
|
package/postinstall.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
|
|
4
|
-
const parts = [process.platform, process.arch];
|
|
5
|
-
if (process.platform === "linux") {
|
|
6
|
-
const { MUSL, family } = require("detect-libc");
|
|
7
|
-
if (family === MUSL) {
|
|
8
|
-
parts.push("musl");
|
|
9
|
-
} else if (process.arch === "arm") {
|
|
10
|
-
parts.push("gnueabihf");
|
|
11
|
-
} else {
|
|
12
|
-
parts.push("gnu");
|
|
13
|
-
}
|
|
14
|
-
} else if (process.platform === "win32") {
|
|
15
|
-
parts.push("msvc");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const binary = process.platform === "win32" ? "codemod.exe" : "codemod";
|
|
19
|
-
|
|
20
|
-
let pkgPath;
|
|
21
|
-
try {
|
|
22
|
-
pkgPath = path.dirname(
|
|
23
|
-
require.resolve(`@codemod.com/cli-${parts.join("-")}/package.json`),
|
|
24
|
-
);
|
|
25
|
-
} catch (err) {
|
|
26
|
-
// Fallback for development - look for local binary
|
|
27
|
-
pkgPath = path.join(__dirname, "..", "target", "release");
|
|
28
|
-
if (!fs.existsSync(path.join(pkgPath, binary))) {
|
|
29
|
-
pkgPath = path.join(__dirname, "..", "target", "debug");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
fs.linkSync(path.join(pkgPath, binary), path.join(__dirname, binary));
|
|
35
|
-
} catch (err) {
|
|
36
|
-
try {
|
|
37
|
-
fs.copyFileSync(path.join(pkgPath, binary), path.join(__dirname, binary));
|
|
38
|
-
} catch (err) {
|
|
39
|
-
console.error("Failed to install codemod binary.");
|
|
40
|
-
console.error("Platform:", process.platform, process.arch);
|
|
41
|
-
console.error("Expected package:", `@codemod.com/cli-${parts.join("-")}`);
|
|
42
|
-
process.exit(1);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Make binary executable
|
|
47
|
-
try {
|
|
48
|
-
fs.chmodSync(path.join(__dirname, binary), 0o755);
|
|
49
|
-
} catch (err) {
|
|
50
|
-
console.warn("Warning: Could not make binary executable");
|
|
51
|
-
}
|