1
1
import * as Generator from 'yeoman-generator'
2
+ import { exec } from 'child_process'
3
+
4
+ import { createRequire } from 'node:module' ;
5
+ const require = createRequire ( import . meta. url ) ;
2
6
3
7
interface InputTransport {
4
8
type : string
@@ -18,8 +22,7 @@ interface GeneratorEndpointContext {
18
22
normalizedEndpointNameCap : string
19
23
}
20
24
21
-
22
- module . exports = class extends Generator < { rootPath : string } > {
25
+ export default class extends Generator . default {
23
26
props : {
24
27
// Current ea-framework version in package.json
25
28
frameworkVersion : string
@@ -43,7 +46,7 @@ module.exports = class extends Generator<{rootPath: string}> {
43
46
standalone = process . env . EXTERNAL_ADAPTER_GENERATOR_STANDALONE === 'true'
44
47
45
48
constructor ( args , opts ) {
46
- super ( args , opts )
49
+ super ( args , opts , { customInstallTask : true } )
47
50
this . argument ( 'rootPath' , {
48
51
type : String ,
49
52
required : false ,
@@ -85,7 +88,7 @@ module.exports = class extends Generator<{rootPath: string}> {
85
88
this . props = {
86
89
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
87
90
// @ts -ignore
88
- frameworkVersion : ( await import ( '../../../package.json' ) ) . version ,
91
+ frameworkVersion : ( await require ( '../../../package.json' ) ) . version ,
89
92
adapterName,
90
93
endpoints,
91
94
endpointNames,
@@ -117,22 +120,22 @@ module.exports = class extends Generator<{rootPath: string}> {
117
120
baseFiles . forEach ( fileName => {
118
121
this . fs . copyTpl (
119
122
this . templatePath ( fileName ) ,
120
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /${ fileName } ` ) ,
123
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /${ fileName } ` ) ,
121
124
{ ...this . props , standalone : this . standalone }
122
125
)
123
126
} )
124
127
125
128
// copy main index.ts file
126
129
this . fs . copyTpl (
127
130
this . templatePath ( `src/index.ts.ejs` ) ,
128
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/index.ts` ) ,
131
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/index.ts` ) ,
129
132
this . props
130
133
)
131
134
132
135
// Copy overrides
133
136
this . fs . copyTpl (
134
137
this . templatePath ( 'src/config/overrides.json' ) ,
135
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/config/overrides.json` ) ,
138
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/config/overrides.json` ) ,
136
139
this . props ,
137
140
)
138
141
@@ -142,7 +145,7 @@ module.exports = class extends Generator<{rootPath: string}> {
142
145
// Router endpoints
143
146
this . fs . copyTpl (
144
147
this . templatePath ( 'src/endpoint/endpoint-router.ts.ejs' ) ,
145
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/endpoint/${ inputEndpointName } .ts` ) ,
148
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/endpoint/${ inputEndpointName } .ts` ) ,
146
149
{
147
150
inputEndpointName,
148
151
inputTransports,
@@ -155,15 +158,15 @@ module.exports = class extends Generator<{rootPath: string}> {
155
158
inputTransports . forEach ( transport => {
156
159
this . fs . copyTpl (
157
160
this . templatePath ( `src/transport/${ transport . type } .ts.ejs` ) ,
158
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/transport/${ inputEndpointName } -${ transport . type } .ts` ) ,
161
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/transport/${ inputEndpointName } -${ transport . type } .ts` ) ,
159
162
{ inputEndpointName, includeComments : this . props . includeComments } ,
160
163
)
161
164
} )
162
165
} else {
163
166
// Single transport endpoints
164
167
this . fs . copyTpl (
165
168
this . templatePath ( 'src/endpoint/endpoint.ts.ejs' ) ,
166
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/endpoint/${ inputEndpointName } .ts` ) ,
169
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/endpoint/${ inputEndpointName } .ts` ) ,
167
170
{
168
171
inputEndpointName,
169
172
inputTransports,
@@ -175,7 +178,7 @@ module.exports = class extends Generator<{rootPath: string}> {
175
178
176
179
this . fs . copyTpl (
177
180
this . templatePath ( `src/transport/${ inputTransports [ 0 ] . type } .ts.ejs` ) ,
178
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/transport/${ inputEndpointName } .ts` ) ,
181
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/transport/${ inputEndpointName } .ts` ) ,
179
182
{ inputEndpointName, includeComments : this . props . includeComments } ,
180
183
)
181
184
}
@@ -184,7 +187,7 @@ module.exports = class extends Generator<{rootPath: string}> {
184
187
// Create endpoint barrel file
185
188
this . fs . copyTpl (
186
189
this . templatePath ( `src/endpoint/index.ts.ejs` ) ,
187
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/endpoint/index.ts` ) ,
190
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/endpoint/index.ts` ) ,
188
191
{ endpoints : Object . values ( this . props . endpoints ) } ,
189
192
)
190
193
@@ -196,7 +199,7 @@ module.exports = class extends Generator<{rootPath: string}> {
196
199
// Copy config
197
200
this . fs . copyTpl (
198
201
this . templatePath ( 'src/config/index.ts.ejs' ) ,
199
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /src/config/index.ts` ) ,
202
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /src/config/index.ts` ) ,
200
203
{ setBgExecuteMsEnv : customBgEndpoints . length }
201
204
)
202
205
@@ -205,7 +208,7 @@ module.exports = class extends Generator<{rootPath: string}> {
205
208
if ( httpEndpoints . length ) {
206
209
this . fs . copyTpl (
207
210
this . templatePath ( `test/adapter.test.ts.ejs` ) ,
208
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /test/integration/adapter.test.ts` ) ,
211
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /test/integration/adapter.test.ts` ) ,
209
212
{ endpoints : httpEndpoints , transportName : 'rest' , setBgExecuteMsEnv : false } ,
210
213
)
211
214
}
@@ -218,7 +221,7 @@ module.exports = class extends Generator<{rootPath: string}> {
218
221
}
219
222
this . fs . copyTpl (
220
223
this . templatePath ( `test/adapter-ws.test.ts.ejs` ) ,
221
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /test/integration/${ fileName } ` ) ,
224
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /test/integration/${ fileName } ` ) ,
222
225
{ endpoints : wsEndpoints } ,
223
226
)
224
227
}
@@ -234,7 +237,7 @@ module.exports = class extends Generator<{rootPath: string}> {
234
237
}
235
238
this . fs . copyTpl (
236
239
this . templatePath ( `test/adapter.test.ts.ejs` ) ,
237
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /test/integration/${ fileName } ` ) ,
240
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /test/integration/${ fileName } ` ) ,
238
241
{ endpoints : customFgEndpoints , transportName : 'customfg' , setBgExecuteMsEnv : false } ,
239
242
)
240
243
}
@@ -245,15 +248,15 @@ module.exports = class extends Generator<{rootPath: string}> {
245
248
}
246
249
this . fs . copyTpl (
247
250
this . templatePath ( `test/adapter.test.ts.ejs` ) ,
248
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /test/integration/${ fileName } ` ) ,
251
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /test/integration/${ fileName } ` ) ,
249
252
{ endpoints : customBgEndpoints , transportName : 'custombg' , setBgExecuteMsEnv : true } ,
250
253
)
251
254
}
252
255
253
256
// Copy test fixtures
254
257
this . fs . copyTpl (
255
258
this . templatePath ( `test/fixtures.ts.ejs` ) ,
256
- this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /test/integration/fixtures.ts` ) ,
259
+ this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /test/integration/fixtures.ts` ) ,
257
260
{
258
261
includeWsFixtures : wsEndpoints . length > 0 ,
259
262
includeHttpFixtures : httpEndpoints . length > 0 || customBgEndpoints . length > 0 || customFgEndpoints . length > 0 ,
@@ -290,17 +293,17 @@ module.exports = class extends Generator<{rootPath: string}> {
290
293
pkgJson . scripts [ 'test' ] = 'EA_PORT=0 METRICS_ENABLED=false jest --updateSnapshot'
291
294
}
292
295
293
- this . fs . extendJSON ( this . destinationPath ( `${ this . options . rootPath } /${ this . props . adapterName } /package.json` ) , pkgJson )
296
+ this . fs . extendJSON ( this . destinationPath ( `${ this . args [ 0 ] } /${ this . props . adapterName } /package.json` ) , pkgJson )
294
297
}
295
298
296
299
// install stage is used to run npm or yarn install scripts
297
- install ( ) {
298
- this . yarnInstall ( [ ] , { cwd : ` ${ this . options . rootPath } /${ this . props . adapterName } `} )
300
+ async install ( ) {
301
+ await exec ( `yarn install -- cwd ${ this . args [ 0 ] } /${ this . props . adapterName } `) ;
299
302
}
300
303
301
304
// end is the last stage. can be used for messages or cleanup
302
305
end ( ) {
303
- this . log ( `🚀 Adapter '${ this . props . adapterName } ' was successfully created. 📍${ this . options . rootPath } /${ this . props . adapterName } ` )
306
+ this . log ( `🚀 Adapter '${ this . props . adapterName } ' was successfully created. 📍${ this . args [ 0 ] } /${ this . props . adapterName } ` )
304
307
}
305
308
306
309
private async _promptAdapterName ( ) : Promise < string > {
0 commit comments