Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b077f05

Browse files
committed
Fix "add to existing project" feature
1 parent 4e072d7 commit b077f05

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

‎index.mjs‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ async function updatePackageJson(projectName) {
7878
);
7979
}
8080

81+
async function updateExistingPackageJson() {
82+
await updateFile("package.json", contents => {
83+
let config = JSON.parse(contents);
84+
config["scripts"] = { ...(config["scripts"] || {}), "res:dev": "rescript build -w" };
85+
return JSON.stringify(config, null, 2);
86+
});
87+
}
88+
8189
async function updateRescriptJson(projectName, sourceDir) {
8290
await updateFile("rescript.json", contents => {
8391
const config = JSON.parse(contents);
@@ -107,21 +115,17 @@ function getProjectPackageJson() {
107115
return JSON.parse(contents);
108116
}
109117

110-
async function addToExistingProject() {
111-
const projectName = getProjectPackageJson().name;
112-
118+
/**
119+
* @param {string} projectName
120+
*/
121+
async function addToExistingProject(projectName) {
113122
const templatePath = path.join(__dirname, "templates", "rescript-template-basic");
114123
const projectPath = process.cwd();
115124
const gitignorePath = path.join(projectPath, ".gitignore");
116125

117126
const s = p.spinner();
118127

119128
try {
120-
const addToProj = await p.confirm({
121-
message: `Detected a package.json file. Do you want to add ReScript to "${projectName}"?`,
122-
});
123-
checkCancel(addToProj);
124-
125129
s.start("Loading available versions...");
126130
const [rescriptVersions, rescriptCoreVersions] = await Promise.all([
127131
getPackageVersions("rescript", rescriptVersionRange),
@@ -149,21 +153,24 @@ async function addToExistingProject() {
149153
});
150154
checkCancel(sourceDir);
151155

156+
s.start("Adding ReScript to your project...");
157+
152158
await fs.promises.copyFile(
153159
path.join(templatePath, "rescript.json"),
154160
path.join(projectPath, "rescript.json")
155161
);
156162

157163
if (fs.existsSync(gitignorePath)) {
158-
await fs.promises.appendFile(gitignorePath, "/lib/" + os.EOL + ".bsb.lock" + os.EOL);
164+
await fs.promises.appendFile(gitignorePath, os.EOL+"/lib/" + os.EOL + ".bsb.lock" + os.EOL);
159165
}
160166

167+
await updateExistingPackageJson();
161168
await updateRescriptJson(projectName, sourceDir);
162169

163170
const sourceDirPath = path.join(projectPath, sourceDir);
164171

165172
if (!fs.existsSync(sourceDirPath)) {
166-
fs.mkdirSync(sourceDirPath);
173+
awaitfs.promises.mkdir(sourceDirPath);
167174
}
168175

169176
await fs.promises.copyFile(
@@ -176,7 +183,7 @@ async function addToExistingProject() {
176183
await promisify(exec)("npm add " + packages.join(" "));
177184

178185
s.stop("Added ReScript to your project.");
179-
p.note(`cd ${projectName}\nnpm run res:dev`, "Next steps");
186+
p.note(`npm run res:dev`, "Next steps");
180187
p.outro(`Happy hacking!`);
181188
} catch (error) {
182189
console.warn(error);
@@ -269,10 +276,21 @@ https://www.rescript-lang.org\n\nCreate a new ReScript 11 project with modern de
269276
"Welcome to ReScript!"
270277
);
271278

272-
const existingPackageJson = fs.existsSync(path.join(process.cwd(), "package.json"));
279+
const existingProjectDetected = fs.existsSync(path.join(process.cwd(), "package.json"));
273280

274-
if (existingPackageJson) {
275-
addToExistingProject();
281+
if (existingProjectDetected) {
282+
const projectName = getProjectPackageJson().name;
283+
284+
const addToExistingProjectConfirmed = await p.confirm({
285+
message: `Detected a package.json file. Do you want to add ReScript to "${projectName}"?`,
286+
});
287+
checkCancel(addToExistingProjectConfirmed);
288+
289+
if (addToExistingProjectConfirmed) {
290+
addToExistingProject(projectName);
291+
} else {
292+
createNewProject();
293+
}
276294
} else {
277295
createNewProject();
278296
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /