Skip to content
Source
Sign inSign up
TanStack/virtual
Open History to see the last commit

verify-examples.ts

86 lines2.1 KB

1import { readFileSync } from 'node:fs'
2import { spawnSync } from 'node:child_process'
3import path from 'node:path'
4import { glob } from 'tinyglobby'
5​
6type PackageJson = {
7 name?: string
8 scripts?: Record<string, string>
9}
10​
11const filter = process.env.EXAMPLE_FILTER
12const skipPackageBuild = process.env.SKIP_PACKAGE_BUILD === 'true'
13​
14if (!skipPackageBuild) {
15 const nxBin =
16 process.platform === 'win32'
17 ? 'node_modules/.bin/nx.cmd'
18 : 'node_modules/.bin/nx'
19​
20 console.log('\nBuilding workspace packages')
21​
22 const packageBuild = spawnSync(
23 nxBin,
24 ['run-many', '--target=build', '--exclude=examples/**'],
25 {
26 env: {
27 ...process.env,
28 NX_DAEMON: 'false',
29 },
30 stdio: 'inherit',
31 },
32 )
33​
34 if (packageBuild.status !== 0) {
35 console.log('\nWorkspace package build failed.')
36 process.exit(1)
37 }
39​
40const packageJsonPaths = await glob('examples/*/*/package.json')
41const examples = packageJsonPaths
42 .map((packageJsonPath) => {
43 const directory = path.dirname(packageJsonPath)
44 const packageJson = JSON.parse(
45 readFileSync(packageJsonPath, 'utf-8'),
46 ) as PackageJson
47​
48 return {
49 directory,
50 name: packageJson.name ?? directory,
51 hasBuild: Boolean(packageJson.scripts?.build),
52 }
53 })
54 .filter((example) => !filter || example.directory.includes(filter))
55 .sort((a, b) => a.directory.localeCompare(b.directory))
56​
57const failures: Array<string> = []
58​
59for (let index = 0; index < examples.length; index++) {
60 const example = examples[index]!
61​
62 if (!example.hasBuild) {
63 failures.push(`${example.directory} is missing a build script`)
64 continue
65 }
66​
67 console.log(`\n[${index + 1}/${examples.length}] ${example.directory}`)
68​
69 const result = spawnSync('npm', ['run', 'build', '--silent'], {
70 cwd: example.directory,
71 stdio: 'inherit',
72 })
73​
74 if (result.status !== 0) {
75 failures.push(`${example.directory} (${example.name})`)
76 }
78​
79if (failures.length > 0) {
80 console.log(
81 `\nFailed examples:\n${failures.map((failure) => `- ${failure}`).join('\n')}`,
82 )
83 process.exit(1)
85​
86console.log(`\nBuilt ${examples.length} examples successfully.`)