11import { readFileSync , readdirSync , watch } from "node:fs" ;
22import { access } from "node:fs/promises" ;
33import path from "node:path" ;
4+ import { type ContributorSchema as ContributorSchemaType } from "../contributors/_schema" ;
5+ import { createJiti } from "jiti" ;
46
57import { parse } from "yaml" ;
68
7- import { ContributorSchema as ContributorFunction } from "../contributors/_schema" ;
89import z from "zod" ;
910
11+ const jiti = createJiti ( import . meta. url ) ;
12+
13+ async function getContributor ( ) {
14+ // Clear schema cache to pick up changes
15+ Object . keys ( jiti . cache ) . forEach ( ( key ) => {
16+ if ( key . includes ( "_schema" ) ) {
17+ delete jiti . cache [ key ] ;
18+ }
19+ } ) ;
20+ const { ContributorSchema } = ( await jiti . import (
21+ "../contributors/_schema"
22+ ) ) as { ContributorSchema : typeof ContributorSchemaType } ;
23+ // @ts -expect-error This is fucked up because it's type of image in Astro
24+ return ContributorSchema ( { image : z . string } ) ;
25+ }
26+
1027const TEAM_DIR = path . resolve ( process . cwd ( ) , "contributors" ) ;
1128const PROJECTS_FILE = path . resolve (
1229 process . cwd ( ) ,
@@ -48,9 +65,6 @@ class InvalidProjectError extends Error {
4865 }
4966}
5067
51- // @ts -expect-error This is fucked up because it's type of image in Astro
52- const Contributor = ContributorFunction ( { image : z . string } ) ;
53-
5468function formatValidationError ( error : any , filePath : string ) : string {
5569 if ( error . code === "unrecognized_keys" ) {
5670 if ( error . path . length === 1 && error . path [ 0 ] . includes ( "roles" ) ) {
@@ -94,6 +108,7 @@ async function validateTeamFile(
94108
95109 const content = readFileSync ( filePath , "utf-8" ) ;
96110 const data = parse ( content ) ;
111+ const Contributor = await getContributor ( ) ;
97112
98113 const validation = await Contributor . superRefine (
99114 async ( contributor , ctx ) => {
@@ -159,10 +174,10 @@ function validateAllTeamFiles(signal?: AbortSignal) {
159174 return results ;
160175}
161176
162- function printResults (
177+ async function printResults (
163178 results : ValidationResult [ ] ,
164179 isWatchMode : boolean = false
165- ) : void {
180+ ) {
166181 if ( isWatchMode ) {
167182 console . clear ( ) ;
168183 }
@@ -189,6 +204,7 @@ function printResults(
189204 ) ;
190205
191206 if ( hasErrors ) {
207+ const Contributor = await getContributor ( ) ;
192208 console . log ( "\n💡 Tips:" ) ;
193209 console . log (
194210 ` • Project names and roles must match those in ./${ path . relative (
@@ -226,7 +242,7 @@ async function runValidation(isWatchMode: boolean = false) {
226242 const results = await Promise . all ( await validateAllTeamFiles ( signal ) ) ;
227243 const hasErrors = results . some ( ( r ) => ! r . isValid ) ;
228244
229- printResults ( await Promise . all ( results ) ) ;
245+ await printResults ( await Promise . all ( results ) ) ;
230246 if ( isWatchMode ) {
231247 console . log ( "Waiting for more changes..." ) ;
232248 }
@@ -247,36 +263,33 @@ function startWatchMode(): void {
247263 console . log ( "Time to watch 👀" ) ;
248264
249265 // Run initial validation
250- runValidation ( ) ;
266+ runValidation ( true ) ;
251267
252268 const watchers : Array < { close : ( ) => void } > = [ ] ;
253269
254270 const teamWatcher = watch (
255271 TEAM_DIR ,
256272 { recursive : true } ,
257273 ( eventType , filename ) => {
258- console . log ( `${ eventType } : ${ filename } ` ) ;
259274 if (
260275 filename &&
261276 ( filename . endsWith ( ".yaml" ) || filename . endsWith ( ".yml" ) )
262277 ) {
263- runValidation ( ) ;
278+ runValidation ( true ) ;
264279 }
265280 }
266281 ) ;
267282 watchers . push ( teamWatcher ) ;
268283
269284 // Watch projects file
270- const projectsWatcher = watch ( PROJECTS_FILE , ( eventType ) => {
271- console . log ( `${ eventType } : ${ PROJECTS_FILE } ` ) ;
272- runValidation ( ) ;
285+ const projectsWatcher = watch ( PROJECTS_FILE , ( ) => {
286+ runValidation ( true ) ;
273287 } ) ;
274288 watchers . push ( projectsWatcher ) ;
275289
276290 // Watch schema file
277- const schemaWatcher = watch ( SCHEMA_FILE , ( eventType ) => {
278- console . log ( `${ eventType } : ${ SCHEMA_FILE } ` ) ;
279- runValidation ( ) ;
291+ const schemaWatcher = watch ( SCHEMA_FILE , ( ) => {
292+ runValidation ( true ) ;
280293 } ) ;
281294 watchers . push ( schemaWatcher ) ;
282295
0 commit comments