@@ -6,36 +6,27 @@ import { fileURLToPath } from 'url';
6
6
import { execa } from 'execa' ;
7
7
import Enquirer from 'enquirer' ;
8
8
import { MongoClient } from 'mongodb' ;
9
+ import ora from 'ora' ;
10
+ import logSymbols from 'log-symbols' ;
9
11
10
12
const __dirname = fileURLToPath ( new URL ( '.' , import . meta. url ) ) ;
11
13
12
14
export default new Command ( 'init' )
13
15
. description ( 'Create a basic mantis app' )
14
16
. action ( async ( ) => {
15
- const templatePath = path . resolve ( __dirname , '../templates/mantis-todo' ) ;
16
- const workspacePath = path . join ( process . cwd ( ) , 'mantis-todo' ) ;
17
- // Get db url
18
- const dbUrl = await promptForDbUrl ( ) ;
19
- // Copy over files
20
- await fs . copy ( templatePath , workspacePath , { overwrite : true } ) ;
21
- const envPath = path . join ( workspacePath , 'apps/server/.env.local' ) ;
22
- await fs . ensureFile ( envPath ) ;
23
- await fs . appendFile ( envPath , `\nMONGODB_URI='${ dbUrl } '` ) ;
24
- // Install dependencies
25
- await installDependencies ( {
26
- cwd : workspacePath ,
27
- } ) ;
28
- // Start application
29
- await execa (
30
- 'npx' ,
31
- [
32
- 'nx' ,
33
- 'run-many' ,
34
- '--target=serve' ,
35
- '--projects=web-client,mobile-client,server' ,
36
- ] ,
37
- { cwd : workspacePath , stdio : 'inherit' } ,
17
+ const templateName = 'mantis-todo' ;
18
+ const templatePath = path . resolve (
19
+ __dirname ,
20
+ `../templates/${ templateName } ` ,
38
21
) ;
22
+ const workspaceName = 'mantis-todo' ;
23
+ const workspacePath = path . join ( process . cwd ( ) , workspaceName ) ;
24
+ await fs . ensureDir ( workspacePath ) ;
25
+
26
+ const dbUrl = await promptForDbUrl ( ) ;
27
+ await copyTemplate ( { templatePath, workspacePath, secrets : { dbUrl } } ) ;
28
+ await installDependenciesWithMessage ( workspacePath ) ;
29
+ await startApplications ( workspacePath ) ;
39
30
} ) ;
40
31
41
32
const promptForDbUrl = async ( ) : Promise < string > => {
@@ -60,3 +51,41 @@ const promptForDbUrl = async (): Promise<string> => {
60
51
] ) ;
61
52
return dbUrl ;
62
53
} ;
54
+
55
+ const copyTemplate = async ( {
56
+ templatePath,
57
+ workspacePath,
58
+ secrets : { dbUrl } ,
59
+ } : {
60
+ templatePath : string ;
61
+ workspacePath : string ;
62
+ secrets : { dbUrl : string } ;
63
+ } ) => {
64
+ await fs . copy ( templatePath , workspacePath , { overwrite : true } ) ;
65
+ const envPath = path . join ( workspacePath , 'apps/server/.env.local' ) ;
66
+ await fs . ensureFile ( envPath ) ;
67
+ await fs . appendFile ( envPath , `\nMONGODB_URI='${ dbUrl } '` ) ;
68
+ } ;
69
+
70
+ const installDependenciesWithMessage = async ( workspacePath : string ) => {
71
+ const spinner = ora ( 'Installing dependencies' ) . start ( ) ;
72
+ await installDependencies ( {
73
+ cwd : workspacePath ,
74
+ silent : true ,
75
+ } ) ;
76
+ spinner . succeed ( 'Installed dependencies' ) ;
77
+ } ;
78
+
79
+ const startApplications = async ( workspacePath : string ) => {
80
+ console . log ( `${ logSymbols . info } Starting applications...` ) ;
81
+ await execa (
82
+ 'npx' ,
83
+ [
84
+ 'nx' ,
85
+ 'run-many' ,
86
+ '--target=serve' ,
87
+ '--projects=web-client,mobile-client,server' ,
88
+ ] ,
89
+ { cwd : workspacePath , stdio : 'inherit' } ,
90
+ ) ;
91
+ } ;
0 commit comments