Skip to content

Commit 10b2ae8

Browse files
authored
Merge pull request #1 from stackbit/support-starters
Basic support for starters
2 parents 9a6c474 + 82fec32 commit 10b2ae8

File tree

5 files changed

+111
-10
lines changed

5 files changed

+111
-10
lines changed

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
11
# create-stackbit-app
22

33
Run `npx create-stackbit-app` in your terminal to create a new Stackbit application or add Stackbit into an existing site.
4+
5+
## Usage
6+
7+
To create a new Stackbit project from a starter, run the following command:
8+
9+
```txt
10+
npx create-stackbit-app [dir]
11+
```
12+
13+
To see a full list of options use the `--help` flag:
14+
15+
```txt
16+
> npx create-stackbit-app --help
17+
18+
Options:
19+
--version Show version number [boolean]
20+
-s, --starter Choose a starter [choices: "nextjs", "ts-nextjs"]
21+
--help Show help [boolean]
22+
```
23+
24+
### Choosing a Starter
25+
26+
Use the `--starter` option for specifying a starter. Run the command with the `--help` flag to see a full list of available starters.
27+
28+
```txt
29+
npx create-stackbit-app --starter ts-nextjs
30+
```
31+
32+
If no starter option is provided, [the default starter](https://github.com/stackbit-themes/nextjs-starter) is used.
33+
34+
### Setting Project Directory
35+
36+
Pass a directory name as the only argument when running the command. For example, if you wanted your directory to be name `my-site`, the command would look something like this:
37+
38+
```txt
39+
npx create-stackbit-app my-site
40+
```
41+
42+
If no name is provided, the directory will be `my-stackbit-site-[id]`, where `[id]` is a randomly-generated string used to avoid directory conflicts.
43+
44+
## Adding Stackbit to Existing Projects
45+
46+
The script detects when you may be working with an existing project (it looks for a `package.json` file in the working directory).
47+
48+
If in an existing project, the script asks if you'd like to add Stackbit to the project. Today, this only prints a resource URL. If you choose _no_, the command exits.

config.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const starters = [
2+
{
3+
name: "nextjs",
4+
repoUrl: "https://github.com/stackbit-themes/nextjs-starter",
5+
},
6+
{
7+
name: "ts-nextjs",
8+
repoUrl: "https://github.com/stackbit-themes/ts-mui-nextjs-starter",
9+
},
10+
];
11+
12+
export default {
13+
defaults: { dirName: "my-stackbit-site", starter: starters[0] },
14+
starters,
15+
};

index.js

+30-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import chalk from "chalk";
44
import { exec } from "child_process";
55
import fs from "fs";
6+
import { nanoid } from "nanoid";
67
import path from "path";
78
import readline from "readline";
89
import util from "util";
910
import yargs from "yargs";
1011
import { hideBin } from "yargs/helpers";
1112

13+
import config from "./config.js";
14+
1215
/* --- Helpers --- */
1316

1417
const run = util.promisify(exec);
@@ -26,29 +29,49 @@ function prompt(question, defaultAnswer) {
2629

2730
/* --- Parse CLI Arguments */
2831

29-
const args = yargs(hideBin(process.argv)).parse();
32+
const args = yargs(hideBin(process.argv))
33+
.option("starter", {
34+
alias: "s",
35+
describe: "Choose a starter",
36+
choices: config.starters.map((s) => s.name),
37+
})
38+
.help()
39+
.parse();
40+
41+
/* --- References --- */
42+
43+
const starter = config.starters.find(
44+
(s) => s.name === (args.starter ?? config.defaults.starter.name)
45+
);
46+
const dirName =
47+
args._[0] ?? `${config.defaults.dirName}-${nanoid(8).toLowerCase()}`;
3048

3149
/* --- New Project --- */
3250

3351
async function cloneStarter() {
3452
// 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} ...`);
53+
const cloneCommand = `git clone --depth=1 ${starter.repoUrl} ${dirName}`;
54+
console.log(`\nCreating new project in ${dirName} ...`);
3955
await run(cloneCommand);
4056

4157
// Install dependencies
4258
console.log(`Installing dependencies ...`);
43-
await run(`cd ${projectName} && npm install`);
59+
await run(`cd ${dirName} && npm install`);
60+
61+
// Set up git
62+
console.log(`Setting up Git ...`);
63+
await run(`rm -rf ${dirName}/.git`);
64+
await run(
65+
`cd ${dirName} && git init && git add . && git commit -m "New Stackbit project"`
66+
);
4467

4568
// Output next steps:
4669
console.log(`
4770
🎉 ${chalk.bold("Welcome to Stackbit!")} 🎉
4871
4972
Follow the instructions for getting Started here:
5073
51-
https://github.com/stackbit/nextjs-starter#getting-started
74+
${starter.repoUrl}#readme
5275
`);
5376
}
5477

package-lock.json

+19-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-stackbit-app",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Create a new Stackbit site, or add Stackbit to an existing site.",
55
"main": "index.js",
66
"scripts": {
@@ -15,6 +15,7 @@
1515
"type": "module",
1616
"dependencies": {
1717
"chalk": "^5.0.0",
18+
"nanoid": "^3.3.4",
1819
"yargs": "^17.3.1"
1920
}
2021
}

0 commit comments

Comments
 (0)