Skip to content

Commit 6725629

Browse files
authored
Interactive prompt for project name and template using enquirer (#9)
1 parent 9c84789 commit 6725629

File tree

6 files changed

+128
-147
lines changed

6 files changed

+128
-147
lines changed

.prettierrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"arrowParens": "avoid"
2+
"arrowParens": "avoid",
3+
"printWidth": 100
34
}

Readme.md

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# create-rescript-app
22

3-
Create ReScript apps with no build configuration.
3+
Quickly create new [ReScript](https://rescript-lang.org/) apps from project templates.
44

55
## Quick Start
66

@@ -9,31 +9,3 @@ npx create-rescript-app my-app
99
cd my-app
1010
npm start
1111
```
12-
13-
## Install options
14-
15-
| Template | short cmd | long cmd |
16-
| :------------------------------- | :-------: | ----------: |
17-
| Basic | -b | --basic |
18-
| Default / CRA equivalent (React) | -d | --default |
19-
| NextJS | -nx | --nextjs |
20-
| GraphQL | -gql | --graphql |
21-
| Storybook | -sb | --storybook |
22-
23-
### Bootstrap a ReScript app with Graphql
24-
25-
```sh
26-
npx create-rescript-app my-app -gql
27-
```
28-
29-
### Bootstrap a ReScript app with Storybook
30-
31-
```sh
32-
npx create-rescript-app my-app -sb
33-
```
34-
35-
### Bootstrap a NextJS app with ReScript
36-
37-
```sh
38-
npx create-rescript-app my-app -nx
39-
```

index.js

Lines changed: 0 additions & 104 deletions
This file was deleted.

index.mjs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env node
2+
3+
import c from "ansi-colors";
4+
import enquirer from "enquirer";
5+
import path from "path";
6+
import fs from "fs";
7+
import { execSync } from "child_process";
8+
9+
const templates = {
10+
Basic: "https://github.com/rescript-lang/rescript-project-template.git",
11+
"Next.js": "https://github.com/ryyppy/rescript-nextjs-template.git",
12+
};
13+
14+
async function getParams() {
15+
return await enquirer.prompt([
16+
{
17+
type: "input",
18+
name: "projectName",
19+
message: "What is the name of your new project?",
20+
initial: "my-rescript-app",
21+
},
22+
{
23+
type: "select",
24+
name: "templateName",
25+
message: "Select a template",
26+
choices: Object.keys(templates),
27+
},
28+
]);
29+
}
30+
31+
function createProjectDir(projectName, projectPath) {
32+
try {
33+
fs.mkdirSync(projectPath);
34+
} catch (err) {
35+
if (err.code === "EEXIST") {
36+
console.log(`The folder ${c.red(projectName)} already exist in the current directory.`);
37+
console.log("Please try again with another name.");
38+
} else {
39+
console.log(err);
40+
}
41+
process.exit(1);
42+
}
43+
}
44+
45+
function houseKeeping(projectName, projectPath) {
46+
process.chdir(projectPath);
47+
48+
console.log("Installing packages. This might take a couple of seconds...");
49+
50+
execSync("npm install");
51+
execSync(`find . | grep "\.git/" | xargs rm -rf`);
52+
execSync("git init");
53+
54+
console.log(`Initialized a git repository.\n`);
55+
console.log(`${c.green("✔ Success!")} Created ${projectName} at ${c.green(projectPath)}`);
56+
}
57+
58+
async function main() {
59+
console.log(c.cyan(`Welcome to ${c.red("create-rescript-app")}!`));
60+
console.log("This tool will help you set up your new ReScript project quickly.\n");
61+
62+
const { projectName, templateName } = await getParams();
63+
console.log();
64+
65+
const projectPath = path.join(process.cwd(), projectName);
66+
createProjectDir(projectName, projectPath);
67+
68+
console.log(
69+
"Creating a new ReScript project",
70+
`in ${c.green(projectPath)} with template ${c.cyan(templateName)}\n`
71+
);
72+
73+
try {
74+
const repoUrl = templates[templateName];
75+
execSync(`git clone --depth 1 ${repoUrl} ${projectPath}`);
76+
77+
houseKeeping(projectName, projectPath);
78+
79+
console.log(c.bold("Happy hacking!"));
80+
} catch (error) {
81+
console.log(error);
82+
}
83+
}
84+
85+
main();

package-lock.json

Lines changed: 33 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
{
22
"name": "create-rescript-app",
33
"version": "1.3.0",
4-
"description": "Create ReScript apps with no build configuration.",
4+
"description": "Quickly create new ReScript apps from project templates.",
55
"main": "index.js",
66
"scripts": {
7-
"start": "node index.js",
7+
"start": "node index.mjs",
88
"format": "prettier --write ."
99
},
1010
"author": "",
1111
"license": "ISC",
12+
"engines": {
13+
"node": ">=12"
14+
},
1215
"dependencies": {
13-
"colors": "^1.4.0"
16+
"ansi-colors": "^4.1.3",
17+
"enquirer": "^2.3.6"
1418
},
1519
"repository": {
1620
"type": "git",

0 commit comments

Comments
 (0)