1- import { appendFileSync , readdirSync , statSync , writeFileSync } from 'node:fs'
1+ import {
2+ appendFileSync ,
3+ readdirSync ,
4+ readFileSync ,
5+ statSync ,
6+ writeFileSync ,
7+ } from 'node:fs'
28import { join } from 'node:path'
39
410const GENERATED_PATH = 'packages_generated'
@@ -12,6 +18,28 @@ const toPascal = (s: string) =>
1218
1319const toSlug = ( s : string ) => s . replace ( / _ / g, '-' )
1420
21+ /**
22+ * Extract real export names from package's index.gen.ts
23+ * This ensures we use the exact same names as exported by the package
24+ */
25+ const getExportsFromPackage = ( packagePath : string ) : string [ ] => {
26+ const indexPath = join ( packagePath , 'src' , 'index.gen.ts' )
27+ try {
28+ const content = readFileSync ( indexPath , 'utf8' )
29+ const exportPattern = / e x p o r t \* a s ( \w + ) f r o m / g
30+ const exports : string [ ] = [ ]
31+ let match
32+
33+ while ( ( match = exportPattern . exec ( content ) ) !== null ) {
34+ exports . push ( match [ 1 ] )
35+ }
36+
37+ return exports
38+ } catch ( err : unknown ) {
39+ throw new Error ( `Error reading exports from ${ indexPath } : ${ err . message } ` )
40+ }
41+ }
42+
1543const services = readdirSync ( GENERATED_PATH ) . filter ( folder => {
1644 const fullPath = join ( GENERATED_PATH , folder )
1745
@@ -29,34 +57,38 @@ let importsOutput = ''
2957for ( const service of services ) {
3058 const slug = toSlug ( service )
3159 const pascal = toPascal ( service )
32- const srcPath = join ( GENERATED_PATH , service , 'src' )
60+ const packagePath = join ( GENERATED_PATH , service )
3361
34- let versions : string [ ] = [ ]
62+ // Get real exports from the package
63+ let exportedNames : string [ ] = [ ]
3564 try {
36- versions = readdirSync ( srcPath ) . filter ( vFolder => {
37- const vPath = join ( srcPath , vFolder )
38-
39- return statSync ( vPath ) . isDirectory ( ) && / ^ v [ 0 - 9 a - z ] + $ / i. test ( vFolder )
40- } )
65+ exportedNames = getExportsFromPackage ( packagePath )
4166 } catch ( err : unknown ) {
4267 throw new Error (
43- `Error: missing or unreadable 'src' folder for package '${ service } ': ${ err . message } ` ,
68+ `Error getting exports for package '${ service } ': ${ err . message } ` ,
4469 )
4570 }
4671
4772 writeFileSync ( OUTPUT_PATH , AUTO_GENERATE_MESSAGE )
4873
49- if ( versions . length > 0 ) {
74+ if ( exportedNames . length > 0 ) {
5075 const imports : string [ ] = [ ]
5176 const versionsImport : string [ ] = [ ]
5277 const mappings : string [ ] = [ ]
5378
54- for ( const version of versions ) {
55- const importName = `${ pascal } ${ version } `
56- versionsImport . push ( `${ importName } ` )
57- mappings . push ( ` ${ version } : ${ importName } ,` )
79+ for ( const exportName of exportedNames ) {
80+ // Extract version from export name (e.g., K8Sv1 -> v1, S2SVpnv1alpha1 -> v1alpha1)
81+ // Version must start with lowercase 'v' followed by digits
82+ const versionMatch = exportName . match ( / ( v \d + [ a - z ] * \d * ) $ / )
83+ if ( versionMatch ) {
84+ const version = versionMatch [ 1 ]
85+ versionsImport . push ( exportName )
86+ mappings . push ( ` ${ version } : ${ exportName } ,` )
87+ }
5888 }
59- imports . push ( `import { ${ versionsImport } } from '@scaleway/sdk-${ slug } '` )
89+ imports . push (
90+ `import { ${ versionsImport . join ( ', ' ) } } from '@scaleway/sdk-${ slug } '` ,
91+ )
6092
6193 importsOutput += `${ imports . join ( '\n' ) } \n`
6294 const importedNames = imports
0 commit comments