Skip to content

Commit d653112

Browse files
committed
Get templates from create-rescript-app package instead of external source
1 parent e0e2d90 commit d653112

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

index.mjs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,30 @@ import c from "ansi-colors";
44
import enquirer from "enquirer";
55
import path from "path";
66
import fs from "fs";
7+
import { fileURLToPath } from "url";
78
import { execSync } from "child_process";
89

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-
};
10+
// Get __dirname in an ES6 module
11+
const __filename = fileURLToPath(import.meta.url);
12+
const __dirname = path.dirname(__filename);
13+
14+
const templates = [
15+
{
16+
name: "rescript-template-vite",
17+
message: "Vite",
18+
hint: "Opinionated boilerplate for Vite, Tailwind and ReScript",
19+
},
20+
{
21+
name: "rescript-template-nextjs",
22+
message: "Next.js",
23+
hint: "Opinionated boilerplate for Next.js, Tailwind and ReScript",
24+
},
25+
{
26+
name: "rescript-template-basic",
27+
message: "Basic",
28+
hint: "Command line hello world app",
29+
},
30+
];
1331

1432
async function getParams() {
1533
return await enquirer.prompt([
@@ -23,25 +41,11 @@ async function getParams() {
2341
type: "select",
2442
name: "templateName",
2543
message: "Select a template",
26-
choices: Object.keys(templates),
44+
choices: templates,
2745
},
2846
]);
2947
}
3048

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-
4549
function houseKeeping(projectName, projectPath) {
4650
process.chdir(projectPath);
4751

@@ -60,20 +64,23 @@ async function main() {
6064
console.log("This tool will help you set up your new ReScript project quickly.\n");
6165

6266
const { projectName, templateName } = await getParams();
63-
console.log();
6467

68+
const templatePath = path.join(__dirname, "templates", templateName);
6569
const projectPath = path.join(process.cwd(), projectName);
66-
createProjectDir(projectName, projectPath);
70+
71+
if (fs.existsSync(projectPath)) {
72+
console.log(`The folder ${c.red(projectName)} already exist in the current directory.`);
73+
console.log("Please try again with another name.");
74+
process.exit(1);
75+
}
6776

6877
console.log(
6978
"Creating a new ReScript project",
7079
`in ${c.green(projectPath)} with template ${c.cyan(templateName)}\n`
7180
);
7281

7382
try {
74-
const repoUrl = templates[templateName];
75-
execSync(`git clone --depth 1 ${repoUrl} ${projectPath}`);
76-
83+
fs.cpSync(templatePath, projectPath, { recursive: true });
7784
houseKeeping(projectName, projectPath);
7885

7986
console.log(c.bold("Happy hacking!"));

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"node": ">=12"
1414
},
1515
"files": [
16-
"index.mjs"
16+
"index.mjs",
17+
"templates"
1718
],
1819
"dependencies": {
1920
"ansi-colors": "^4.1.3",

0 commit comments

Comments
 (0)