Skip to content

Commit ef4d156

Browse files
Existing project improvements (rescript-lang#38)
* Stop exisitng project installation if rescript config is found * Add missing npm scripts to existing project * Ask for module system / suffix and git tracking of JS files for existing projects * Uppercase es6 label
1 parent f447202 commit ef4d156

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/ExistingProject.res

+43-2
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,50 @@ let updatePackageJson = async () =>
1313
config->Dict.set("scripts", Object(scripts))
1414
scripts
1515
}
16+
scripts->Dict.set("res:build", String("rescript"))
17+
scripts->Dict.set("res:clean", String("rescript clean"))
1618
scripts->Dict.set("res:dev", String("rescript build -w"))
1719
| _ => ()
1820
}
1921
)
2022

21-
let updateRescriptJson = async (~projectName, ~sourceDir) =>
23+
let updateRescriptJson = async (~projectName, ~sourceDir, ~moduleSystem, ~suffix) =>
2224
await JsonUtils.updateJsonFile("rescript.json", json =>
2325
switch json {
2426
| Object(config) =>
2527
config->Dict.set("name", String(projectName))
28+
config->Dict.set("suffix", String(suffix))
2629
switch config->Dict.get("sources") {
2730
| Some(Object(sources)) => sources->Dict.set("dir", String(sourceDir))
2831
| _ => ()
2932
}
33+
switch config->Dict.get("package-specs") {
34+
| Some(Object(sources)) => sources->Dict.set("module", String(moduleSystem))
35+
| _ => ()
36+
}
3037
| _ => ()
3138
}
3239
)
3340

41+
let getSuffixForModuleSystem = moduleSystem =>
42+
switch moduleSystem {
43+
| "es6" => ".res.mjs"
44+
| _ => ".res.js"
45+
}
46+
47+
let moduleSystemOptions = [
48+
{
49+
P.value: "commonjs",
50+
label: "CommonJS",
51+
hint: "Use require syntax and .res.js extension",
52+
},
53+
{
54+
value: "es6",
55+
label: "ES6",
56+
hint: "Use import syntax and .res.mjs extension",
57+
},
58+
]
59+
3460
let addToExistingProject = async (~projectName) => {
3561
let versions = await RescriptVersions.promptVersions()
3662

@@ -41,6 +67,17 @@ let addToExistingProject = async (~projectName) => {
4167
initialValue: "src",
4268
})->P.resultOrRaise
4369

70+
let moduleSystem = await P.select({
71+
message: "What module system will you use?",
72+
options: moduleSystemOptions,
73+
})->P.resultOrRaise
74+
75+
let suffix = moduleSystem->getSuffixForModuleSystem
76+
77+
let shouldCheckJsFilesIntoGit = await P.confirm({
78+
message: `Do you want to check generated ${suffix} files into git?`,
79+
})->P.resultOrRaise
80+
4481
let templatePath = CraPaths.getTemplatePath(~templateName=Templates.basicTemplateName)
4582
let projectPath = Process.cwd()
4683
let gitignorePath = Path.join2(projectPath, ".gitignore")
@@ -62,8 +99,12 @@ let addToExistingProject = async (~projectName) => {
6299
await Fs.Promises.copyFile(Path.join2(templatePath, "_gitignore"), gitignorePath)
63100
}
64101

102+
if !shouldCheckJsFilesIntoGit {
103+
await Fs.Promises.appendFile(gitignorePath, `**/*${suffix}${Os.eol}`)
104+
}
105+
65106
await updatePackageJson()
66-
await updateRescriptJson(~projectName, ~sourceDir)
107+
await updateRescriptJson(~projectName, ~sourceDir, ~moduleSystem, ~suffix)
67108

68109
if !Fs.existsSync(sourceDirPath) {
69110
await Fs.Promises.mkdir(sourceDirPath)

src/Main.res

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ https://www.rescript-lang.org`,
3232
)
3333

3434
let packageJsonPath = Path.join2(Process.cwd(), "package.json")
35-
if Fs.existsSync(packageJsonPath) {
35+
let rescriptJsonPath = Path.join2(Process.cwd(), "rescript.json")
36+
let bsconfigJsonPath = Path.join2(Process.cwd(), "bsconfig.json")
37+
38+
if Fs.existsSync(rescriptJsonPath) || Fs.existsSync(bsconfigJsonPath) {
39+
P.outro("ReScript is already installed. No changes were made to your project.")
40+
} else if Fs.existsSync(packageJsonPath) {
3641
let packageJson = await JsonUtils.readJsonFile(packageJsonPath)
3742
let projectName =
3843
packageJson->JsonUtils.getStringValue(~fieldName="name")->Option.getOr("unknown")

0 commit comments

Comments
 (0)