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/electric-db-collection`,
33 `@tanstack/query-db-collection`,
34 `@tanstack/powersync-db-collection`,
35 `@tanstack/rxdb-db-collection`,
36 `@tanstack/trailbase-db-collection`,
37 `@tanstack/db-collection-e2e`,
38 ],
39}
40
41const root = fileURLToPath(new URL(`../`, import.meta.url))
42const manifests = globSync(`packages/**/package.json`, {
43 cwd: root,
44 exclude: [`**/node_modules/**`, `**/dist/**`],
45}).map((file) => JSON.parse(readFileSync(join(root, file), `utf8`)))
46const testPackages = manifests.filter((manifest) => manifest.scripts?.test)
47const assigned = Object.values(groups).flat()
48for (const { name } of testPackages) {
49 if (assigned.filter((candidate) => candidate === name).length !== 1) {
50 throw new Error(`Assign ${name} to exactly one CI test group`)
51 }
52}
53for (const name of assigned) {
54 if (!testPackages.some((manifest) => manifest.name === name)) {
55 throw new Error(
56 `CI test group names a package without a test script: ${name}`,
57 )
58 }
59}
60
61const [command, group] = process.argv.slice(2)
62if (command === `check`) {
63 console.log(`CI groups cover all ${testPackages.length} package test scripts`)
64} else {
65 const dbShards = { 'db-1': `1/2`, 'db-2': `2/2`, 'db-replay': undefined }
66 const isDb = Object.hasOwn(dbShards, group)
67 const packages = groups[isDb ? `db` : group]
68 if (!packages || ![`build`, `test`].includes(command)) {
69 throw new Error(
70 `Usage: node scripts/ci-tests.mjs <check|build|test> [group]`,
71 )
72 }
73 let args
74 if (command === `build`) {
75 // pnpm skips packages without a build script, but still builds their
76 // dependencies. Keep its topological order for all selected packages.
77 args = [
78 ...packages.flatMap((name) => [`--filter`, `${name}...`]),
79 `run`,
80 `build`,
81 ]
82 } else if (isDb) {
83 const selection =
84 group === `db-replay`
85 ? [`tests/oracle-replay.test.ts`]
86 : [`--exclude=**/oracle-replay.test.ts`, `--shard=${dbShards[group]}`]
87 args = [
88 `--filter`,
89 `@tanstack/db`,
90 `run`,
91 `test`,
92 ...selection,
93 `--maxWorkers=2`,
94 `--reporter=default`,
95 `--reporter=blob`,
96 `--outputFile.blob=.vitest-reports/${group}.json`,
97 ]
98 } else {
99 // Builds are complete: test suites need not wait for dependency suites.
100 // Bound both package and Vitest concurrency on the four-core CI runner.
101 args = [
102 `--no-sort`,
103 `--workspace-concurrency=2`,
104 ...packages.flatMap((name) => [`--filter`, name]),
105 `run`,
106 `test`,
107 `--maxWorkers=2`,
108 ]
109 }
110 execFileSync(`pnpm`, args, { cwd: root, stdio: `inherit` })
111 if (command === `test` && group === `db-1`) {
112 // The group build is complete. Verify published declarations after runtime
113 // suites finish, without rebuilding a dependency they could still read.
114 execFileSync(
115 `pnpm`,
116 [
117 `--filter`,
118 `@tanstack/db`,
119 `exec`,
120 `vitest`,
121 `run`,
122 `--config`,
123 `vitest.indexed-db.package.config.ts`,
124 `--maxWorkers=2`,
125 ],
126 { cwd: root, stdio: `inherit` },
127 )
128 }
129}