11import fs from "node:fs" ;
22import path from "node:path" ;
3+ import { execSync } from "node:child_process" ;
34
45function readJson ( p ) {
56 return JSON . parse ( fs . readFileSync ( p , "utf8" ) ) ;
@@ -12,33 +13,86 @@ function listJsonFiles(dir) {
1213 . map ( ( f ) => path . join ( dir , f ) ) ;
1314}
1415
15- function main ( ) {
16- const root = process . cwd ( ) ;
16+ function sh ( cmd , cwd ) {
17+ return execSync ( cmd , { cwd, stdio : "pipe" } ) . toString ( "utf8" ) . trim ( ) ;
18+ }
1719
18- // repo "registry" cloné à côté, exemple:
19- // vixcpp.github.io/registry (ou vixcpp/registry)
20- // on tente plusieurs chemins
20+ function ensureRegistryRepo ( { root } ) {
2121 const candidates = [
22- path . join ( root , ".." , ".." , "registry" ) , // vix-site -> vixcpp.github.io -> registry
23- path . join ( root , ".." , "registry" ) , // vix-site -> registry
24- path . join ( root , "registry" ) , // vix-site -> registry (si copié)
22+ path . join ( root , ".." , ".." , "registry" ) ,
23+ path . join ( root , ".." , "registry" ) ,
24+ path . join ( root , "registry" ) ,
2525 ] ;
2626
27- let registryRoot = "" ;
2827 for ( const c of candidates ) {
2928 if (
3029 fs . existsSync ( path . join ( c , "registry.json" ) ) &&
31- fs . existsSync ( path . join ( c , "index" ) )
30+ fs . existsSync ( path . join ( c , "index" ) ) &&
31+ fs . existsSync ( path . join ( c , ".git" ) )
3232 ) {
33- registryRoot = c ;
34- break ;
33+ try {
34+ sh ( "git fetch origin --prune" , c ) ;
35+ sh ( "git checkout main" , c ) ;
36+ sh ( "git pull --rebase origin main" , c ) ;
37+ } catch { }
38+ return c ;
39+ }
40+ }
41+
42+ const vixClone = path . join (
43+ process . env . HOME || "" ,
44+ ".vix" ,
45+ "registry" ,
46+ "index" ,
47+ ) ;
48+
49+ if (
50+ vixClone &&
51+ fs . existsSync ( path . join ( vixClone , "registry.json" ) ) &&
52+ fs . existsSync ( path . join ( vixClone , "index" ) )
53+ ) {
54+ return vixClone ;
55+ }
56+
57+ const cacheDir = path . join ( root , "tools" , ".cache" ) ;
58+ const cloneDir = path . join ( cacheDir , "registry" ) ;
59+
60+ fs . mkdirSync ( cacheDir , { recursive : true } ) ;
61+
62+ if ( ! fs . existsSync ( path . join ( cloneDir , ".git" ) ) ) {
63+ sh (
64+ "git clone --depth=1 https://github.com/vixcpp/registry.git registry" ,
65+ cacheDir ,
66+ ) ;
67+ } else {
68+ try {
69+ sh ( "git fetch origin --prune" , cloneDir ) ;
70+ sh ( "git checkout main" , cloneDir ) ;
71+ sh ( "git pull --rebase origin main" , cloneDir ) ;
72+ } catch {
73+ // ignore
3574 }
3675 }
3776
77+ if (
78+ fs . existsSync ( path . join ( cloneDir , "registry.json" ) ) &&
79+ fs . existsSync ( path . join ( cloneDir , "index" ) )
80+ ) {
81+ return cloneDir ;
82+ }
83+
84+ return "" ;
85+ }
86+
87+ function main ( ) {
88+ const root = process . cwd ( ) ;
89+
90+ const registryRoot = ensureRegistryRepo ( { root } ) ;
91+
3892 if ( ! registryRoot ) {
3993 console . error ( "build_registry_index: registry repo not found." ) ;
4094 console . error (
41- "Expected a folder containing registry.json and index/ near vix-site ." ,
95+ "Looked for ../registry, ../../registry, ./ registry, ~/.vix/registry/index, or a cached clone ." ,
4296 ) ;
4397 process . exit ( 1 ) ;
4498 }
@@ -54,17 +108,15 @@ function main() {
54108 try {
55109 const e = readJson ( file ) ;
56110 entries . push ( e ) ;
57- } catch {
58- // ignore broken entries
59- }
111+ } catch { }
60112 }
61113
62114 const out = {
63115 meta : {
64116 registryId : registryMeta . id || "vixcpp-registry" ,
65117 specVersion : registryMeta . specVersion || "1.0.0" ,
66118 generatedAt : new Date ( ) . toISOString ( ) ,
67- sourceRepo : registryMeta . homepage || "" ,
119+ sourceRepo : registryMeta . homepage || "https://github.com/vixcpp/registry " ,
68120 indexFormat : registryMeta . index ?. format || "json-per-package" ,
69121 entryCount : entries . length ,
70122 } ,
@@ -77,7 +129,16 @@ function main() {
77129 const outPath = path . join ( outDir , "all.min.json" ) ;
78130 fs . writeFileSync ( outPath , JSON . stringify ( out ) , "utf8" ) ;
79131
80- console . log ( "registry index built:" , outPath , "entries:" , entries . length ) ;
132+ const has071 = JSON . stringify ( out ) . includes ( '"0.7.1"' ) ;
133+ console . log (
134+ "registry index built:" ,
135+ outPath ,
136+ "entries:" ,
137+ entries . length ,
138+ "has_0.7.1:" ,
139+ has071 ,
140+ ) ;
141+ console . log ( "registry source:" , registryRoot ) ;
81142}
82143
83144main ( ) ;
0 commit comments