@@ -5,6 +5,8 @@ import * as path from 'path';
5
5
import { version } from '../../package.json' ;
6
6
import * as ora from 'ora' ;
7
7
import { exec } from 'child_process' ;
8
+ import * as nunjucks from 'nunjucks' ;
9
+ import * as glob from 'glob' ;
8
10
9
11
const validators : Record <
10
12
string ,
@@ -73,6 +75,7 @@ export class InitCommand extends Command {
73
75
name : options . projectName ,
74
76
dependencies : {
75
77
'@vulcan-sql/core' : options . version ,
78
+ // TODO: Install build/serve package when they are ready
76
79
} ,
77
80
} ,
78
81
null ,
@@ -84,6 +87,13 @@ export class InitCommand extends Command {
84
87
installSpinner . start ( 'Installing dependencies...' ) ;
85
88
await this . execAndWait ( `yarn --silent` , projectPath ) ;
86
89
installSpinner . succeed ( `Dependencies have been installed.` ) ;
90
+ installSpinner . start ( 'Writing initial content...' ) ;
91
+ await this . addInitFiles ( projectPath , options ) ;
92
+ installSpinner . succeed ( 'Initial done.' ) ;
93
+
94
+ this . logger . info (
95
+ `Project has been initialized. Run "cd ${ projectPath } && vulcan start" to start the server.`
96
+ ) ;
87
97
} catch ( e ) {
88
98
installSpinner . fail ( ) ;
89
99
throw e ;
@@ -102,4 +112,36 @@ export class InitCommand extends Command {
102
112
} ) ;
103
113
} ) ;
104
114
}
115
+
116
+ private async addInitFiles ( projectPath : string , options : InitCommandOptions ) {
117
+ const files = await this . listFiles (
118
+ path . resolve ( __dirname , '..' , 'schemas' , 'init' , '**/*.*' )
119
+ ) ;
120
+ for ( const file of files ) {
121
+ const relativePath = path . relative (
122
+ path . resolve ( __dirname , '..' , 'schemas' , 'init' ) ,
123
+ file
124
+ ) ;
125
+ let templateContent = await fs . readFile ( file , 'utf8' ) ;
126
+ if ( path . extname ( file ) === '.yaml' ) {
127
+ // Only render yaml files because sql files have some template scripts which are used by Vulcan.
128
+ templateContent = nunjucks . renderString ( templateContent , {
129
+ options,
130
+ } ) ;
131
+ }
132
+ const targetPath = path . resolve ( projectPath , relativePath ) ;
133
+ const dir = path . dirname ( targetPath ) ;
134
+ await fs . mkdir ( dir , { recursive : true } ) ;
135
+ await fs . writeFile ( targetPath , templateContent , 'utf8' ) ;
136
+ }
137
+ }
138
+
139
+ private async listFiles ( path : string ) {
140
+ return new Promise < string [ ] > ( ( resolve , reject ) => {
141
+ glob ( path , ( err , files ) => {
142
+ if ( err ) return reject ( err ) ;
143
+ return resolve ( files ) ;
144
+ } ) ;
145
+ } ) ;
146
+ }
105
147
}
0 commit comments