@@ -3,6 +3,9 @@ import { promises as fPromises } from 'fs';
3
3
import Command from '../../base' ;
4
4
import { resolve , join } from 'path' ;
5
5
import fs from 'fs-extra' ;
6
+ import { load , Specification } from '../../models/SpecificationFile' ;
7
+ import { parse } from '../../parser'
8
+ import { exec } from 'child_process' ;
6
9
7
10
export default class NewGlee extends Command {
8
11
static description = 'Creates a new Glee project' ;
@@ -14,16 +17,39 @@ export default class NewGlee extends Command {
14
17
name : Flags . string ( { char : 'n' , description : 'name of the project' , default : 'project' } ) ,
15
18
} ;
16
19
20
+ static args = [
21
+ {
22
+ name : 'file' ,
23
+ description : 'spec path, URL or context-name' ,
24
+ required : true ,
25
+ } ,
26
+ ] ;
17
27
async run ( ) {
18
- const { flags } = await this . parse ( NewGlee ) ; // NOSONAR
28
+ const { args , flags } = await this . parse ( NewGlee ) ; // NOSONAR
19
29
20
30
const projectName = flags . name ;
31
+ // console.log(flags.name , {flags} , {args})
21
32
22
33
const PROJECT_DIRECTORY = join ( process . cwd ( ) , projectName ) ;
23
34
const GLEE_TEMPLATES_DIRECTORY = resolve ( __dirname , '../../../assets/create-glee-app/templates/default' ) ;
24
-
35
+ let operationList : Array < any > = [ ]
36
+ let response :any ;
37
+ let asyncApiFile :any ;
25
38
try {
26
39
await fPromises . mkdir ( PROJECT_DIRECTORY ) ;
40
+ const document = await load ( args . file ) ;
41
+ response = await parse ( this , document )
42
+ const Jsondocument :any = JSON . stringify ( response . document )
43
+ asyncApiFile = response . document . _meta . asyncapi . input ;
44
+
45
+ // console.log({document},{response},{Jsondocument})
46
+ console . log ( response . document . _meta . asyncapi . input , "<<<<<<<this is the response" )
47
+ // console.log(Object.keys(response.document._json.operations))
48
+ operationList = Object . keys ( response . document . _json . operations ) ;
49
+ // console.log( JSON.stringify(response.document), {Jsondocument}, response.document._json.channels['/hello'].publish.operationId)
50
+ // operation_id = response.document._json.channels['/hello'].publish.operationId
51
+ // console.log({operation_id})
52
+
27
53
} catch ( err : any ) {
28
54
switch ( err . code ) {
29
55
case 'EEXIST' :
@@ -40,14 +66,34 @@ export default class NewGlee extends Command {
40
66
}
41
67
}
42
68
43
- try {
69
+
70
+ const createOperationFunction = async ( ) => {
71
+ for ( const fileName of operationList ) {
72
+ await fPromises . writeFile ( `${ PROJECT_DIRECTORY } /functions/${ fileName } .js` , "" ) ;
73
+ await fPromises . copyFile (
74
+ `${ PROJECT_DIRECTORY } /functions/onHello.js` ,
75
+ `${ PROJECT_DIRECTORY } /functions/${ fileName } .js`
76
+ ) ;
77
+ }
78
+ } ;
79
+ try {
44
80
await fs . copy ( GLEE_TEMPLATES_DIRECTORY , PROJECT_DIRECTORY ) ;
45
81
await fPromises . rename ( `${ PROJECT_DIRECTORY } /env` , `${ PROJECT_DIRECTORY } /.env` ) ;
46
82
await fPromises . rename ( `${ PROJECT_DIRECTORY } /gitignore` , `${ PROJECT_DIRECTORY } /.gitignore` ) ;
47
83
await fPromises . rename ( `${ PROJECT_DIRECTORY } /README-template.md` , `${ PROJECT_DIRECTORY } /README.md` ) ;
84
+
85
+ await createOperationFunction ( )
86
+ await fPromises . unlink ( `${ PROJECT_DIRECTORY } /functions/onHello.js` ) ;
87
+
88
+ await fPromises . writeFile ( `${ PROJECT_DIRECTORY } /asyncapi.yaml` , asyncApiFile ) ;
89
+
90
+
48
91
this . log ( `Your project "${ projectName } " has been created successfully!\n\nNext steps:\n\n cd ${ projectName } \n npm install\n npm run dev\n\nAlso, you can already open the project in your favorite editor and start tweaking it.` ) ;
49
92
} catch ( err ) {
50
93
this . error ( `Unable to create the project. Please check the following message for further info about the error:\n\n${ err } ` ) ;
51
94
}
52
95
}
53
- }
96
+ // const parseDocument = async () =>{
97
+
98
+ // }
99
+ }
0 commit comments