1+ import { execSync } from 'node:child_process' ;
12import { existsSync , mkdirSync , writeFileSync } from 'node:fs' ;
23import { dirname , join } from 'node:path' ;
34import snakeCase from 'lodash/snakeCase' ;
@@ -6,6 +7,12 @@ import { render as renderTemplate } from 'squirrelly';
67import { CodeGenerator } from '../types' ;
78import { packageTemplatesData , modelTemplatesData } from './templates' ;
89
10+ type TExecCommandError = Error & {
11+ status ?: number ;
12+ stdout ?: Buffer ;
13+ stderr ?: Buffer ;
14+ } ;
15+
916const servicesToTypePrefixMap = {
1017 core : 'TCore' ,
1118 ctp : 'TCtp' ,
@@ -114,7 +121,7 @@ export const newTestModelGenerator: CodeGenerator = {
114121 join ( __dirname , '..' , '..' , '..' , 'core' , 'package.json' )
115122 ) ;
116123
117- // 2. Generate the files
124+ // 2. Generate the model files
118125 const templatesData = {
119126 modelName,
120127 modelCodename,
@@ -188,6 +195,61 @@ export const newTestModelGenerator: CodeGenerator = {
188195 } ) ;
189196 }
190197
198+ // 3. Update the single package preset
199+ if ( generationType === 'standalone' ) {
200+ const presetDirectoryPath = join (
201+ __dirname ,
202+ '..' ,
203+ '..' ,
204+ '..' ,
205+ 'presets' ,
206+ 'all-packages'
207+ ) ;
208+ // Generate the re-export proxy file
209+ writeFileSync (
210+ join ( presetDirectoryPath , 'src' , `${ modelCodename } .ts` ) ,
211+ `export * from '@commercetools-test-data/${ modelCodename } ';`
212+ ) ;
213+ // Update package.json
214+ const presetPackageJson = (
215+ await import ( join ( presetDirectoryPath , 'package.json' ) )
216+ ) . default ;
217+ presetPackageJson . files . push ( modelCodename ) ;
218+ presetPackageJson . preconstruct . entrypoints . push ( `${ modelCodename } .ts` ) ;
219+ presetPackageJson . dependencies [
220+ `@commercetools-test-data/${ modelCodename } `
221+ ] = 'workspace:*' ;
222+ writeFileSync (
223+ join ( presetDirectoryPath , 'package.json' ) ,
224+ JSON . stringify ( presetPackageJson , null , 2 )
225+ ) ;
226+
227+ // Run preconstruct to generate the proxy package.json file
228+ const rootDirectoryPath = join ( __dirname , '..' , '..' , '..' ) ;
229+ try {
230+ execSync ( 'npx preconstruct fix' , {
231+ cwd : rootDirectoryPath ,
232+ } ) ;
233+ } catch ( err ) {
234+ const error = err as TExecCommandError ;
235+ throw new Error (
236+ `Failed to run "preconstruct fix": ${ error . message } - Exit code: ${ error . status } - stdout: ${ error . stdout ?. toString ( ) } - stderr: ${ error . stderr ?. toString ( ) } - cwd: ${ rootDirectoryPath } `
237+ ) ;
238+ }
239+
240+ // Run pnpm install to install the new package
241+ try {
242+ execSync ( 'pnpm install' , {
243+ cwd : rootDirectoryPath ,
244+ } ) ;
245+ } catch ( err ) {
246+ const error = err as TExecCommandError ;
247+ throw new Error (
248+ `Failed to run "pnpm install": ${ error . message } - Exit code: ${ error . status } - stdout: ${ error . stdout ?. toString ( ) } - stderr: ${ error . stderr ?. toString ( ) } - cwd: ${ rootDirectoryPath } `
249+ ) ;
250+ }
251+ }
252+
191253 console . log (
192254 "\n 🚀 All set! We've generated all files. You can now adjust them to your needs."
193255 ) ;
0 commit comments