Skip to content

Commit f1c1ef8

Browse files
committed
Make including Core optional
1 parent 8940d8a commit f1c1ef8

File tree

9 files changed

+51
-43
lines changed

9 files changed

+51
-43
lines changed

index.mjs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ const templates = [
1616
{
1717
value: "rescript-template-vite",
1818
label: "Vite",
19-
hint: "Opinionated boilerplate for Vite, Tailwind and ReScript",
20-
},
21-
{
22-
value: "rescript-template-nextjs",
23-
label: "Next.js",
24-
hint: "Opinionated boilerplate for Next.js, Tailwind and ReScript",
19+
hint: "ReScript 10.1, JSX4, Vite, Tailwind CSS",
2520
},
2621
{
2722
value: "rescript-template-basic",
2823
label: "Basic",
29-
hint: "Command line hello world app",
24+
hint: "ReScript 10.1, command line hello world app",
25+
},
26+
{
27+
value: "rescript-template-nextjs",
28+
label: "Next.js",
29+
hint: "ReScript 9.1, Next.js, Tailwind CSS",
30+
incompatibleWithCore: true,
3031
},
3132
];
3233

@@ -54,15 +55,31 @@ function validateProjectName(projectName) {
5455
}
5556
}
5657

57-
async function replaceLineInFile(filename, search, replace) {
58+
async function updateFile(filename, updateContents) {
5859
const contents = await fs.promises.readFile(filename, "utf8");
59-
const replaced = contents.replace(search, replace);
60-
await fs.promises.writeFile(filename, replaced, "utf8");
60+
const updated = updateContents(contents);
61+
await fs.promises.writeFile(filename, updated, "utf8");
6162
}
6263

63-
async function setProjectName(templateName, projectName) {
64-
await replaceLineInFile("package.json", `"name": "${templateName}"`, `"name": "${projectName}"`);
65-
await replaceLineInFile("bsconfig.json", `"name": "${templateName}"`, `"name": "${projectName}"`);
64+
async function updatePackageJson(projectName) {
65+
await updateFile("package.json", contents =>
66+
contents.replace(/"name": "rescript-template-.*"/, `"name": "${projectName}"`)
67+
);
68+
}
69+
70+
async function updateBsconfigJson(projectName, withCore) {
71+
await updateFile("bsconfig.json", contents => {
72+
const config = JSON.parse(contents);
73+
74+
config["name"] = projectName;
75+
76+
if (withCore) {
77+
config["bs-dependencies"] = [...(config["bs-dependencies"] || []), "@rescript/core"];
78+
config["bsc-flags"] = [...(config["bsc-flags"] || []), "-open RescriptCore"];
79+
}
80+
81+
return JSON.stringify(config, null, 2);
82+
});
6683
}
6784

6885
function getVersion() {
@@ -90,15 +107,14 @@ async function main() {
90107
});
91108
checkCancel(templateName);
92109

93-
const shouldContinue = await p.confirm({
94-
message: `Your new ReScript project ${c.cyan(projectName)} will now be created. Continue?`,
95-
});
96-
checkCancel(shouldContinue);
110+
const incompatibleWithCore = templates.find(t => t.value === templateName).incompatibleWithCore;
97111

98-
if (!shouldContinue) {
99-
p.outro("No project created.");
100-
process.exit(0);
101-
}
112+
const withCore =
113+
!incompatibleWithCore &&
114+
(await p.confirm({
115+
message: "Add the new @rescript/core standard libary?",
116+
}));
117+
checkCancel(withCore);
102118

103119
const templatePath = path.join(__dirname, "templates", templateName);
104120
const projectPath = path.join(process.cwd(), projectName);
@@ -110,8 +126,15 @@ async function main() {
110126
await fs.promises.cp(templatePath, projectPath, { recursive: true });
111127
process.chdir(projectPath);
112128

113-
await setProjectName(templateName, projectName);
114-
await promisify(exec)("npm install");
129+
await updatePackageJson(projectName);
130+
await updateBsconfigJson(projectName, withCore);
131+
132+
if (withCore) {
133+
await promisify(exec)("npm add @rescript/core");
134+
} else {
135+
await promisify(exec)("npm install");
136+
}
137+
115138
await promisify(exec)("git init");
116139
s.stop("Project created.");
117140

templates/rescript-template-basic/bsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@
99
"in-source": true
1010
},
1111
"suffix": ".bs.js",
12-
"bs-dependencies": ["@rescript/core"],
13-
"bsc-flags": ["-open RescriptCore"]
12+
"bs-dependencies": []
1413
}

templates/rescript-template-basic/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"author": "",
1313
"license": "MIT",
1414
"dependencies": {
15-
"@rescript/core": "^0.1.0",
1615
"rescript": "^10.1.2"
1716
}
1817
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Console.log("Hello, World!")
1+
Js.log("Hello, World!")

templates/rescript-template-vite/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a Vite-based template with following setup:
44

5-
- [ReScript](https://rescript-lang.org) 10.1 with JSX 4 automatic mode and the new [`@rescript/core`](https://github.com/rescript-association/rescript-core) standard library
5+
- [ReScript](https://rescript-lang.org) 10.1 with @rescript/react and JSX 4 automatic mode
66
- ES6 modules (ReScript code compiled to `.bs.mjs` files)
77
- Vite 4 with React Plugin (Fast Refresh)
88
- Tailwind 3

templates/rescript-template-vite/bsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
}
1414
],
1515
"suffix": ".bs.mjs",
16-
"bs-dependencies": ["@rescript/core", "@rescript/react"],
17-
"bsc-flags": ["-open RescriptCore"],
16+
"bs-dependencies": ["@rescript/react"],
1817
"jsx": {
1918
"version": 4,
2019
"mode": "automatic"

templates/rescript-template-vite/package-lock.json

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

templates/rescript-template-vite/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"preview": "vite preview"
1313
},
1414
"dependencies": {
15-
"@rescript/core": "^0.1.0",
1615
"@rescript/react": "^0.11.0",
1716
"react": "^18.2.0",
1817
"react-dom": "^18.2.0",

templates/rescript-template-vite/src/App.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let make = () => {
99
</p>
1010
<h2 className="text-2xl font-semibold mt-5"> {React.string("Fast Refresh Test")} </h2>
1111
<Button onClick={_ => setCount(count => count + 1)}>
12-
{React.string(`count is ${count->Int.toString}`)}
12+
{React.string(`count is ${count->string_of_int}`)}
1313
</Button>
1414
<p>
1515
{React.string("Edit ")}

0 commit comments

Comments
 (0)