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

validate-packages.ts

80 lines1.9 KB

1import path from 'path'
2import fsp from 'fs/promises'
3import jsonfile from 'jsonfile'
4import { packages, rootDir } from './config'
5​
6import type { PackageJson } from 'type-fest'
7​
8async function run() {
9 console.info('Validating packages...')
10 const failedValidations: string[] = []
11​
12 await Promise.all(
13 packages.map(async (pkg) => {
14 const pkgJson = await readPackageJson(
15 path.resolve(rootDir, 'packages', pkg.packageDir, 'package.json'),
16 )
17​
18 await Promise.all(
19 pkg.entries.map(async (entryKey) => {
20 const entry = pkgJson[entryKey] as unknown
21​
22 if (typeof entry !== 'string') {
23 throw new Error(
24 `Missing entry for "${entryKey}" in ${pkg.packageDir}/package.json!`,
25 )
26 }
27​
28 const filePath = path.resolve(
29 rootDir,
30 'packages',
31 pkg.packageDir,
32 entry,
33 )
34​
35 try {
36 await fsp.access(filePath)
37 } catch (err) {
38 failedValidations.push(`Missing build file: ${filePath}`)
39 }
40 }),
41 )
42​
43 const defaultExport = pkgJson.exports?.['.']?.['default'] as unknown
44​
45 if (typeof defaultExport !== 'string') {
46 throw new Error(
47 `Missing exports['.']['default'] in ${pkg.packageDir}/package.json!`,
48 )
49 }
50​
51 const filePath = path.resolve(
52 rootDir,
53 'packages',
54 pkg.packageDir,
55 defaultExport,
56 )
57​
58 try {
59 await fsp.access(filePath)
60 } catch (err) {
61 failedValidations.push(`Missing build file: ${filePath}`)
62 }
63 }),
64 )
65 console.info('')
66 if (failedValidations.length > 0) {
67 throw new Error(
68 'Some packages failed validation:\n\n' + failedValidations.join('\n'),
69 )
70 }
72​
73run().catch((err) => {
74 console.info(err)
75 process.exit(1)
76})
77​
78async function readPackageJson(pathName: string) {
79 return (await jsonfile.readFile(pathName)) as PackageJson