Skip to content

Commit 3c39822

Browse files
committed
fix: Update collect-schemas script to collect partial definitions
1 parent 4eea2e0 commit 3c39822

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

packages/@green-stack-core/scripts/collect-schemas.ts

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs'
2-
import { getAvailableSchemas } from './helpers/scriptUtils'
2+
import { getAvailableSchemas, globRel, excludeModules, parseWorkspaces, uppercaseFirstChar, createDivider } from './helpers/scriptUtils'
33
import { isEmpty } from '../utils/commonUtils'
44

55
/* --- Constants ------------------------------------------------------------------------------ */
@@ -11,6 +11,11 @@ const genMsg = `// -i- Auto generated with "npx turbo run @green-stack/core#coll
1111
const collectSchemas = () => {
1212
try {
1313

14+
// Get workspace imports
15+
const { workspaceImports } = parseWorkspaces('../../')
16+
17+
/* -- 1. Schemas -- */
18+
1419
// Figure out the available schemas
1520
const availableSchemas = getAvailableSchemas('../../', { includeOptOut: true })
1621

@@ -25,11 +30,106 @@ const collectSchemas = () => {
2530
}
2631

2732
// Log completion message
28-
console.log('-----------------------------------------------------------------')
33+
console.log('\n-----------------------------------------------------------------')
2934
console.log('-i- Successfully created schema registry at:')
3035
console.log('-----------------------------------------------------------------')
3136
console.log(' ✅ packages/@registries/schemas.generated.ts')
3237

38+
/* -- 2. Partials? -- */
39+
40+
// Check if we should collect partial schemas from workspaces
41+
const packagePartialPaths = globRel(`../../packages/**/schemas/[A-Z]*.partial.ts`).filter(excludeModules)
42+
const featurePartialPaths = globRel(`../../features/**/schemas/[A-Z]*.partial.ts`).filter(excludeModules)
43+
const allPartialSchemaPaths = [...packagePartialPaths, ...featurePartialPaths]
44+
45+
// Abort and put an empty partials config if there were no partial schemas to collect
46+
if (isEmpty(allPartialSchemaPaths)) {
47+
fs.writeFileSync('../../packages/@registries/schemas.partials.ts', [
48+
49+
genMsg,
50+
`import { z } from '@green-stack/schemas'`,
51+
52+
createDivider('Exports'),
53+
54+
`export const partials = {} as Record<string, z.ZodRawShape>`,
55+
56+
].join('\n'))
57+
return
58+
}
59+
60+
// Build the partial schema registry file
61+
const partialsLookup = allPartialSchemaPaths.reduce((acc, partialPath) => {
62+
63+
// Figure out the schema name
64+
const schemaFileName = partialPath.split('/').pop()!.replace('.ts', '')
65+
const schemaName = schemaFileName.split('.').reverse()!.pop()!
66+
const SchemaName = uppercaseFirstChar(schemaName)
67+
68+
// Determine import line + partial spread
69+
const workspacePath = partialPath.split('/schemas/')?.[0]?.replace(`../../`, '')
70+
const workspaceName = workspaceImports[workspacePath]
71+
const [pkgScopeOrName, ...restPkgName] = workspaceName.replace('@', '').replace('-', '').split('/')
72+
const workspacePrefix = [pkgScopeOrName, ...(restPkgName.map(uppercaseFirstChar))].join('')
73+
const importName = `${workspacePrefix}${uppercaseFirstChar(schemaName)}Fields`
74+
const importLine = `import * as ${importName} from '${workspaceName}/schemas/${schemaFileName}'`
75+
76+
// Determine entries
77+
const partialSpread = ` ...${importName},`
78+
const partialEntry = ` ${SchemaName}: ${SchemaName}Partials,`
79+
80+
// Append to registry
81+
return {
82+
[SchemaName]: {
83+
SchemaName,
84+
importLines: [...(acc[SchemaName]?.importLines || []), importLine],
85+
partialSpreads: [...(acc[SchemaName]?.partialSpreads || []), partialSpread],
86+
partialEntry,
87+
partialDivider: createDivider(`Expansions > ${SchemaName}.schema`),
88+
}
89+
}
90+
91+
}, {} as Record<string, {
92+
SchemaName: string,
93+
importLines: string[],
94+
partialSpreads: string[],
95+
partialEntry: string,
96+
partialDivider: string,
97+
}>)
98+
99+
// Group all import lines
100+
const partialConfigs = Object.values(partialsLookup)
101+
const allImportLines = partialConfigs.flatMap(cfg => cfg.importLines)
102+
103+
// Build final schema partials registry content
104+
const partialsRegistry = [
105+
106+
`${genMsg}${allImportLines.join('\n')}\n`,
107+
108+
...Object.keys(partialsLookup).map((schemaName: string) => [
109+
110+
`${partialsLookup[schemaName].partialDivider}\n`,
111+
112+
`// -i- Expandable partial fields for the '${schemaName}' schema`,
113+
`export const ${schemaName}Partials = {`,
114+
...partialsLookup[schemaName].partialSpreads,
115+
`}\n`,
116+
117+
].join('\n')).flat(),
118+
119+
`${createDivider('Exports')}\n`,
120+
121+
`export const partials = {`,
122+
...partialConfigs.map(cfg => cfg.partialEntry),
123+
`}\n`,
124+
125+
].join('\n')
126+
127+
// Write barrel file to 'packages/@registries/schemas.partials.ts'
128+
fs.writeFileSync('../../packages/@registries/schemas.partials.ts', partialsRegistry)
129+
130+
// Log completion message
131+
console.log(' ✅ packages/@registries/schemas.partials.ts')
132+
33133
} catch (error) {
34134
console.error("Error running script 'collect-schemas':", error)
35135
process.exit(1)

packages/@registries/models.generated.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// -i- Auto generated with "npx turbo run @db/driver#collect-models"
2+
import { driverModel as Users } from '@app/core/models/Users.model.ts'
23
import { driverModel as Settings } from '@app/core/models/Settings.model.ts'
34

45
/* --- Reexports ------------------------------------------------------------------------------- */
56

67
export {
8+
Users,
79
Settings
810
}
911

1012
/* --- Models ---------------------------------------------------------------------------------- */
1113

1214
const dbModels = {
15+
Users,
1316
Settings
1417
}
1518

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// -i- Auto generated with "npx turbo run @green-stack/core#collect:schemas"
2+
3+
import { z } from '@green-stack/schemas'
4+
/* --- Exports --------------------------------------------------------------------------------- */
5+
export const partials = {} as Record<string, z.ZodRawShape>

0 commit comments

Comments
 (0)