Open History to see the last commit
1import { execFileSync } from 'node:child_process'
2import { globSync, readFileSync } from 'node:fs'
3import { join } from 'node:path'
4import { fileURLToPath } from 'node:url'
5
6// Keep package selection in one place for both builds and tests. A new package
7// with a test script must be assigned here before CI can pass.
8const groups = {
9 db: [`@tanstack/db`],
10 'ivm-offline': [`@tanstack/db-ivm`, `@tanstack/offline-transactions`],
11 'sqlite-core': [`@tanstack/db-sqlite-persistence-core`],
12 'sqlite-adapters': [
13 `@tanstack/browser-db-sqlite-persistence`,
14 `@tanstack/node-db-sqlite-persistence`,
15 `@tanstack/electron-db-sqlite-persistence`,
16 `@tanstack/cloudflare-durable-objects-db-sqlite-persistence`,
17 `@tanstack/react-native-db-sqlite-persistence`,
18 `@tanstack/expo-db-sqlite-persistence`,
19 `@tanstack/capacitor-db-sqlite-persistence`,
20 `@tanstack/tauri-db-sqlite-persistence`,
21 ],
22 frameworks: [
23 `@tanstack/react-db`,
24 `@tanstack/preact-db`,
25 `@tanstack/react-router-with-db`,
26 `@tanstack/solid-db`,
27 `@tanstack/vue-db`,
28 `@tanstack/svelte-db`,
29 `@tanstack/angular-db`,
30 ],
31 collections: [
32 `@tanstack/convex-db-collection`,
33 `@tanstack/electric-db-collection`,
34 `@tanstack/query-db-collection`,
35 `@tanstack/powersync-db-collection`,
36 `@tanstack/rxdb-db-collection`,
37 `@tanstack/trailbase-db-collection`,
38 `@tanstack/db-collection-e2e`,
39 ],
40}
41
42const root = fileURLToPath(new URL(`../`, import.meta.url))
43const manifests = globSync(`packages/**/package.json`, {
44 cwd: root,
45 exclude: [`**/node_modules/**`, `**/dist/**`],
46}).map((file) => JSON.parse(readFileSync(join(root, file), `utf8`)))
47const testPackages = manifests.filter((manifest) => manifest.scripts?.test)
48const assigned = Object.values(groups).flat()
49for (const { name } of testPackages) {
50 if (assigned.filter((candidate) => candidate === name).length !== 1) {
51 throw new Error(`Assign ${name} to exactly one CI test group`)
52 }
53}
54for (const name of assigned) {
55 if (!testPackages.some((manifest) => manifest.name === name)) {
56 throw new Error(
57 `CI test group names a package without a test script: ${name}`,
58 )
59 }
60}
61
62const [command, group] = process.argv.slice(2)
63if (command === `check`) {
64 console.log(`CI groups cover all ${testPackages.length} package test scripts`)
65} else {
66 const dbShards = { 'db-1': `1/2`, 'db-2': `2/2`, 'db-replay': undefined }
67 const isDb = Object.hasOwn(dbShards, group)
68 const packages = groups[isDb ? `db` : group]
69 if (!packages || ![`build`, `test`].includes(command)) {
70 throw new Error(
71 `Usage: node scripts/ci-tests.mjs <check|build|test> [group]`,
72 )
73 }
74 let args
75 if (command === `build`) {
76 // pnpm skips packages without a build script, but still builds their
77 // dependencies. Keep its topological order for all selected packages.
78 args = [
79 ...packages.flatMap((name) => [`--filter`, `${name}...`]),
80 `run`,
81 `build`,
82 ]
83 } else if (isDb) {
84 const selection =
85 group === `db-replay`
86 ? [`tests/oracle-replay.test.ts`]
87 : [`--exclude=**/oracle-replay.test.ts`, `--shard=${dbShards[group]}`]
88 args = [
89 `--filter`,
90 `@tanstack/db`,
91 `run`,
92 `test`,
93 ...selection,
94 `--maxWorkers=2`,
95 `--reporter=default`,
96 `--reporter=blob`,
97 `--outputFile.blob=.vitest-reports/${group}.json`,
98 ]
99 } else {
100 // Builds are complete: test suites need not wait for dependency suites.
101 // Bound both package and Vitest concurrency on the four-core CI runner.
102 args = [
103 `--no-sort`,
104 `--workspace-concurrency=2`,
105 ...packages.flatMap((name) => [`--filter`, name]),
106 `run`,
107 `test`,
108 `--maxWorkers=2`,
109 ]
110 }
111 execFileSync(`pnpm`, args, { cwd: root, stdio: `inherit` })
112 if (command === `test` && group === `db-1`) {
113 // The group build is complete. Verify published declarations after runtime
114 // suites finish, without rebuilding a dependency they could still read.
115 execFileSync(
116 `pnpm`,
117 [
118 `--filter`,
119 `@tanstack/db`,
120 `exec`,
121 `vitest`,
122 `run`,
123 `--config`,
124 `vitest.indexed-db.package.config.ts`,
125 `--maxWorkers=2`,
126 ],
127 { cwd: root, stdio: `inherit` },
128 )
129 }
130}