-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeenv.ts
168 lines (145 loc) · 4.59 KB
/
nodeenv.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
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
import { exec, ExecOptions, execSync } from "node:child_process";
import { appendFileSync, readFileSync } from "node:fs";
import { EOL, platform } from "node:os";
import { dirname, isAbsolute, join } from "node:path";
import allNodeVersions from "all-node-versions";
import normalizeNodeVersion from "normalize-node-version";
import { fileURLToPath } from "url";
import { TempFolderManager } from "./TempFolderManager.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
export const debug = (stringOrObject: string | object) => {
if (process.env.DEBUG?.toLowerCase() !== "true") return;
const string =
typeof stringOrObject === "object"
? JSON.stringify(stringOrObject, null, 2)
: stringOrObject;
appendFileSync("log.txt", string + EOL);
};
export type Engines = {
node: string;
npm: string;
};
export const getPythonExecutable = (): "python" | "python3" | null => {
const checkExists = (executable: "python" | "python3"): boolean => {
try {
const result = execSync(`${executable} --version`, {
encoding: "utf-8",
stdio: "pipe",
});
if (result.startsWith("Python 3.")) return true;
// eslint-disable-next-line no-empty
} catch (e) {}
return false;
};
if (checkExists("python")) return "python";
if (checkExists("python3")) return "python3";
return null;
};
export const getEngines = async (
packageJsonPath = "package.json"
): Promise<Engines | undefined> => {
if (!isAbsolute(packageJsonPath))
packageJsonPath = join(__dirname, packageJsonPath);
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as {
engines?: {
node?: string;
npm?: string;
};
};
if (
packageJson.engines &&
packageJson.engines.node &&
packageJson.engines.npm
) {
const engines = {
node: await normalizeNodeVersion(packageJson.engines.node),
npm: packageJson.engines.npm,
};
return engines;
}
};
const nodeenvActivate = (nodeenvPath: string) =>
platform() === "win32"
? [join(nodeenvPath, "Scripts", "activate")]
: [". " + join(nodeenvPath, "bin", "activate")];
export const isDefaultNpmVersion = async (node: string, npm: string) => {
const nodeNormalized = await normalizeNodeVersion(node);
const { versions } = await allNodeVersions();
const defaultNpm = versions.find((x) => x.node === nodeNormalized)?.npm;
return defaultNpm === npm;
};
const getNodeenvCommand = ({ node }: Engines, nodeenvPath: string): string => {
const nodeenvCommand = `${getPythonExecutable()} -m nodeenv ${nodeenvPath} --node=${node}`;
debug({ nodeenvCommand });
return nodeenvCommand;
};
const getNodeenvActivateCommands = async (
{ node, npm }: Engines,
nodeenvPath: string
): Promise<string[]> => {
const nodeenvCommands = [...nodeenvActivate(nodeenvPath)];
if ((await isDefaultNpmVersion(node, npm)) === false) {
nodeenvCommands.push(`npm install --global npm@${npm}`);
}
debug({ nodeenvCommands });
return nodeenvCommands;
};
export const runWithNodeenv = async (
command: string,
nodeenvPath: string,
tempFolderManager: TempFolderManager,
cwd?: string
) => {
const useNodeenv: string[] = [];
if (
command === "npm install" ||
command === "npm i" ||
command === "npm ci"
) {
const cacheFolderFullPath = tempFolderManager.add("npm_cache");
command += ` --cache ${cacheFolderFullPath}`;
}
useNodeenv.push(...nodeenvActivate(nodeenvPath));
const options: ExecOptions = {
...(cwd ? { cwd } : {}),
};
if (platform() === "win32") {
const newEnv = { ...process.env }; // workaround for https://github.com/ekalinin/nodeenv/issues/309
for (const key in newEnv) {
if (key.startsWith("npm_")) {
delete newEnv[key];
}
}
options.env = newEnv;
} else {
options.shell = "/bin/bash";
}
debug({ useNodeenv, command, options });
return exec([...useNodeenv, command].join(" && "), options);
};
export const installNodeenv = async (
engines: Engines,
cwd: string,
nodeenvPath: string
) => {
const useNodeenv = [
getNodeenvCommand(engines, nodeenvPath),
...(await getNodeenvActivateCommands(engines, nodeenvPath)),
];
const options: ExecOptions = {
...(cwd ? { cwd } : {}),
};
if (platform() === "win32") {
const newEnv = { ...process.env }; // workaround for https://github.com/ekalinin/nodeenv/issues/309
for (const key in newEnv) {
if (key.startsWith("npm_")) {
delete newEnv[key];
}
}
options.env = newEnv;
} else {
options.shell = "/bin/bash";
}
debug({ useNodeenv, options });
return exec([...useNodeenv].join(" && "), options);
};