@@ -50,7 +50,8 @@ export const task: Task<Args> = {
5050
5151 const { local_repos} = await get_gitops_ready ( { config, dir, download, log} ) ;
5252
53- const outfile = resolve ( outdir , 'repos.ts' ) ;
53+ const outfile_json = resolve ( outdir , 'repos.json' ) ;
54+ const outfile_ts = resolve ( outdir , 'repos.ts' ) ;
5455
5556 // This searches the parent directory for the env var, so we don't use SvelteKit's $env imports
5657 const token = load_from_env ( 'SECRET_GITHUB_API_TOKEN' ) ;
@@ -76,27 +77,44 @@ export const task: Task<Args> = {
7677 ? '$lib/repo.svelte.js'
7778 : '@fuzdev/fuz_gitops/repo.svelte.js' ;
7879
79- log . info ( ' generating ' + outfile ) ;
80+ log . info ( ` generating ${ outfile_json } and ${ outfile_ts } ` ) ;
8081
82+ // Generate repos.json with the raw data
83+ const json_contents = await format_file ( JSON . stringify ( repos_json ) , { filepath : outfile_json } ) ;
84+ const existing_json = existsSync ( outfile_json ) ? await readFile ( outfile_json , 'utf8' ) : '' ;
85+ const json_changed = existing_json !== json_contents ;
86+ if ( json_changed ) {
87+ log . info ( `writing changes to ${ print_path ( outfile_json ) } ` ) ;
88+ await writeFile ( outfile_json , json_contents ) ;
89+ } else {
90+ log . info ( `no changes to ${ print_path ( outfile_json ) } ` ) ;
91+ }
92+
93+ // Generate repos.ts that imports from repos.json
8194 // TODO the `basename` is used here because we don't have an `origin_id` like with gen,
8295 // and this file gets re-exported,
8396 // and we don't want the file to change based on where it's being generated,
8497 // because for example linking to a local package would change the contents
85- const contents = `
86- // generated by ${ basename ( import . meta. filename ) } !! do not edit directly or risk lost data
87-
98+ const ts_contents = `
99+ // generated by ${ basename ( import . meta. filename ) } - do not edit
100+
88101 import type {RepoJson} from '${ repo_specifier } ';
89102
90- export const repos_json: Array<RepoJson> = ${ JSON . stringify ( repos_json , null , '\t' ) } ;
103+ import json from './repos.json' with {type: 'json'};
104+
105+ export const repos_json: Array<RepoJson> = json as Array<RepoJson>;
91106 ` ;
92107 // TODO think about possibly using the `gen` functionality in this task, not sure what the API design could look like
93- const formatted = await format_file ( contents , { filepath : outfile } ) ;
94- const existing = existsSync ( outfile ) ? await readFile ( outfile , 'utf8' ) : '' ;
95- if ( existing === formatted ) {
96- log . info ( `no changes to ${ print_path ( outfile ) } ` ) ;
108+ const formatted_ts = await format_file ( ts_contents , { filepath : outfile_ts } ) ;
109+ const existing_ts = existsSync ( outfile_ts ) ? await readFile ( outfile_ts , 'utf8' ) : '' ;
110+ if ( existing_ts === formatted_ts ) {
111+ log . info ( `no changes to ${ print_path ( outfile_ts ) } ` ) ;
97112 } else {
98- log . info ( `writing changes to ${ print_path ( outfile ) } ` ) ;
99- await writeFile ( outfile , formatted ) ;
113+ log . info ( `writing changes to ${ print_path ( outfile_ts ) } ` ) ;
114+ await writeFile ( outfile_ts , formatted_ts ) ;
115+ }
116+
117+ if ( json_changed ) {
100118 await invoke_task ( 'gen' ) ;
101119 }
102120
0 commit comments