-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessProject.ts
200 lines (171 loc) · 5.85 KB
/
ProcessProject.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
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
import { exec } from "child_process";
import { rmSync, readFileSync, writeFileSync, appendFileSync } from "node:fs";
import { join } from "node:path";
import { defaultEngines } from "./config.js";
import {
execDefaultOptions,
measureAverageAsyncFunctionTimeInMs,
} from "./helpers.js";
import {
Engines,
debug,
getEngines,
runWithNodeenv,
installNodeenv,
} from "./nodeenv.js";
import { CollectResult, Reporter } from "./reporters/Reporter.js";
import { TempFolderManager } from "./TempFolderManager.js";
import { Project, RunWithNodeenvResult, Command, Patch } from "./types.js";
export class ProcessProject {
private project: Project;
private projectTempFolder: string;
private engines: Engines | null = null;
private reporters: Reporter[];
private tempFolderManager = new TempFolderManager();
constructor(project: Project, reporters: Reporter[]) {
this.project = project;
this.projectTempFolder = this.tempFolderManager.add("dev-bench");
this.reporters = reporters;
}
private get repositoryFolder() {
return join(this.projectTempFolder, "repository");
}
private get nodeenvFolder() {
return join(this.projectTempFolder, "nodeenv");
}
private get projectRootFolder() {
return join(this.repositoryFolder, this.project.rootFolder);
}
private async gitClone() {
console.log(`Cloning ${this.project.gitUrl} into ${this.repositoryFolder}`);
const getGitCliParameters = () =>
Object.entries(this.project.gitCliConfigOverrides)
.map(([parameter, value]) => `-c ${[parameter, value].join("=")}`)
.join(" ");
const process = exec(
`git clone ${this.project.gitUrl} ${
this.repositoryFolder
} ${getGitCliParameters()}`,
execDefaultOptions
);
const result = await this.runProcess(process);
debug(result);
}
private async npmCi() {
console.log("Installing node modules");
const result = await this.runWithNodeenv("npm ci");
debug(result);
}
private async getEngines() {
if (this.engines === null) {
this.engines =
(await getEngines(join(this.projectRootFolder, "package.json"))) ??
defaultEngines;
}
return this.engines;
}
private async runWithNodeenv(command: string) {
const process = await runWithNodeenv(
command,
this.nodeenvFolder,
this.tempFolderManager,
this.projectRootFolder
);
return this.runProcess(process);
}
private async runProcess(process: ReturnType<typeof exec>) {
return new Promise<RunWithNodeenvResult>((resolve, reject) => {
if (process.stdout === null || process.stderr === null) {
return reject("cant start check process");
}
let output = "",
error = "";
process.stdout.on("data", (data) => {
output += data;
});
process.stderr.on("data", (data) => {
error += data;
});
process.on("exit", (code) => {
if (code === 0) {
resolve({ output, error });
} else {
reject(JSON.stringify({ code, error, output }, null, 2));
}
});
});
}
private async npmCommand(npmCommand: Required<Command>["npmCommand"]) {
return this.runWithNodeenv(["npm", npmCommand].join(" "));
}
private async npxCommand(npxCommand: Required<Command>["npxCommand"]) {
return this.runWithNodeenv(["npx", npxCommand].join(" "));
}
private async npmRun(npmScriptName: Required<Command>["npmScriptName"]) {
return this.npmCommand(["run", npmScriptName].join(" "));
}
private processPatch(patch: Patch) {
console.log(`Processing patch: ${patch.name}`);
const fullFilePath = join(this.projectRootFolder, patch.file);
if (patch.delete === true) {
rmSync(fullFilePath);
return;
} else if (patch.search !== undefined && patch.replace !== undefined) {
const original = readFileSync(fullFilePath, "utf-8");
const patched = original.replace(patch.search, patch.replace);
writeFileSync(fullFilePath, patched);
} else if (patch.append !== undefined) {
appendFileSync(fullFilePath, patch.append, "utf-8");
}
}
private async processCommand(command: Command) {
console.log(`Processing command: ${command.name}`);
const { npmScriptName, npxCommand, npmCommand } = command;
let asyncFunctionToMeasure: () => Promise<RunWithNodeenvResult>;
if (npmScriptName !== undefined) {
asyncFunctionToMeasure = () => this.npmRun(npmScriptName);
} else if (npxCommand !== undefined) {
asyncFunctionToMeasure = () => this.npxCommand(npxCommand);
} else if (npmCommand !== undefined) {
asyncFunctionToMeasure = () => this.npmCommand(npmCommand);
} else {
throw "no command";
}
const totals = await measureAverageAsyncFunctionTimeInMs(
asyncFunctionToMeasure
);
const result: CollectResult = {
commandName: command.name,
projectName: this.project.name,
totalsInMs: totals,
};
for (const reporter of this.reporters) {
await reporter.reportResult(result);
}
}
private async setupNodeenv() {
console.log("Setting up nodeenv");
const nodeenvInstallProcess = await installNodeenv(
await this.getEngines(),
this.projectRootFolder,
this.nodeenvFolder
);
const result = await this.runProcess(nodeenvInstallProcess);
debug(result);
}
private async cleanUp() {
console.log("Cleaning up");
await this.tempFolderManager.cleanup();
}
async main() {
console.log(`Processing project: ${this.project.name}`);
await this.gitClone();
this.project.patches?.forEach(this.processPatch.bind(this)); // patch before `npm ci` to allow disabling some post-install scripts, etc.
await this.setupNodeenv();
// await this.npmCi(); // TODO: make this configurable
for (const command of this.project.commands) {
await this.processCommand(command);
}
await this.cleanUp();
}
}