11#!/usr/bin/env node
22
3- import readline from "readline " ;
3+ import chalk from "chalk " ;
44import { exec } from "child_process" ;
5+ import fs from "fs" ;
6+ import path from "path" ;
7+ import readline from "readline" ;
58import util from "util" ;
6- import chalk from "chalk" ;
9+ import yargs from "yargs" ;
10+ import { hideBin } from "yargs/helpers" ;
711
812/* --- Helpers --- */
913
14+ const run = util . promisify ( exec ) ;
15+
1016const rl = readline . createInterface ( {
1117 input : process . stdin ,
1218 output : process . stdout ,
1319} ) ;
1420
1521function prompt ( question , defaultAnswer ) {
1622 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 ) ) ;
1924 } ) ;
2025}
2126
22- const run = util . promisify ( exec ) ;
27+ /* --- Parse CLI Arguments */
2328
24- /* --- User Input --- */
29+ const args = yargs ( hideBin ( process . argv ) ) . parse ( ) ;
2530
26- const projectName = await prompt ( "Project Name" , "my-stackbit-site" ) ;
27- const repoName = await prompt ( "GitHub Repo" , "stackbit/nextjs-starter" ) ;
31+ /* --- New Project --- */
2832
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 ) ;
3040
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` ) ;
3644
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 ( `
4347🎉 ${ chalk . bold ( "Welcome to Stackbit!" ) } 🎉
4448
4549Run the following commands:
@@ -48,11 +52,36 @@ Run the following commands:
4852 npm run dev
4953
5054When 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.
5357Open 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/
5475` ) ;
76+ return resolve ( true ) ;
77+ } ) ;
78+ }
79+
80+ /* --- Run --- */
5581
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 ( ) ;
5786
5887rl . close ( ) ;
0 commit comments