Skip to content

Commit 3f6e96e

Browse files
committed
Formatting
1 parent b7c76a2 commit 3f6e96e

File tree

1 file changed

+84
-84
lines changed

1 file changed

+84
-84
lines changed

index.js

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
#!/usr/bin/env node
22

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";
1111

12-
import config from './config.js'
12+
import config from "./config.js";
1313

1414
/* --- Helpers --- */
1515

16-
const run = util.promisify(exec)
16+
const run = util.promisify(exec);
1717

1818
const rl = readline.createInterface({
1919
input: process.stdin,
2020
output: process.stdout,
21-
})
21+
});
2222

2323
function prompt(question, defaultAnswer) {
2424
return new Promise((resolve) => {
25-
rl.question(question, (input) => resolve(input || defaultAnswer))
26-
})
25+
rl.question(question, (input) => resolve(input || defaultAnswer));
26+
});
2727
}
2828

2929
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;
3333
}
3434

3535
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`);
3838
}
3939

4040
async function initGit(dirName) {
41-
console.log(`Setting up Git ...`)
41+
console.log(`Setting up Git ...`);
4242
// remove .git folder
43-
await fs.removeSync(`${dirName} / .git`)
43+
await fs.removeSync(`${dirName} / .git`);
4444
await run(
4545
`cd ${dirName} && git init && git add . && git commit -m "New Stackbit project"`
46-
)
46+
);
4747
}
4848
/**
4949
* Given a version string, compare it to a control version. Returns:
@@ -57,138 +57,138 @@ async function initGit(dirName) {
5757
*/
5858
function compareVersion(version, control) {
5959
// References
60-
let returnValue = 0
60+
let returnValue = 0;
6161
// Return 0 if the versions match.
62-
if (version === control) return returnValue
62+
if (version === control) return returnValue;
6363
// 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);
6767
// Loop and compare each item.
6868
controlParts.every((controlPart, idx) => {
6969
// 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;
7171
// 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;
7676
}
7777

7878
/* --- Parse CLI Arguments */
7979

8080
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",
8484
choices: config.starters.map((s) => s.name),
8585
})
86-
.option('example', {
87-
alias: 'e',
88-
describe: 'Start from an example',
86+
.option("example", {
87+
alias: "e",
88+
describe: "Start from an example",
8989
choices: config.examples.directories,
9090
})
9191
.help()
92-
.parse()
92+
.parse();
9393

9494
/* --- References --- */
9595

9696
const starter = config.starters.find(
9797
(s) => s.name === (args.starter ?? config.defaults.starter.name)
98-
)
98+
);
9999

100100
// Current time in seconds.
101-
const timestamp = Math.round(new Date().getTime() / 1000)
101+
const timestamp = Math.round(new Date().getTime() / 1000);
102102

103103
/* --- New Project from Starter --- */
104104

105105
async function cloneStarter() {
106106
// Set references
107-
const dirName = getDirName(config.defaults.dirName)
107+
const dirName = getDirName(config.defaults.dirName);
108108

109109
// 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);
113113

114114
// Project Setup
115-
await installDependencies(dirName)
116-
await initGit(dirName)
115+
await installDependencies(dirName);
116+
await initGit(dirName);
117117

118118
// Output next steps:
119119
console.log(`
120-
🎉 ${chalk.bold('Welcome to Stackbit!')} 🎉
120+
🎉 ${chalk.bold("Welcome to Stackbit!")} 🎉
121121
122122
Follow the instructions for getting Started here:
123123
124124
${starter.repoUrl}#readme
125-
`)
125+
`);
126126
}
127127

128128
/* --- New Project from Example --- */
129129

130130
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+/);
133133
if (!gitVersionMatch || !gitVersionMatch[0]) {
134134
console.error(
135135
`Cannot determine git version, which is required for starting from an example.`,
136136
`\nPlease report this:`,
137137
chalk.underline(
138-
'https://github.com/stackbit/create-stackbit-app/issues/new'
138+
"https://github.com/stackbit/create-stackbit-app/issues/new"
139139
)
140-
)
141-
process.exit(1)
140+
);
141+
process.exit(1);
142142
}
143143
if (compareVersion(gitVersionMatch[0], config.minGitVersion) < 0) {
144144
console.error(
145145
`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);
149149
}
150150

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} ...`);
154154

155155
try {
156156
// Sparse clone the monorepo.
157157
await run(
158158
`git clone --depth 1 --filter=blob:none --sparse ${config.examples.repoUrl} ${tmpDir}`
159-
)
159+
);
160160
// 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}`);
162162

163163
// move out into a new directory.
164-
await fs.moveSync(`${tmpDir}/${args.example}`, dirName)
164+
await fs.moveSync(`${tmpDir}/${args.example}`, dirName);
165165

166166
// Delete the clone.
167-
await fs.removeSync(tmpDir)
167+
await fs.removeSync(tmpDir);
168168

169169
// Project Setup
170-
await installDependencies(dirName)
171-
await initGit(dirName)
170+
await installDependencies(dirName);
171+
await initGit(dirName);
172172
} catch (err) {
173-
if (fs.existsSync(dirName)) await fs.remove(dirName)
173+
if (fs.existsSync(dirName)) await fs.remove(dirName);
174174
if (fs.existsSync(tmpDir))
175175
// remove temp directory
176176
await fs.remove(tmpDir, (err) => {
177177
if (err) {
178-
console.log(err)
178+
console.log(err);
179179
}
180-
})
181-
process.exit(1)
180+
});
181+
process.exit(1);
182182
}
183183

184184
// Output next steps:
185185
console.log(`
186-
🎉 ${chalk.bold('Your example project is ready!')} 🎉
186+
🎉 ${chalk.bold("Your example project is ready!")} 🎉
187187
188188
Follow the instructions and learn more about the example here:
189189
190190
${config.examples.repoUrl}/tree/main/${args.example}#readme
191-
`)
191+
`);
192192
}
193193

194194
/* --- Existing Project --- */
@@ -197,37 +197,37 @@ async function integrateStackbit() {
197197
return new Promise(async (resolve) => {
198198
const integrate = await prompt(`
199199
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] `);
201201

202-
if (!['yes', 'y'].includes(integrate?.toLowerCase())) return resolve(false)
202+
if (!["yes", "y"].includes(integrate?.toLowerCase())) return resolve(false);
203203

204204
console.log(`
205205
Visit the following URL to learn more about the integration process:
206206
207207
https://docs.stackbit.com/how-to-guides/site-management/integrate-stackbit/
208-
`)
209-
return resolve(true)
210-
})
208+
`);
209+
return resolve(true);
210+
});
211211
}
212212

213213
/* --- Run --- */
214214

215215
async function doCreate() {
216216
// If the current directory has a package.json file, we assume we're in an
217217
// 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();
220220
// If both starter and example were specified, throw an error message.
221221
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);
224224
}
225225
// Start from an example if specified.
226-
if (args.example) return cloneExample()
226+
if (args.example) return cloneExample();
227227
// Otherwise, use a starter, which falls back to the default if not set.
228-
return cloneStarter()
228+
return cloneStarter();
229229
}
230230

231-
await doCreate()
231+
await doCreate();
232232

233-
rl.close()
233+
rl.close();

0 commit comments

Comments
 (0)