1
1
#!/usr/bin/env node
2
2
3
- import chalk from ' chalk'
4
- import { exec } from ' child_process'
5
- import fs from ' fs-extra'
6
- import path from ' path'
7
- import readline from ' readline'
8
- import util from ' util'
9
- import yargs from ' yargs'
10
- import { hideBin } from ' yargs/helpers'
3
+ import chalk from " chalk" ;
4
+ import { exec } from " child_process" ;
5
+ import fs from " fs-extra" ;
6
+ import path from " path" ;
7
+ import readline from " readline" ;
8
+ import util from " util" ;
9
+ import yargs from " yargs" ;
10
+ import { hideBin } from " yargs/helpers" ;
11
11
12
- import config from ' ./config.js'
12
+ import config from " ./config.js" ;
13
13
14
14
/* --- Helpers --- */
15
15
16
- const run = util . promisify ( exec )
16
+ const run = util . promisify ( exec ) ;
17
17
18
18
const rl = readline . createInterface ( {
19
19
input : process . stdin ,
20
20
output : process . stdout ,
21
- } )
21
+ } ) ;
22
22
23
23
function prompt ( question , defaultAnswer ) {
24
24
return new Promise ( ( resolve ) => {
25
- rl . question ( question , ( input ) => resolve ( input || defaultAnswer ) )
26
- } )
25
+ rl . question ( question , ( input ) => resolve ( input || defaultAnswer ) ) ;
26
+ } ) ;
27
27
}
28
28
29
29
function getDirName ( defaultDirName ) {
30
- let dirName = args . _ [ 0 ] ?? defaultDirName
31
- if ( fs . existsSync ( dirName ) ) dirName += `-${ timestamp } `
32
- return dirName
30
+ let dirName = args . _ [ 0 ] ?? defaultDirName ;
31
+ if ( fs . existsSync ( dirName ) ) dirName += `-${ timestamp } ` ;
32
+ return dirName ;
33
33
}
34
34
35
35
async function installDependencies ( dirName ) {
36
- console . log ( `Installing dependencies ...` )
37
- await run ( `cd ${ dirName } && npm install` )
36
+ console . log ( `Installing dependencies ...` ) ;
37
+ await run ( `cd ${ dirName } && npm install` ) ;
38
38
}
39
39
40
40
async function initGit ( dirName ) {
41
- console . log ( `Setting up Git ...` )
41
+ console . log ( `Setting up Git ...` ) ;
42
42
// remove .git folder
43
- await fs . removeSync ( `${ dirName } / .git` )
43
+ await fs . removeSync ( `${ dirName } / .git` ) ;
44
44
await run (
45
45
`cd ${ dirName } && git init && git add . && git commit -m "New Stackbit project"`
46
- )
46
+ ) ;
47
47
}
48
48
/**
49
49
* Given a version string, compare it to a control version. Returns:
@@ -57,138 +57,138 @@ async function initGit(dirName) {
57
57
*/
58
58
function compareVersion ( version , control ) {
59
59
// References
60
- let returnValue = 0
60
+ let returnValue = 0 ;
61
61
// Return 0 if the versions match.
62
- if ( version === control ) return returnValue
62
+ if ( version === control ) return returnValue ;
63
63
// Break the versions into arrays of integers.
64
- const getVersionParts = ( str ) => str . split ( '.' ) . map ( ( v ) => parseInt ( v ) )
65
- const versionParts = getVersionParts ( version )
66
- const controlParts = getVersionParts ( control )
64
+ const getVersionParts = ( str ) => str . split ( "." ) . map ( ( v ) => parseInt ( v ) ) ;
65
+ const versionParts = getVersionParts ( version ) ;
66
+ const controlParts = getVersionParts ( control ) ;
67
67
// Loop and compare each item.
68
68
controlParts . every ( ( controlPart , idx ) => {
69
69
// If the versions are equal at this part, we move on to the next part.
70
- if ( versionParts [ idx ] === controlPart ) return true
70
+ if ( versionParts [ idx ] === controlPart ) return true ;
71
71
// Otherwise, set the return value, then break out of the loop.
72
- returnValue = versionParts [ idx ] > controlPart ? 1 : - 1
73
- return false
74
- } )
75
- return returnValue
72
+ returnValue = versionParts [ idx ] > controlPart ? 1 : - 1 ;
73
+ return false ;
74
+ } ) ;
75
+ return returnValue ;
76
76
}
77
77
78
78
/* --- Parse CLI Arguments */
79
79
80
80
const args = yargs ( hideBin ( process . argv ) )
81
- . option ( ' starter' , {
82
- alias : 's' ,
83
- describe : ' Choose a starter' ,
81
+ . option ( " starter" , {
82
+ alias : "s" ,
83
+ describe : " Choose a starter" ,
84
84
choices : config . starters . map ( ( s ) => s . name ) ,
85
85
} )
86
- . option ( ' example' , {
87
- alias : 'e' ,
88
- describe : ' Start from an example' ,
86
+ . option ( " example" , {
87
+ alias : "e" ,
88
+ describe : " Start from an example" ,
89
89
choices : config . examples . directories ,
90
90
} )
91
91
. help ( )
92
- . parse ( )
92
+ . parse ( ) ;
93
93
94
94
/* --- References --- */
95
95
96
96
const starter = config . starters . find (
97
97
( s ) => s . name === ( args . starter ?? config . defaults . starter . name )
98
- )
98
+ ) ;
99
99
100
100
// Current time in seconds.
101
- const timestamp = Math . round ( new Date ( ) . getTime ( ) / 1000 )
101
+ const timestamp = Math . round ( new Date ( ) . getTime ( ) / 1000 ) ;
102
102
103
103
/* --- New Project from Starter --- */
104
104
105
105
async function cloneStarter ( ) {
106
106
// Set references
107
- const dirName = getDirName ( config . defaults . dirName )
107
+ const dirName = getDirName ( config . defaults . dirName ) ;
108
108
109
109
// Clone repo
110
- const cloneCommand = `git clone --depth=1 ${ starter . repoUrl } ${ dirName } `
111
- console . log ( `\nCreating new project in ${ dirName } ...` )
112
- await run ( cloneCommand )
110
+ const cloneCommand = `git clone --depth=1 ${ starter . repoUrl } ${ dirName } ` ;
111
+ console . log ( `\nCreating new project in ${ dirName } ...` ) ;
112
+ await run ( cloneCommand ) ;
113
113
114
114
// Project Setup
115
- await installDependencies ( dirName )
116
- await initGit ( dirName )
115
+ await installDependencies ( dirName ) ;
116
+ await initGit ( dirName ) ;
117
117
118
118
// Output next steps:
119
119
console . log ( `
120
- 🎉 ${ chalk . bold ( ' Welcome to Stackbit!' ) } 🎉
120
+ 🎉 ${ chalk . bold ( " Welcome to Stackbit!" ) } 🎉
121
121
122
122
Follow the instructions for getting Started here:
123
123
124
124
${ starter . repoUrl } #readme
125
- ` )
125
+ ` ) ;
126
126
}
127
127
128
128
/* --- New Project from Example --- */
129
129
130
130
async function cloneExample ( ) {
131
- const gitResult = await run ( ' git --version' )
132
- const gitVersionMatch = gitResult . stdout . match ( / \d + \. \d + \. \d + / )
131
+ const gitResult = await run ( " git --version" ) ;
132
+ const gitVersionMatch = gitResult . stdout . match ( / \d + \. \d + \. \d + / ) ;
133
133
if ( ! gitVersionMatch || ! gitVersionMatch [ 0 ] ) {
134
134
console . error (
135
135
`Cannot determine git version, which is required for starting from an example.` ,
136
136
`\nPlease report this:` ,
137
137
chalk . underline (
138
- ' https://github.com/stackbit/create-stackbit-app/issues/new'
138
+ " https://github.com/stackbit/create-stackbit-app/issues/new"
139
139
)
140
- )
141
- process . exit ( 1 )
140
+ ) ;
141
+ process . exit ( 1 ) ;
142
142
}
143
143
if ( compareVersion ( gitVersionMatch [ 0 ] , config . minGitVersion ) < 0 ) {
144
144
console . error (
145
145
`Starting from an example requires git version ${ config . minGitVersion } or later.` ,
146
- ' Please upgrade'
147
- )
148
- process . exit ( 1 )
146
+ " Please upgrade"
147
+ ) ;
148
+ process . exit ( 1 ) ;
149
149
}
150
150
151
- const dirName = getDirName ( args . example )
152
- const tmpDir = `__tmp${ timestamp } __`
153
- console . log ( `\nCreating new project in ${ dirName } ...` )
151
+ const dirName = getDirName ( args . example ) ;
152
+ const tmpDir = `__tmp${ timestamp } __` ;
153
+ console . log ( `\nCreating new project in ${ dirName } ...` ) ;
154
154
155
155
try {
156
156
// Sparse clone the monorepo.
157
157
await run (
158
158
`git clone --depth 1 --filter=blob:none --sparse ${ config . examples . repoUrl } ${ tmpDir } `
159
- )
159
+ ) ;
160
160
// Checkout just the example dir.
161
- await run ( `cd ${ tmpDir } && git sparse-checkout set ${ args . example } ` )
161
+ await run ( `cd ${ tmpDir } && git sparse-checkout set ${ args . example } ` ) ;
162
162
163
163
// move out into a new directory.
164
- await fs . moveSync ( `${ tmpDir } /${ args . example } ` , dirName )
164
+ await fs . moveSync ( `${ tmpDir } /${ args . example } ` , dirName ) ;
165
165
166
166
// Delete the clone.
167
- await fs . removeSync ( tmpDir )
167
+ await fs . removeSync ( tmpDir ) ;
168
168
169
169
// Project Setup
170
- await installDependencies ( dirName )
171
- await initGit ( dirName )
170
+ await installDependencies ( dirName ) ;
171
+ await initGit ( dirName ) ;
172
172
} catch ( err ) {
173
- if ( fs . existsSync ( dirName ) ) await fs . remove ( dirName )
173
+ if ( fs . existsSync ( dirName ) ) await fs . remove ( dirName ) ;
174
174
if ( fs . existsSync ( tmpDir ) )
175
175
// remove temp directory
176
176
await fs . remove ( tmpDir , ( err ) => {
177
177
if ( err ) {
178
- console . log ( err )
178
+ console . log ( err ) ;
179
179
}
180
- } )
181
- process . exit ( 1 )
180
+ } ) ;
181
+ process . exit ( 1 ) ;
182
182
}
183
183
184
184
// Output next steps:
185
185
console . log ( `
186
- 🎉 ${ chalk . bold ( ' Your example project is ready!' ) } 🎉
186
+ 🎉 ${ chalk . bold ( " Your example project is ready!" ) } 🎉
187
187
188
188
Follow the instructions and learn more about the example here:
189
189
190
190
${ config . examples . repoUrl } /tree/main/${ args . example } #readme
191
- ` )
191
+ ` ) ;
192
192
}
193
193
194
194
/* --- Existing Project --- */
@@ -197,37 +197,37 @@ async function integrateStackbit() {
197
197
return new Promise ( async ( resolve ) => {
198
198
const integrate = await prompt ( `
199
199
This looks like an existing project.
200
- ${ chalk . bold ( ' Would you like to install Stackbit in this project?' ) } [Y/n] ` )
200
+ ${ chalk . bold ( " Would you like to install Stackbit in this project?" ) } [Y/n] ` ) ;
201
201
202
- if ( ! [ ' yes' , 'y' ] . includes ( integrate ?. toLowerCase ( ) ) ) return resolve ( false )
202
+ if ( ! [ " yes" , "y" ] . includes ( integrate ?. toLowerCase ( ) ) ) return resolve ( false ) ;
203
203
204
204
console . log ( `
205
205
Visit the following URL to learn more about the integration process:
206
206
207
207
https://docs.stackbit.com/how-to-guides/site-management/integrate-stackbit/
208
- ` )
209
- return resolve ( true )
210
- } )
208
+ ` ) ;
209
+ return resolve ( true ) ;
210
+ } ) ;
211
211
}
212
212
213
213
/* --- Run --- */
214
214
215
215
async function doCreate ( ) {
216
216
// If the current directory has a package.json file, we assume we're in an
217
217
// active project, and will not create a new project.
218
- const packageJsonFilePath = path . join ( process . cwd ( ) , ' package.json' )
219
- if ( fs . existsSync ( packageJsonFilePath ) ) return integrateStackbit ( )
218
+ const packageJsonFilePath = path . join ( process . cwd ( ) , " package.json" ) ;
219
+ if ( fs . existsSync ( packageJsonFilePath ) ) return integrateStackbit ( ) ;
220
220
// If both starter and example were specified, throw an error message.
221
221
if ( args . starter && args . example ) {
222
- console . error ( ' [ERROR] Cannot specify a starter and an example.' )
223
- process . exit ( 1 )
222
+ console . error ( " [ERROR] Cannot specify a starter and an example." ) ;
223
+ process . exit ( 1 ) ;
224
224
}
225
225
// Start from an example if specified.
226
- if ( args . example ) return cloneExample ( )
226
+ if ( args . example ) return cloneExample ( ) ;
227
227
// Otherwise, use a starter, which falls back to the default if not set.
228
- return cloneStarter ( )
228
+ return cloneStarter ( ) ;
229
229
}
230
230
231
- await doCreate ( )
231
+ await doCreate ( ) ;
232
232
233
- rl . close ( )
233
+ rl . close ( ) ;
0 commit comments