1
1
import type { Tree } from '@nx/devkit' ;
2
2
import {
3
+ addDependenciesToPackageJson ,
3
4
addProjectConfiguration ,
4
5
formatFiles ,
5
6
generateFiles ,
@@ -15,6 +16,7 @@ import { existsSync } from 'fs';
15
16
import { mkdir , readFile , rm } from 'fs/promises' ;
16
17
import { join } from 'path' ;
17
18
19
+ import packageJson from '../../../package.json' ;
18
20
import type { UpdateApiExecutorSchema } from '../../executors/update-api/schema' ;
19
21
20
22
const tempFolder = join ( process . cwd ( ) , 'tmp' ) ;
@@ -57,7 +59,7 @@ export default async function (
57
59
} ) ;
58
60
59
61
// Update the package.json file
60
- updatePackageJson ( {
62
+ const installDeps = await updatePackageJson ( {
61
63
clientType,
62
64
projectName,
63
65
projectRoot,
@@ -78,7 +80,8 @@ export default async function (
78
80
await rm ( tempFolder , { force : true , recursive : true } ) ;
79
81
80
82
// Return a function that installs the packages
81
- return ( ) => {
83
+ return async ( ) => {
84
+ await installDeps ( ) ;
82
85
installPackagesTask ( tree ) ;
83
86
} ;
84
87
}
@@ -100,9 +103,9 @@ export function normalizeOptions(
100
103
options : OpenApiClientGeneratorSchema ,
101
104
) : NormalizedOptions {
102
105
const name = names ( options . name ) . fileName ;
103
- const projectDirectory = names ( options . directory ) . fileName ;
106
+ const projectDirectory = names ( options . directory ) . fileName . replace ( './' , '' ) ;
104
107
const projectName = name . replace ( new RegExp ( '/' , 'g' ) , '-' ) ;
105
- const projectRoot = join ( projectDirectory , projectName ) ;
108
+ const projectRoot = ` ${ projectDirectory } / ${ projectName } ` ;
106
109
const tagArray = options . tags
107
110
? options . tags . split ( ',' ) . map ( ( s ) => s . trim ( ) )
108
111
: [ 'api' , 'openapi' ] ;
@@ -147,56 +150,51 @@ export function generateNxProject({
147
150
} ;
148
151
149
152
// Create basic project structure
150
- addProjectConfiguration (
151
- tree ,
152
- `${ projectScope } /${ projectName } ` ,
153
- {
154
- projectType : 'library' ,
155
- root : projectRoot ,
156
- sourceRoot : `${ projectRoot } /src` ,
157
- tags : tagArray ,
158
- targets : {
159
- build : {
160
- executor : '@nx/js:tsc' ,
161
- options : {
162
- additionalEntryPoints : [ `${ projectRoot } /src/rq.ts` ] ,
163
- assets : [
164
- {
165
- glob : 'README.md' ,
166
- input : `./${ projectRoot } ` ,
167
- output : '.' ,
168
- } ,
169
- ] ,
170
- main : `${ projectRoot } /src/index.ts` ,
171
- outputPath : `dist/${ projectRoot } ` ,
172
- tsConfig : `${ projectRoot } /tsconfig.lib.json` ,
173
- } ,
174
- outputs : [ '{options.outputPath}' ] ,
175
- } ,
176
- generateApi : {
177
- executor : 'nx:run-commands' ,
178
- options : {
179
- command : `npx @hey-api/openapi-ts -i ./api/spec.yaml -o ./src/generated -c @hey-api/client-${ clientType } -p @tanstack/react-query` ,
180
- cwd : projectRoot ,
181
- } ,
153
+ addProjectConfiguration ( tree , `${ projectScope } /${ projectName } ` , {
154
+ projectType : 'library' ,
155
+ root : projectRoot ,
156
+ sourceRoot : `${ projectRoot } /src` ,
157
+ tags : tagArray ,
158
+ targets : {
159
+ build : {
160
+ executor : '@nx/js:tsc' ,
161
+ options : {
162
+ additionalEntryPoints : [ `${ projectRoot } /src/rq.ts` ] ,
163
+ assets : [
164
+ {
165
+ glob : 'README.md' ,
166
+ input : `./${ projectRoot } ` ,
167
+ output : '.' ,
168
+ } ,
169
+ ] ,
170
+ main : `${ projectRoot } /src/index.ts` ,
171
+ outputPath : `dist/${ projectRoot } ` ,
172
+ tsConfig : `${ projectRoot } /tsconfig.lib.json` ,
182
173
} ,
183
- lint : {
184
- executor : '@nx/eslint:lint' ,
185
- options : {
186
- lintFilePatterns : [
187
- `${ projectRoot } /**/*.ts` ,
188
- `${ projectRoot } /package.json` ,
189
- ] ,
190
- } ,
174
+ outputs : [ '{options.outputPath}' ] ,
175
+ } ,
176
+ generateApi : {
177
+ executor : 'nx:run-commands' ,
178
+ options : {
179
+ command : `npx @hey-api/openapi-ts -i ./api/spec.yaml -o ./src/generated -c @hey-api/client-${ clientType } -p @tanstack/react-query` ,
180
+ cwd : projectRoot ,
191
181
} ,
192
- updateApi : {
193
- executor : './libs/openapi-generator:update-api' ,
194
- options : updateOptions ,
182
+ } ,
183
+ lint : {
184
+ executor : '@nx/eslint:lint' ,
185
+ options : {
186
+ lintFilePatterns : [
187
+ `${ projectRoot } /**/*.ts` ,
188
+ `${ projectRoot } /package.json` ,
189
+ ] ,
195
190
} ,
196
191
} ,
192
+ updateApi : {
193
+ executor : `${ packageJson . name } :update-api` ,
194
+ options : updateOptions ,
195
+ } ,
197
196
} ,
198
- false ,
199
- ) ;
197
+ } ) ;
200
198
201
199
// Create directory structure
202
200
const templatePath = join ( __dirname , 'files' ) ;
@@ -269,7 +267,7 @@ export async function generateApi({
269
267
/**
270
268
* Updates the package.json file to add dependencies and scripts
271
269
*/
272
- export function updatePackageJson ( {
270
+ export async function updatePackageJson ( {
273
271
clientType,
274
272
projectName,
275
273
projectRoot,
@@ -283,46 +281,44 @@ export function updatePackageJson({
283
281
tree : Tree ;
284
282
} ) {
285
283
// Update package.json to add dependencies and scripts
286
- if ( tree . exists ( `${ projectRoot } /package.json` ) ) {
287
- updateJson ( tree , `${ projectRoot } /package.json` , ( json ) => {
288
- json . scripts = {
289
- ...json . scripts ,
290
- generate : 'nx run ' + projectName + ':generateApi' ,
291
- } ;
292
-
293
- // Add the required dependencies
294
- json . dependencies = json . dependencies || { } ;
295
- json . devDependencies = json . devDependencies || { } ;
296
-
297
- // Add the client dependency
298
- if ( clientType === 'fetch' ) {
299
- json . dependencies [ '@hey-api/client-fetch' ] = '^0.9.0' ;
300
- } else if ( clientType === 'axios' ) {
301
- json . dependencies [ '@hey-api/client-axios' ] = '^0.7.0' ;
302
- json . dependencies [ 'axios' ] = '^1.6.0' ;
303
- }
304
-
305
- // Add dev dependency for the generator
306
- json . devDependencies [ '@hey-api/openapi-ts' ] = '^0.66.0' ;
307
-
308
- return json ;
309
- } ) ;
310
- }
284
+ const deps : Record < string , string > =
285
+ clientType === 'fetch'
286
+ ? {
287
+ '@hey-api/client-fetch' : '^0.9.0' ,
288
+ }
289
+ : {
290
+ '@hey-api/client-axios' : '^0.7.0' ,
291
+ axios : '^1.6.0' ,
292
+ } ;
293
+
294
+ const installDeps = addDependenciesToPackageJson (
295
+ tree ,
296
+ deps ,
297
+ { } ,
298
+ join ( projectRoot , 'package.json' ) ,
299
+ ) ;
311
300
312
301
const tsConfigPath = join ( workspaceRoot , 'tsconfig.base.json' ) ;
313
302
if ( existsSync ( tsConfigPath ) ) {
314
303
updateJson ( tree , 'tsconfig.base.json' , ( json ) => {
315
- json . compilerOptions . paths [ `${ projectScope } /${ projectName } ` ] = [
316
- `${ projectRoot } /src/index.ts` ,
304
+ const paths = json . compilerOptions . paths || { } ;
305
+ paths [ `${ projectScope } /${ projectName } ` ] = [
306
+ `./${ projectRoot } /src/index.ts` ,
317
307
] ;
318
- json . compilerOptions . paths [ `${ projectScope } /${ projectName } /rq` ] = [
319
- `${ projectRoot } /src/rq.ts` ,
308
+ paths [ `${ projectScope } /${ projectName } /rq` ] = [
309
+ `./ ${ projectRoot } /src/rq.ts` ,
320
310
] ;
311
+ json . compilerOptions . paths = paths ;
321
312
return json ;
322
313
} ) ;
323
314
} else {
324
- logger . error ( `Failed to find tsconfig.base.json file in ${ workspaceRoot } ` ) ;
315
+ logger . error ( `Failed to find tsconfig.base.json file in ${ workspaceRoot } .` ) ;
316
+ throw new Error (
317
+ `Failed to find tsconfig.base.json file in ${ workspaceRoot } .` ,
318
+ ) ;
325
319
}
320
+
321
+ return installDeps ;
326
322
}
327
323
328
324
/**
0 commit comments