Skip to content

Existing project improvements #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/ExistingProject.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,50 @@ let updatePackageJson = async () =>
config->Dict.set("scripts", Object(scripts))
scripts
}
scripts->Dict.set("res:build", String("rescript"))
scripts->Dict.set("res:clean", String("rescript clean"))
scripts->Dict.set("res:dev", String("rescript build -w"))
| _ => ()
}
)

let updateRescriptJson = async (~projectName, ~sourceDir) =>
let updateRescriptJson = async (~projectName, ~sourceDir, ~moduleSystem, ~suffix) =>
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))
| _ => ()
}
| _ => ()
}
)

let getSuffixForModuleSystem = moduleSystem =>
switch moduleSystem {
| "es6" => ".res.mjs"
| _ => ".res.js"
}

let moduleSystemOptions = [
{
P.value: "commonjs",
label: "CommonJS",
hint: "Use require syntax and .res.js extension",
},
{
value: "es6",
label: "ES6",
hint: "Use import syntax and .res.mjs extension",
},
]

let addToExistingProject = async (~projectName) => {
let versions = await RescriptVersions.promptVersions()

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

let moduleSystem = await P.select({
message: "What module system will you use?",
options: moduleSystemOptions,
})->P.resultOrRaise

let suffix = moduleSystem->getSuffixForModuleSystem

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")
Expand All @@ -62,8 +99,12 @@ let addToExistingProject = async (~projectName) => {
await Fs.Promises.copyFile(Path.join2(templatePath, "_gitignore"), gitignorePath)
}

if !shouldCheckJsFilesIntoGit {
await Fs.Promises.appendFile(gitignorePath, `**/*${suffix}${Os.eol}`)
}

await updatePackageJson()
await updateRescriptJson(~projectName, ~sourceDir)
await updateRescriptJson(~projectName, ~sourceDir, ~moduleSystem, ~suffix)

if !Fs.existsSync(sourceDirPath) {
await Fs.Promises.mkdir(sourceDirPath)
Expand Down
7 changes: 6 additions & 1 deletion src/Main.res
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ https://www.rescript-lang.org`,
)

let packageJsonPath = Path.join2(Process.cwd(), "package.json")
if Fs.existsSync(packageJsonPath) {
let rescriptJsonPath = Path.join2(Process.cwd(), "rescript.json")
let bsconfigJsonPath = Path.join2(Process.cwd(), "bsconfig.json")

if Fs.existsSync(rescriptJsonPath) || Fs.existsSync(bsconfigJsonPath) {
P.outro("ReScript is already installed. No changes were made to your project.")
} else if Fs.existsSync(packageJsonPath) {
let packageJson = await JsonUtils.readJsonFile(packageJsonPath)
let projectName =
packageJson->JsonUtils.getStringValue(~fieldName="name")->Option.getOr("unknown")
Expand Down