diff --git a/src/ExistingJsProject.res b/src/ExistingJsProject.res index fb94207..f65e764 100644 --- a/src/ExistingJsProject.res +++ b/src/ExistingJsProject.res @@ -20,7 +20,7 @@ let updatePackageJson = async () => } ) -let updateRescriptJson = async (~projectName, ~sourceDir, ~moduleSystem, ~suffix) => +let updateRescriptJson = async (~projectName, ~sourceDir, ~moduleSystem, ~suffix, ~versions) => await JsonUtils.updateJsonFile("rescript.json", json => switch json { | Object(config) => @@ -34,6 +34,10 @@ let updateRescriptJson = async (~projectName, ~sourceDir, ~moduleSystem, ~suffix | Some(Object(sources)) => sources->Dict.set("module", String(moduleSystem)) | _ => () } + + if Option.isNone(versions.RescriptVersions.rescriptCoreVersion) { + RescriptJsonUtils.removeRescriptCore(config) + } | _ => () } ) @@ -98,7 +102,7 @@ let addToExistingProject = async (~projectName) => { } await updatePackageJson() - await updateRescriptJson(~projectName, ~sourceDir, ~moduleSystem, ~suffix) + await updateRescriptJson(~projectName, ~sourceDir, ~moduleSystem, ~suffix, ~versions) if !Fs.existsSync(sourceDirPath) { await Fs.Promises.mkdir(sourceDirPath) diff --git a/src/NewProject.res b/src/NewProject.res index f76211b..90a1611 100644 --- a/src/NewProject.res +++ b/src/NewProject.res @@ -37,6 +37,10 @@ let updateRescriptJson = async (~projectName, ~versions) => config->Dict.set("suffix", String(suffix)) | _ => () } + + if Option.isNone(versions.rescriptCoreVersion) { + RescriptJsonUtils.removeRescriptCore(config) + } | _ => () } ) @@ -84,7 +88,7 @@ let createNewProject = async () => { await createProject( ~templateName="rescript-template-basic", ~projectName="test", - ~versions={rescriptVersion: "11.1.1", rescriptCoreVersion: "1.5.0"}, + ~versions={rescriptVersion: "11.1.1", rescriptCoreVersion: Some("1.5.0")}, ) } else { let projectName = await P.text({ diff --git a/src/RescriptJsonUtils.res b/src/RescriptJsonUtils.res new file mode 100644 index 0000000..d8190b2 --- /dev/null +++ b/src/RescriptJsonUtils.res @@ -0,0 +1,29 @@ +let removeRescriptCore = (config: Dict.t) => { + // Remove @rescript/core from bs-dependencies if the version is not set. + switch config->Dict.get("bs-dependencies") { + | Some(Array(dependencies)) => { + let newDependencies = dependencies->Array.filter(dependency => + switch dependency { + | String("@rescript/core") => false + | _ => true + } + ) + config->Dict.set("bs-dependencies", Array(newDependencies)) + } + | _ => () + } + + // Remove "-open RescriptCore" from bsc-flags if the version is not set. + switch config->Dict.get("bsc-flags") { + | Some(Array(flags)) => { + let newFlags = flags->Array.filter(flag => + switch flag { + | String("-open RescriptCore") => false + | _ => true + } + ) + config->Dict.set("bsc-flags", Array(newFlags)) + } + | _ => () + } +} diff --git a/src/RescriptVersions.res b/src/RescriptVersions.res index 225c1b2..3a6afd0 100644 --- a/src/RescriptVersions.res +++ b/src/RescriptVersions.res @@ -1,9 +1,10 @@ module P = ClackPrompts -let rescriptVersionRange = "11.x.x" +let rescript12VersionRange = ">=12.0.0-alpha.5" +let rescriptVersionRange = `11.x.x || ${rescript12VersionRange}` let rescriptCoreVersionRange = ">=1.0.0" -type versions = {rescriptVersion: string, rescriptCoreVersion: string} +type versions = {rescriptVersion: string, rescriptCoreVersion: option} let getCompatibleRescriptCoreVersions = (~rescriptVersion, ~rescriptCoreVersions) => if CompareVersions.compareVersions(rescriptVersion, "11.1.0")->Ordering.isLess { @@ -34,10 +35,14 @@ let promptVersions = async () => { let rescriptVersion = switch rescriptVersionsResult { | Ok([version]) => version | Ok(rescriptVersions) => - await P.select({ - message: "ReScript version?", - options: rescriptVersions->Array.map(v => {P.value: v}), - })->P.resultOrRaise + let options = rescriptVersions->Array.map(v => {P.value: v}) + + let initialValue = + options->Array.find(o => o.value->String.startsWith("11."))->Option.map(o => o.value) + + let selectOptions = {ClackPrompts.message: "ReScript version?", options, ?initialValue} + + await P.select(selectOptions)->P.resultOrRaise | Error(error) => error->NpmRegistry.getFetchErrorMessage->Error.make->Error.raise } @@ -51,13 +56,17 @@ let promptVersions = async () => { ~rescriptCoreVersions, ) + let isRescript12 = CompareVersions.satisfies(rescriptVersion, rescript12VersionRange) + let rescriptCoreVersion = switch rescriptCoreVersions { - | [version] => version + | _ if isRescript12 => None + | [version] => Some(version) | _ => - await P.select({ + let version = await P.select({ message: "ReScript Core version?", options: rescriptCoreVersions->Array.map(v => {P.value: v}), })->P.resultOrRaise + Some(version) } {rescriptVersion, rescriptCoreVersion} @@ -65,7 +74,13 @@ let promptVersions = async () => { let installVersions = async ({rescriptVersion, rescriptCoreVersion}) => { let packageManager = PackageManagers.getActivePackageManager() - let packages = [`rescript@${rescriptVersion}`, `@rescript/core@${rescriptCoreVersion}`] + let packages = switch rescriptCoreVersion { + | Some(rescriptCoreVersion) => [ + `rescript@${rescriptVersion}`, + `@rescript/core@${rescriptCoreVersion}`, + ] + | None => [`rescript@${rescriptVersion}`] + } // #58: Windows: packageManager may be something like // "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js". diff --git a/src/RescriptVersions.resi b/src/RescriptVersions.resi index 42d894d..2e53c4f 100644 --- a/src/RescriptVersions.resi +++ b/src/RescriptVersions.resi @@ -1,4 +1,4 @@ -type versions = {rescriptVersion: string, rescriptCoreVersion: string} +type versions = {rescriptVersion: string, rescriptCoreVersion: option} let promptVersions: unit => promise diff --git a/src/bindings/ClackPrompts.res b/src/bindings/ClackPrompts.res index a149af3..da5a8c8 100644 --- a/src/bindings/ClackPrompts.res +++ b/src/bindings/ClackPrompts.res @@ -38,6 +38,7 @@ type selectOption = { type selectOptions = { message: string, options: array, + initialValue?: string, } @module("@clack/prompts") diff --git a/templates/rescript-template-basic/package.json b/templates/rescript-template-basic/package.json index fc86171..d494346 100644 --- a/templates/rescript-template-basic/package.json +++ b/templates/rescript-template-basic/package.json @@ -11,8 +11,5 @@ ], "author": "", "license": "MIT", - "dependencies": { - "@rescript/core": "^1.3.0", - "rescript": "^11.1.0" - } + "dependencies": {} } diff --git a/templates/rescript-template-nextjs/package.json b/templates/rescript-template-nextjs/package.json index 781ad3d..e1fb9e2 100644 --- a/templates/rescript-template-nextjs/package.json +++ b/templates/rescript-template-nextjs/package.json @@ -4,12 +4,10 @@ "author": "Patrick Ecker ", "license": "Apache-2.0", "dependencies": { - "@rescript/core": "^1.3.0", "@rescript/react": "^0.12.1", "next": "^14.0.4", "react": "^18.2.0", - "react-dom": "^18.2.0", - "rescript": "^11.1.0" + "react-dom": "^18.2.0" }, "repository": "https://github.com/rescript-lang/create-rescript-app", "scripts": { diff --git a/templates/rescript-template-vite/package.json b/templates/rescript-template-vite/package.json index d5b9ea2..5c6dd86 100644 --- a/templates/rescript-template-vite/package.json +++ b/templates/rescript-template-vite/package.json @@ -12,11 +12,9 @@ "preview": "vite preview" }, "dependencies": { - "@rescript/core": "^1.3.0", "@rescript/react": "^0.12.1", "react": "^18.2.0", - "react-dom": "^18.2.0", - "rescript": "^11.1.0" + "react-dom": "^18.2.0" }, "devDependencies": { "@vitejs/plugin-react": "^4.2.1",