Repository navigation
Expand file tree
/
Copy pathpostInstall.js
More file actions
177 lines (161 loc) · 5.63 KB
/
Copy pathpostInstall.js
File metadata and controls
177 lines (161 loc) · 5.63 KB
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
// Copied from https://github.com/microsoft/vscode-jupyter/blob/main/build/ci/postInstall.js
const fs = require("fs");
const path = require("path");
/**
* In order to get raw kernels working, we reuse the default kernel that jupyterlab ships.
* However it expects to be talking to a websocket which is serializing the messages to strings.
* Our raw kernel is not a web socket and needs to do its own serialization. To do so, we make a copy
* of the default kernel with the serialization stripped out. This is simpler than making a copy of the module
* at runtime.
*/
function createJupyterKernelWithoutSerialization() {
var relativePath = path.join(
"node_modules",
"@jupyterlab",
"services",
"lib",
"kernel",
"default.js",
);
var filePath = path.join("", relativePath);
if (!fs.existsSync(filePath)) {
throw new Error(
"Jupyter lab default kernel not found '" +
filePath +
"' (Jupyter Extension post install script)",
);
}
var fileContents = fs.readFileSync(filePath, { encoding: "utf8" });
// Strip websocket serialization by replacing serializer calls with pass-through.
var replacedContents = fileContents
.replace(
/this\.serverSettings\.serializer\.deserialize\(([^,]+),\s*this\._ws\.protocol\)/g,
"$1",
)
.replace(
/this\.serverSettings\.serializer\.serialize\(([^,]+),\s*this\._ws\.protocol\)/g,
"$1",
);
if (replacedContents === fileContents) {
throw new Error(
"Jupyter lab default kernel cannot be made non serializing",
);
}
var destPath = path.join(path.dirname(filePath), "nonSerializingKernel.js");
fs.writeFileSync(destPath, replacedContents);
console.log(destPath + " file generated (by Jupyter VSC)");
}
/**
* Install altimate-core platform binaries.
*
* When VSCE_TARGET is set (CI builds platform-specific VSIXs), only install the
* matching platform binary. Otherwise (local dev), install all platforms so the
* extension works regardless of which machine it runs on.
*
* npm only installs optional dependencies for the current platform.
* We use npm pack + tar to manually extract the tarballs into node_modules.
*/
async function installAltimateCoreAllPlatforms() {
const { execSync } = require("child_process");
// Read version from the main altimate-core package to keep platform
// packages in sync (npm pack without a version would fetch "latest").
const corePkgPath = path.join(
"node_modules",
"@altimateai",
"altimate-core",
"package.json",
);
if (!fs.existsSync(corePkgPath)) {
console.warn(
"@altimateai/altimate-core not found in node_modules, skipping platform binary install",
);
return;
}
const { version: coreVersion } = JSON.parse(
fs.readFileSync(corePkgPath, "utf8"),
);
console.log(`altimate-core version: ${coreVersion}`);
// Map VS Code target platforms to altimate-core npm package names
const vsceTargetToPackage = {
"darwin-arm64": "@altimateai/altimate-core-darwin-arm64",
"darwin-x64": "@altimateai/altimate-core-darwin-x64",
"linux-arm64": "@altimateai/altimate-core-linux-arm64-gnu",
"linux-x64": "@altimateai/altimate-core-linux-x64-gnu",
"win32-x64": "@altimateai/altimate-core-win32-x64-msvc",
};
const allPackages = Object.values(vsceTargetToPackage);
const vsceTarget = process.env.VSCE_TARGET;
let packagesToInstall;
if (vsceTarget) {
const pkg = vsceTargetToPackage[vsceTarget];
if (!pkg) {
console.warn(
`Unknown VSCE_TARGET "${vsceTarget}", installing all platforms`,
);
packagesToInstall = allPackages;
} else {
console.log(`VSCE_TARGET=${vsceTarget} — installing only ${pkg}`);
packagesToInstall = [pkg];
}
} else {
packagesToInstall = allPackages;
}
const missing = packagesToInstall.filter((pkg) => {
const pkgDir = path.join("node_modules", ...pkg.split("/"));
return !fs.existsSync(pkgDir);
});
if (missing.length === 0) {
console.log(
"All required altimate-core platform packages already installed",
);
return;
}
console.log(
`Installing altimate-core platform packages via npm pack: ${missing.join(", ")}`,
);
const os = require("os");
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "altimate-core-"));
for (const pkg of missing) {
try {
// npm pack downloads the tarball without os/cpu filtering
const tgzFile = execSync(
`npm pack ${pkg}@${coreVersion} --pack-destination ${tmpDir}`,
{ encoding: "utf8" },
).trim();
const tgzPath = path.join(tmpDir, tgzFile);
const destDir = path.join("node_modules", ...pkg.split("/"));
fs.mkdirSync(destDir, { recursive: true });
execSync(`tar xzf "${tgzPath}" --strip-components=1 -C "${destDir}"`);
console.log(`Installed ${pkg} via npm pack + tar`);
} catch (ex) {
console.error(`Failed to install ${pkg}: ${ex.message}`);
}
}
// Clean up temp dir
try {
fs.rmSync(tmpDir, { recursive: true });
} catch {}
// Verify required packages are present
const stillMissing = packagesToInstall.filter((pkg) => {
const pkgDir = path.join("node_modules", ...pkg.split("/"));
return !fs.existsSync(pkgDir);
});
if (stillMissing.length > 0) {
console.error(
`ERROR: These altimate-core packages are still missing: ${stillMissing.join(", ")}`,
);
} else {
console.log(
"All required altimate-core platform packages installed successfully",
);
}
}
createJupyterKernelWithoutSerialization();
installAltimateCoreAllPlatforms()
.then(() => {
process.exit(0);
})
.catch((ex) => {
console.error("Post-install failed", ex);
process.exit(1);
});