Skip to content

Commit c1feac7

Browse files
committed
Add postinstall & hash handling step
1 parent 8ec0812 commit c1feac7

File tree

6 files changed

+126
-9
lines changed

6 files changed

+126
-9
lines changed

packages/cli-v3/e2e/compile.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,10 @@ export async function compile(options: CompileOptions) {
233233
// Save the entryPoint outputFile to /tmp/dir/index.js
234234
await writeFile(join(tempDir, "index.js"), entryPointOutputFile.text);
235235

236-
return { metaOutput, entryPointMetaOutput };
236+
return {
237+
workerMetaOutput: metaOutput,
238+
workerOutputFile,
239+
entryPointMetaOutput,
240+
entryPointOutputFile,
241+
};
237242
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { readFileSync } from "node:fs";
2+
import { writeFile } from "node:fs/promises";
3+
import { join, resolve } from "node:path";
4+
import { cliRootPath } from "../src/utilities/resolveInternalFilePath";
5+
import { ReadConfigResult } from "../src/utilities/configFiles";
6+
7+
type CreateContainerFileOptions = {
8+
resolvedConfig: ReadConfigResult;
9+
tempDir: string;
10+
};
11+
12+
export async function createContainerFile(options: CreateContainerFileOptions) {
13+
if (options.resolvedConfig.status === "error") {
14+
throw new Error("cannot resolve config");
15+
}
16+
const {
17+
resolvedConfig: { config },
18+
tempDir,
19+
} = options;
20+
21+
// COPIED FROM compileProject()
22+
// Write the Containerfile to /mpt / dir / Containerfile;
23+
// const containerFilePath = join(cliRootPath(), "Containerfile.prod");
24+
const containerFilePath = resolve("./src/Containerfile.prod");
25+
26+
let containerFileContents = readFileSync(containerFilePath, "utf-8");
27+
28+
if (config.postInstall) {
29+
containerFileContents = containerFileContents.replace(
30+
"__POST_INSTALL__",
31+
`RUN ${config.postInstall}`
32+
);
33+
} else {
34+
containerFileContents = containerFileContents.replace("__POST_INSTALL__", "");
35+
}
36+
37+
await writeFile(join(tempDir, "Containerfile"), containerFileContents);
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createHash } from "node:crypto";
2+
import { OutputFile } from "esbuild";
3+
4+
type CreateDeployHashOptions = {
5+
dependencies: { [k: string]: string };
6+
entryPointOutputFile: OutputFile;
7+
workerOutputFile: OutputFile;
8+
};
9+
10+
export async function createDeployHash(options: CreateDeployHashOptions) {
11+
const { entryPointOutputFile, workerOutputFile } = options;
12+
13+
// COPIED FROM compileProject()
14+
const contentHasher = createHash("sha256");
15+
contentHasher.update(Buffer.from(entryPointOutputFile.text));
16+
contentHasher.update(Buffer.from(workerOutputFile.text));
17+
contentHasher.update(Buffer.from(JSON.stringify(dependencies)));
18+
19+
const contentHash = contentHasher.digest("hex");
20+
21+
// span.setAttributes({
22+
// contentHash: contentHash,
23+
// });
24+
25+
// span.end();
26+
27+
return { contentHash };
28+
}

packages/cli-v3/e2e/handleDependencies.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ export async function handleDependencies(options: HandleDependenciesOptions) {
109109
if (!resolvingDependenciesResult) {
110110
throw new SkipLoggingError("Failed to resolve dependencies");
111111
}
112+
113+
return { dependencies };
112114
}

packages/cli-v3/e2e/index.test.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { PackageManager } from "../src/utilities/getUserPackageManager";
1010
import { logger } from "../src/utilities/logger";
1111
import { compile } from "./compile";
1212
import { handleDependencies } from "./handleDependencies";
13+
import { createContainerFile } from "./createContainerFile";
14+
import { createDeployHash } from "./createDeployHash";
1315

1416
type TestCase = {
1517
name: string;
@@ -175,12 +177,19 @@ if (testCases.length > 0) {
175177
async () => {
176178
const expectation = expect(
177179
(async () => {
178-
const { entryPointMetaOutput, metaOutput } = await compile({
180+
const {
181+
workerMetaOutput,
182+
workerOutputFile,
183+
entryPointMetaOutput,
184+
entryPointOutputFile,
185+
} = await compile({
179186
resolvedConfig: global.resolvedConfig!,
180187
tempDir: global.tempDir!,
181188
});
182189
global.entryPointMetaOutput = entryPointMetaOutput;
183-
global.metaOutput = metaOutput;
190+
global.entryPointOutputFile = entryPointOutputFile;
191+
global.workerMetaOutput = workerMetaOutput;
192+
global.workerOutputFile = workerOutputFile;
184193
})()
185194
);
186195

@@ -196,21 +205,24 @@ if (testCases.length > 0) {
196205
describe.skipIf(wantCompilationError)("with successful compilation", () => {
197206
afterAll(() => {
198207
delete global.entryPointMetaOutput;
199-
delete global.metaOutput;
208+
delete global.entryPointOutputFile;
209+
delete global.workerMetaOutput;
210+
delete global.workerOutputFile;
200211
});
201212

202213
test(
203214
wantDependenciesError ? "does not resolve dependencies" : "resolves dependencies",
204215
async () => {
205216
const expectation = expect(
206217
(async () => {
207-
await handleDependencies({
218+
const { dependencies } = await handleDependencies({
208219
entryPointMetaOutput: global.entryPointMetaOutput!,
209-
metaOutput: global.metaOutput!,
220+
metaOutput: global.workerMetaOutput!,
210221
resolvedConfig: global.resolvedConfig!,
211222
tempDir: global.tempDir!,
212223
packageManager,
213224
});
225+
global.dependencies = dependencies;
214226
})()
215227
);
216228

@@ -222,6 +234,35 @@ if (testCases.length > 0) {
222234
},
223235
{ timeout: 60_000 }
224236
);
237+
238+
describe.skipIf(wantDependenciesError)("with resolved dependencies", () => {
239+
afterAll(() => {
240+
delete global.dependencies;
241+
});
242+
243+
test("copies postinstall command into Containerfile.prod", async () => {
244+
await expect(
245+
(async () => {
246+
await createContainerFile({
247+
resolvedConfig: global.resolvedConfig!,
248+
tempDir: global.tempDir!,
249+
});
250+
})()
251+
).resolves.not.toThrowError();
252+
});
253+
254+
test("creates deploy hash", async () => {
255+
await expect(
256+
(async () => {
257+
await createDeployHash({
258+
dependencies: global.dependencies!,
259+
entryPointOutputFile: global.entryPointOutputFile!,
260+
workerOutputFile: global.workerOutputFile!,
261+
});
262+
})()
263+
).resolves.not.toThrowError();
264+
});
265+
});
225266
});
226267
});
227268
}

packages/cli-v3/e2e/vitest.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { Metafile } from "esbuild";
1+
import { Metafile, OutputFile } from "esbuild";
22

33
import { ReadConfigResult } from "../src/utilities/configFiles";
44

55
declare global {
6+
var dependencies: { [k: string]: string } | undefined;
7+
var entryPointMetaOutput: Metafile["outputs"]["out/stdin.js"] | undefined;
8+
var entryPointOutputFile: OutputFile | undefined;
69
var resolvedConfig: ReadConfigResult | undefined;
710
var tempDir: string | undefined;
8-
var metaOutput: Metafile["outputs"]["out/stdin.js"] | undefined;
9-
var entryPointMetaOutput: Metafile["outputs"]["out/stdin.js"] | undefined;
11+
var workerMetaOutput: Metafile["outputs"]["out/stdin.js"] | undefined;
12+
var workerOutputFile: OutputFile | undefined;
1013
}

0 commit comments

Comments
 (0)