forked from swc-project/swc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
227 lines (227 loc) · 8.36 KB
/
postinstall.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"use strict";
var __createBinding =
(this && this.__createBinding) ||
(Object.create
? function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (
!desc ||
("get" in desc
? !m.__esModule
: desc.writable || desc.configurable)
) {
desc = {
enumerable: true,
get: function () {
return m[k];
},
};
}
Object.defineProperty(o, k2, desc);
}
: function (o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
});
var __setModuleDefault =
(this && this.__setModuleDefault) ||
(Object.create
? function (o, v) {
Object.defineProperty(o, "default", {
enumerable: true,
value: v,
});
}
: function (o, v) {
o["default"] = v;
});
var __importStar =
(this && this.__importStar) ||
function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (
k !== "default" &&
Object.prototype.hasOwnProperty.call(mod, k)
)
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter =
(this && this.__awaiter) ||
function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P
? value
: new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done
? resolve(result.value)
: adopt(result.value).then(fulfilled, rejected);
}
step(
(generator = generator.apply(thisArg, _arguments || [])).next()
);
});
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* A postinstall script runs after `@swc/core` is installed.
*
* It checks if corresponding optional dependencies for native binary is installed and can be loaded properly.
* If it fails, it'll internally try to install `@swc/wasm` as fallback.
*/
const fs_1 = require("fs");
const assert = __importStar(require("assert"));
const path = __importStar(require("path"));
const child_process = __importStar(require("child_process"));
const fs = __importStar(require("fs"));
function removeRecursive(dir) {
for (const entry of fs.readdirSync(dir)) {
const entryPath = path.join(dir, entry);
let stats;
try {
stats = fs.lstatSync(entryPath);
} catch (_a) {
continue; // Guard against https://github.com/nodejs/node/issues/4760
}
if (stats.isDirectory()) removeRecursive(entryPath);
else fs.unlinkSync(entryPath);
}
fs.rmdirSync(dir);
}
/**
* Trying to validate @swc/core's native binary installation, then installs if it is not supported.
*/
const validateBinary = () =>
__awaiter(void 0, void 0, void 0, function* () {
var _a;
try {
const { name } = require(path.resolve(
process.env.INIT_CWD,
"package.json"
));
if (name === "@swc/core") {
return;
}
} catch (_) {
return;
}
// TODO: We do not take care of the case if user try to install with `--no-optional`.
// For now, it is considered as deliberate decision.
let binding;
try {
binding = require("./binding");
// Check if binding binary actually works.
// For the latest version, checks target triple. If it's old version doesn't have target triple, use parseSync instead.
const triple = binding.getTargetTriple
? binding.getTargetTriple()
: binding.parseSync(
"console.log()",
Buffer.from(JSON.stringify({ syntax: "ecmascript" }))
);
assert.ok(
triple,
"Failed to read target triple from native binary."
);
} catch (error) {
// if error is unsupported architecture, ignore to display.
if (
!((_a = error.message) === null || _a === void 0
? void 0
: _a.includes("Unsupported architecture"))
) {
console.warn(error);
}
console.warn(
`@swc/core was not able to resolve native bindings installation. It'll try to use @swc/wasm as fallback instead.`
);
}
if (!!binding) {
return;
}
// User choose to override the binary installation. Skip remanining validation.
if (!!process.env["SWC_BINARY_PATH"]) {
console.warn(
`@swc/core could not resolve native bindings installation, but found manual override config SWC_BINARY_PATH specified. Skipping remaning validation.`
);
return;
}
// Check if top-level package.json installs @swc/wasm separately already
let wasmBinding;
try {
wasmBinding = require.resolve(`@swc/wasm`);
} catch (_) {}
if (!!wasmBinding && (0, fs_1.existsSync)(wasmBinding)) {
return;
}
const env = Object.assign(Object.assign({}, process.env), {
npm_config_global: undefined,
});
const { version } = require(path.join(
path.dirname(require.resolve("@swc/core")),
"package.json"
));
// We want to place @swc/wasm next to the @swc/core as if normal installation was done,
// but can't directly set cwd to INIT_CWD as npm seems to acquire lock to the working dir.
// Instead, create a temporary inner and move it out.
const coreDir = path.dirname(require.resolve("@swc/core"));
const installDir = path.join(coreDir, "npm-install");
try {
fs.mkdirSync(installDir);
fs.writeFileSync(path.join(installDir, "package.json"), "{}");
// Instead of carrying over own dependencies to download & resolve package which increases installation sizes of `@swc/core`,
// assume & relies on system's npm installation.
child_process.execSync(
`npm install --no-save --loglevel=error --prefer-offline --no-audit --progress=false @swc/wasm@${version}`,
{ cwd: installDir, stdio: "pipe", env }
);
const installedBinPath = path.join(
installDir,
"node_modules",
`@swc/wasm`
);
// INIT_CWD is injected via npm. If it doesn't exists, can't proceed.
fs.renameSync(
installedBinPath,
path.resolve(process.env.INIT_CWD, "node_modules", `@swc/wasm`)
);
} catch (error) {
console.error(error);
console.error(`Failed to install fallback @swc/wasm@${version}. @swc/core will not properly.
Please install @swc/wasm manually, or retry whole installation.
If there are unexpected errors, please report at https://github.com/swc-project/swc/issues`);
} finally {
try {
removeRecursive(installDir);
} catch (_) {
// Gracefully ignore any failures. This'll make few leftover files but it shouldn't block installation.
}
}
});
validateBinary().catch((error) => {
// for now just throw the error as-is.
throw error;
});