-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathNewProject.res
More file actions
178 lines (148 loc) · 5.41 KB
/
Copy pathNewProject.res
File metadata and controls
178 lines (148 loc) · 5.41 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
open Node
module P = ClackPrompts
let updatePackageJson = async (~projectName, ~versions) =>
await JsonUtils.updateJsonFile("package.json", json =>
switch json {
| Object(config) => {
config->Dict.set("name", String(projectName))
let scripts = switch config->Dict.get("scripts") {
| Some(Object(scripts)) => scripts
| _ =>
let scripts = Dict.make()
config->Dict.set("scripts", Object(scripts))
scripts
}
if RescriptVersions.usesRewatch(versions) {
scripts->Dict.set("res:dev", String("rescript watch"))
}
}
| _ => ()
}
)
let updateRescriptJson = async (~projectName, ~versions: RescriptVersions.versions) =>
await JsonUtils.updateJsonFile("rescript.json", json =>
switch json {
| Object(config) =>
config->Dict.set("name", String(projectName))
switch config->Dict.get("package-specs") {
| Some(Object(packageSpecs)) | Some(Array([Object(packageSpecs)])) =>
packageSpecs->Dict.set("module", String("esmodule"))
config->Dict.set("suffix", String(".res.mjs"))
| _ => ()
}
if Option.isNone(versions.rescriptCoreVersion) {
RescriptJsonUtils.removeRescriptCore(config)
}
// https://github.com/rescript-lang/rescript/blob/master/CHANGELOG.md#1200-beta3
if CompareVersions.satisfies(versions.rescriptVersion, ">=12.0.0-beta.3") {
RescriptJsonUtils.modernizeConfigurationFields(config)
}
| _ => ()
}
)
let updateViteConfig = async () => {
if Fs.existsSync("vite.config.js") {
let rescriptConfig = await JsonUtils.readJsonFile("rescript.json")
let suffix = switch rescriptConfig->JsonUtils.getStringValue(~fieldName="suffix") {
| Some(suffix) => suffix
| None => ".res.mjs"
}
let viteConfig = await Fs.Promises.readFile("vite.config.js")
await Fs.Promises.writeFile(
"vite.config.js",
viteConfig->ViteTemplateUtils.stampOutputSuffix(~suffix),
)
}
}
let newProjectMessage = "Create a new ReScript project"
let getTemplateOptions = () =>
Templates.templates->Array.map(({name, displayName, shortDescription, _}) => {
P.value: name,
label: displayName,
hint: shortDescription,
})
let getVariantOptions = (variants: array<Templates.variant>) =>
variants->Array.map(({name, displayName, shortDescription}) => {
P.value: name,
label: displayName,
hint: shortDescription,
})
let promptTemplateName = async () => {
let selectedName = await P.select({
message: "Select a template",
options: getTemplateOptions(),
})->P.resultOrRaise
switch Templates.templates->Array.find(template => template.name === selectedName) {
| Some({variants: Some(variants)}) =>
await P.select({
message: "Select a variant",
options: getVariantOptions(variants),
})->P.resultOrRaise
| _ => selectedName
}
}
let createProject = async (~templateName, ~projectName, ~versions) => {
let templatePath = CraPaths.getTemplatePath(~templateName)
let projectPath = Path.join2(Process.cwd(), projectName)
let s = P.spinner()
if !CI.isRunningInCI {
s->P.Spinner.start("Creating project...")
}
await Fs.Promises.cp(templatePath, projectPath, ~options={recursive: true})
Process.chdir(projectPath)
await Fs.Promises.rename("_gitignore", ".gitignore")
await updatePackageJson(~projectName, ~versions)
await updateRescriptJson(~projectName, ~versions)
await updateViteConfig()
await RescriptVersions.installVersions(versions)
let _ = await Promisified.ChildProcess.exec("git init")
if !CI.isRunningInCI {
s->P.Spinner.stop("Project created.")
}
P.note(
~title="Get started",
~message=`cd ${projectName}
# See the project's README.md for more information.`,
)
}
let createNewProject = async () => {
P.note(~title="New Project", ~message=newProjectMessage)
if CI.isRunningInCI {
// type versions = {rescriptVersion: string, rescriptCoreVersion: string}
await createProject(
~templateName="rescript-template-basic",
~projectName="test",
~versions={rescriptVersion: "11.1.1", rescriptCoreVersion: Some("1.5.0")},
)
} else {
let commandLineArguments = CommandLineArguments.fromProcessArgv(Process.argv)->Result.getOrThrow
let useDefaultVersions = Option.isSome(commandLineArguments.templateName)
let projectName = switch commandLineArguments.projectName {
| Some(projectName) if useDefaultVersions =>
// Note this throws in the some case, which is why we cannot use Option.getOrThrow here.
switch NewProjectValidation.validateProjectName(projectName) {
| Error(message) => JsError.throwWithMessage(message)
| Ok() => projectName
}
| initialValue =>
await P.text({
message: "What is the name of your new ReScript project?",
placeholder: "my-rescript-app",
?initialValue,
validate: projectName =>
switch NewProjectValidation.validateProjectName(projectName) {
| Ok() => None
| Error(error) => Some(error)
},
})->P.resultOrRaise
}
let templateName = switch commandLineArguments.templateName {
| Some(templateName) => templateName
| None => await promptTemplateName()
}
let versions = useDefaultVersions
? await RescriptVersions.getDefaultVersions()
: await RescriptVersions.promptVersions()
await createProject(~templateName, ~projectName, ~versions)
}
}