Open History to see the last commit
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 }
38}
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 }
77}
78β
79if (failures.length > 0) {
80 console.log(
81 `\nFailed examples:\n${failures.map((failure) => `- ${failure}`).join('\n')}`,
82 )
83 process.exit(1)
84}
85β
86console.log(`\nBuilt ${examples.length} examples successfully.`)