-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathExistingJsProject.res
More file actions
124 lines (102 loc) · 3.59 KB
/
ExistingJsProject.res
File metadata and controls
124 lines (102 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
open Node
module P = ClackPrompts
let updatePackageJson = async (~versions) =>
await JsonUtils.updateJsonFile("package.json", json =>
switch json {
| Object(config) =>
let scripts = switch config->Dict.get("scripts") {
| Some(Object(scripts)) => scripts
| _ =>
let scripts = Dict.make()
config->Dict.set("scripts", Object(scripts))
scripts
}
scripts->Dict.set("res:build", String("rescript"))
scripts->Dict.set("res:clean", String("rescript clean"))
if RescriptVersions.usesRewatch(versions) {
scripts->Dict.set("res:dev", String("rescript watch"))
} else {
scripts->Dict.set("res:dev", String("rescript -w"))
}
| _ => ()
}
)
let updateRescriptJson = async (~projectName, ~sourceDir, ~moduleSystem, ~suffix, ~versions) =>
await JsonUtils.updateJsonFile("rescript.json", json =>
switch json {
| Object(config) =>
config->Dict.set("name", String(projectName))
config->Dict.set("suffix", String(suffix))
switch config->Dict.get("sources") {
| Some(Object(sources)) => sources->Dict.set("dir", String(sourceDir))
| _ => ()
}
switch config->Dict.get("package-specs") {
| Some(Object(sources)) => sources->Dict.set("module", String(moduleSystem))
| _ => ()
}
if Option.isNone(versions.RescriptVersions.rescriptCoreVersion) {
RescriptJsonUtils.removeRescriptCore(config)
}
| _ => ()
}
)
let getModuleSystemOptions = () => [
{
P.value: "esmodule",
label: "ES Modules",
hint: "Use import syntax and .res.mjs extension",
},
{
value: "commonjs",
label: "CommonJS",
hint: "Use require syntax and .res.js extension",
},
]
let addToExistingProject = async (~projectName) => {
let versions = await RescriptVersions.promptVersions()
let sourceDir = await P.text({
message: "Where will you put your ReScript source files?",
defaultValue: "src",
placeholder: "src",
initialValue: "src",
})->P.resultOrRaise
let moduleSystem = await P.select({
message: "What module system will you use?",
options: getModuleSystemOptions(),
})->P.resultOrRaise
let suffix = moduleSystem === "esmodule" ? ".res.mjs" : ".res.js"
let shouldCheckJsFilesIntoGit = await P.confirm({
message: `Do you want to check generated ${suffix} files into git?`,
})->P.resultOrRaise
let templatePath = CraPaths.getTemplatePath(~templateName=Templates.basicTemplateName)
let projectPath = Process.cwd()
let gitignorePath = Path.join2(projectPath, ".gitignore")
let sourceDirPath = Path.join2(projectPath, sourceDir)
let s = P.spinner()
s->P.Spinner.start("Adding ReScript to your project...")
await Fs.Promises.copyFile(
Path.join2(templatePath, "rescript.json"),
Path.join2(projectPath, "rescript.json"),
)
if Fs.existsSync(gitignorePath) {
open Os
await Fs.Promises.appendFile(gitignorePath, `${eol}/lib/${eol}.bsb.lock${eol}`)
} else {
await Fs.Promises.copyFile(Path.join2(templatePath, "_gitignore"), gitignorePath)
}
if !shouldCheckJsFilesIntoGit {
await Fs.Promises.appendFile(gitignorePath, `**/*${suffix}${Os.eol}`)
}
await updatePackageJson(~versions)
await updateRescriptJson(~projectName, ~sourceDir, ~moduleSystem, ~suffix, ~versions)
if !Fs.existsSync(sourceDirPath) {
await Fs.Promises.mkdir(sourceDirPath)
}
await Fs.Promises.copyFile(
Path.join([templatePath, "src", "Demo.res"]),
Path.join([sourceDirPath, "Demo.res"]),
)
await RescriptVersions.installVersions(versions)
s->P.Spinner.stop("Added ReScript to your project.")
}