1
1
#!/usr/bin/env node
2
2
3
- import readline from "readline " ;
3
+ import chalk from "chalk " ;
4
4
import { exec } from "child_process" ;
5
+ import fs from "fs" ;
6
+ import path from "path" ;
7
+ import readline from "readline" ;
5
8
import util from "util" ;
6
- import chalk from "chalk" ;
9
+ import yargs from "yargs" ;
10
+ import { hideBin } from "yargs/helpers" ;
7
11
8
12
/* --- Helpers --- */
9
13
14
+ const run = util . promisify ( exec ) ;
15
+
10
16
const rl = readline . createInterface ( {
11
17
input : process . stdin ,
12
18
output : process . stdout ,
13
19
} ) ;
14
20
15
21
function prompt ( question , defaultAnswer ) {
16
22
return new Promise ( ( resolve ) => {
17
- const questionText = `${ chalk . bold ( question ) } (${ defaultAnswer } ): ` ;
18
- rl . question ( questionText , ( input ) => resolve ( input || defaultAnswer ) ) ;
23
+ rl . question ( question , ( input ) => resolve ( input || defaultAnswer ) ) ;
19
24
} ) ;
20
25
}
21
26
22
- const run = util . promisify ( exec ) ;
27
+ /* --- Parse CLI Arguments */
23
28
24
- /* --- User Input --- */
29
+ const args = yargs ( hideBin ( process . argv ) ) . parse ( ) ;
25
30
26
- const projectName = await prompt ( "Project Name" , "my-stackbit-site" ) ;
27
- const repoName = await prompt ( "GitHub Repo" , "stackbit/nextjs-starter" ) ;
31
+ /* --- New Project --- */
28
32
29
- /* --- Run --- */
33
+ async function cloneStarter ( ) {
34
+ // Clone repo
35
+ const projectName = args . _ [ 0 ] ?? "my-stackbit-site" ;
36
+ const repoUrl = `https://github.com/stackbit/nextjs-starter` ;
37
+ const cloneCommand = `git clone --depth=1 ${ repoUrl } ${ projectName } ` ;
38
+ console . log ( `\nCloning into ${ projectName } ...` ) ;
39
+ await run ( cloneCommand ) ;
30
40
31
- // Clone repo
32
- const repoUrl = `https://github.com/${ repoName } ` ;
33
- const cloneCommand = `git clone --depth=1 ${ repoUrl } ${ projectName } ` ;
34
- console . log ( `\nCloning into ${ projectName } ...` ) ;
35
- await run ( cloneCommand ) ;
41
+ // Install dependencies
42
+ console . log ( `Installing dependencies ...` ) ;
43
+ await run ( `cd ${ projectName } && npm install` ) ;
36
44
37
- // Install dependencies
38
- console . log ( `Installing dependencies ...` ) ;
39
- await run ( `cd ${ projectName } && npm install` ) ;
40
-
41
- // Output next steps:
42
- console . log ( `
45
+ // Output next steps:
46
+ console . log ( `
43
47
🎉 ${ chalk . bold ( "Welcome to Stackbit!" ) } 🎉
44
48
45
49
Run the following commands:
@@ -48,11 +52,36 @@ Run the following commands:
48
52
npm run dev
49
53
50
54
When your dev server boots, you'll see an ${ chalk . bgYellow . black . bold (
51
- " app.stackbit.com "
52
- ) } URL in the logs.
55
+ " app.stackbit.com "
56
+ ) } URL in the logs.
53
57
Open this URL in your browser and start building!
58
+ ` ) ;
59
+ }
60
+
61
+ /* --- Existing Project --- */
62
+
63
+ async function integrateStackbit ( ) {
64
+ return new Promise ( async ( resolve ) => {
65
+ const integrate = await prompt ( `
66
+ This looks like an existing project.
67
+ ${ chalk . bold ( "Would you like to install Stackbit in this project?" ) } [Y/n] ` ) ;
68
+
69
+ if ( ! [ "yes" , "y" ] . includes ( integrate ?. toLowerCase ( ) ) ) return resolve ( false ) ;
70
+
71
+ console . log ( `
72
+ Visit the following URL to learn more about the integration process:
73
+
74
+ https://docs.stackbit.com/how-to-guides/site-management/integrate-stackbit/
54
75
` ) ;
76
+ return resolve ( true ) ;
77
+ } ) ;
78
+ }
79
+
80
+ /* --- Run --- */
55
81
56
- /* --- Clean Up --- */
82
+ const packageJsonFilePath = path . join ( process . cwd ( ) , "package.json" ) ;
83
+ const hasPackageJson = fs . existsSync ( packageJsonFilePath ) ;
84
+ const runFunc = hasPackageJson ? integrateStackbit : cloneStarter ;
85
+ await runFunc ( ) ;
57
86
58
87
rl . close ( ) ;
0 commit comments