Skip to content

Commit 40de26b

Browse files
authored
C3 - Re-enable pnpm test and cleanup pages projects between retries (#3796)
* C3 - Re-enable pnpm test and cleanup pages projects between retries * C3: Convert CI scripts to .ts
1 parent 94f1580 commit 40de26b

File tree

6 files changed

+56
-24
lines changed

6 files changed

+56
-24
lines changed

.github/workflows/test-c3.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
# os: [ubuntu-latest, windows-latest, macos-latest]
6363
os: [ubuntu-latest]
6464
# pm: [npm, yarn, pnpm]
65-
pm: [npm]
65+
pm: [npm, pnpm]
6666
runs-on: ${{ matrix.os }}
6767
steps:
6868
- name: Checkout Repo

packages/create-cloudflare/e2e-tests/pages.test.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FrameworkMap } from "frameworks/index";
33
import { readJSON } from "helpers/files";
44
import { fetch } from "undici";
55
import { describe, expect, test, afterEach, beforeEach } from "vitest";
6+
import { deleteProject } from "../scripts/e2eCleanup";
67
import { keys, runC3, testProjectDir } from "./helpers";
78
import type { RunnerConfig } from "./helpers";
89

@@ -16,16 +17,25 @@ type FrameworkTestConfig = RunnerConfig & {
1617
};
1718

1819
describe(`E2E: Web frameworks`, () => {
19-
const { getPath, clean } = testProjectDir("pages");
20+
const { getPath, getName, clean } = testProjectDir("pages");
2021

2122
beforeEach((ctx) => {
2223
const framework = ctx.meta.name;
2324
clean(framework);
2425
});
2526

26-
afterEach((ctx) => {
27+
afterEach(async (ctx) => {
2728
const framework = ctx.meta.name;
2829
clean(framework);
30+
31+
// Cleanup the pages project in case we need to retry it
32+
const projectName = getName(framework);
33+
try {
34+
await deleteProject(projectName);
35+
} catch (error) {
36+
console.error(`Failed to cleanup project: ${projectName}`);
37+
console.error(error);
38+
}
2939
});
3040

3141
const runCli = async (

packages/create-cloudflare/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
"templates"
2727
],
2828
"scripts": {
29-
"build": "node scripts/build.js",
29+
"build": "node -r esbuild-register scripts/build.ts",
3030
"check:lint": "eslint .",
3131
"check:type": "tsc",
3232
"lint": "eslint",
3333
"prepublishOnly": "npm run build",
34-
"test:e2e:cleanup": "node scripts/e2eCleanup.mjs",
34+
"test:e2e:cleanup": "node -r esbuild-register scripts/e2eCleanup.ts",
3535
"test:e2e:npm": "npm run build && TEST_PM=npm vitest run --config ./vitest-e2e.config.ts",
3636
"test:e2e:pnpm": "npm run build && TEST_PM=pnpm vitest run --config ./vitest-e2e.config.ts",
3737
"test:unit": "vitest run --config ./vitest.config.ts",
38-
"watch": "node scripts/build.js --watch"
38+
"watch": "node -r esbuild-register scripts/build.ts --watch"
3939
},
4040
"devDependencies": {
4141
"@babel/parser": "^7.21.3",

packages/create-cloudflare/scripts/build.js renamed to packages/create-cloudflare/scripts/build.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const { build, context } = require("esbuild");
2-
const { cp } = require("fs/promises");
1+
import { build, context, BuildOptions } from "esbuild";
2+
import { cp } from "fs/promises";
33

44
const run = async () => {
55
const argv = process.argv.slice(2);
66
const watchMode = argv[0] === "--watch";
77

8-
const config = {
8+
const config: BuildOptions = {
99
entryPoints: ["./src/cli.ts"],
1010
bundle: true,
1111
outdir: "./dist",

packages/create-cloudflare/scripts/e2eCleanup.mjs renamed to packages/create-cloudflare/scripts/e2eCleanup.ts

+36-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,23 @@ if (!process.env.CLOUDFLARE_ACCOUNT_ID) {
1010
process.exit(1);
1111
}
1212

13-
const apiFetch = async (path, init = { method: "GET" }, queryParams = {}) => {
13+
type ApiErrorBody = {
14+
errors: string[];
15+
};
16+
17+
type ApiSuccessBody = {
18+
result: any[];
19+
};
20+
21+
type Project = {
22+
name: string;
23+
};
24+
25+
const apiFetch = async (
26+
path: string,
27+
init = { method: "GET" },
28+
queryParams = {}
29+
) => {
1430
const baseUrl = `https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}`;
1531
const queryString = queryParams
1632
? `?${new URLSearchParams(queryParams).toString()}`
@@ -27,7 +43,7 @@ const apiFetch = async (path, init = { method: "GET" }, queryParams = {}) => {
2743
if (response.status >= 400) {
2844
console.error(`REQUEST ERROR: ${url}`);
2945
console.error(`(${response.status}) ${response.statusText}`);
30-
const body = await response.json();
46+
const body = (await response.json()) as ApiErrorBody;
3147
console.error(body.errors);
3248

3349
// Returning null instead of throwing an error here allows the caller to decide whether
@@ -37,7 +53,7 @@ const apiFetch = async (path, init = { method: "GET" }, queryParams = {}) => {
3753
return null;
3854
}
3955

40-
const json = await response.json();
56+
const json = (await response.json()) as ApiSuccessBody;
4157

4258
return json.result;
4359
};
@@ -72,20 +88,25 @@ const listC3Projects = async () => {
7288
return projects.filter((p) => p.name.startsWith("c3-e2e-"));
7389
};
7490

75-
const deleteProject = async (project) => {
76-
console.log(`Deleting project: ${project.name}`);
77-
await apiFetch(`/pages/projects/${project.name}`, {
91+
export const deleteProject = async (project: string) => {
92+
console.log(`Deleting project: ${project}`);
93+
await apiFetch(`/pages/projects/${project}`, {
7894
method: "DELETE",
7995
});
8096
};
8197

82-
const projectsToDelete = await listC3Projects();
83-
for (const project of projectsToDelete) {
84-
await deleteProject(project);
85-
}
98+
const run = async () => {
99+
const projectsToDelete = (await listC3Projects()) as Project[];
86100

87-
if (projectsToDelete.length === 0) {
88-
console.log(`No projects to delete.`);
89-
} else {
90-
console.log(`Successfully deleted ${projectsToDelete.length} projects`);
91-
}
101+
for (const project of projectsToDelete) {
102+
await deleteProject(project.name);
103+
}
104+
105+
if (projectsToDelete.length === 0) {
106+
console.log(`No projects to delete.`);
107+
} else {
108+
console.log(`Successfully deleted ${projectsToDelete.length} projects`);
109+
}
110+
};
111+
112+
run();

packages/create-cloudflare/src/helpers/command.ts

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export const resetPackageManager = async (ctx: PagesGeneratorContext) => {
251251

252252
await runCommand(`${npm} install`, {
253253
silent: true,
254+
cwd: ctx.project.path,
254255
startText: "Installing dependencies",
255256
doneText: `${brandColor("installed")} ${dim(`via \`${npm} install\``)}`,
256257
});

0 commit comments

Comments
 (0)