Skip to content

Commit

Permalink
Add support v12 alpha (#76)
Browse files Browse the repository at this point in the history
* Detect v12 and make rescript core optional

* Remove rescript packages from templates

* Update rescript.json

* Select v11 by default

* Fix prompt binding

* Remove switch
  • Loading branch information
nojaf authored Nov 30, 2024
1 parent 2d95888 commit 5ddbb65
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/ExistingJsProject.res
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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)
}
| _ => ()
}
)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/NewProject.res
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ let updateRescriptJson = async (~projectName, ~versions) =>
config->Dict.set("suffix", String(suffix))
| _ => ()
}

if Option.isNone(versions.rescriptCoreVersion) {
RescriptJsonUtils.removeRescriptCore(config)
}
| _ => ()
}
)
Expand Down Expand Up @@ -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({
Expand Down
29 changes: 29 additions & 0 deletions src/RescriptJsonUtils.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let removeRescriptCore = (config: Dict.t<JSON.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))
}
| _ => ()
}
}
33 changes: 24 additions & 9 deletions src/RescriptVersions.res
Original file line number Diff line number Diff line change
@@ -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<string>}

let getCompatibleRescriptCoreVersions = (~rescriptVersion, ~rescriptCoreVersions) =>
if CompareVersions.compareVersions(rescriptVersion, "11.1.0")->Ordering.isLess {
Expand Down Expand Up @@ -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
}

Expand All @@ -51,21 +56,31 @@ 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}
}

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".
Expand Down
2 changes: 1 addition & 1 deletion src/RescriptVersions.resi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type versions = {rescriptVersion: string, rescriptCoreVersion: string}
type versions = {rescriptVersion: string, rescriptCoreVersion: option<string>}

let promptVersions: unit => promise<versions>

Expand Down
1 change: 1 addition & 0 deletions src/bindings/ClackPrompts.res
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type selectOption = {
type selectOptions = {
message: string,
options: array<selectOption>,
initialValue?: string,
}

@module("@clack/prompts")
Expand Down
5 changes: 1 addition & 4 deletions templates/rescript-template-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@
],
"author": "",
"license": "MIT",
"dependencies": {
"@rescript/core": "^1.3.0",
"rescript": "^11.1.0"
}
"dependencies": {}
}
4 changes: 1 addition & 3 deletions templates/rescript-template-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
"author": "Patrick Ecker <ryyppy@users.noreply.github.com>",
"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": {
Expand Down
4 changes: 1 addition & 3 deletions templates/rescript-template-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5ddbb65

Please sign in to comment.