@@ -6,8 +6,12 @@ import fs from "fs";
66import  {  fileURLToPath  }  from  "url" ; 
77import  {  exec  }  from  "child_process" ; 
88import  {  promisify  }  from  "util" ; 
9+ import  {  satisfies  }  from  "compare-versions" ; 
910import  c  from  "picocolors" ; 
1011
12+ const  rescriptVersionRange  =  "~11 >=11.0.0-rc.6" ; 
13+ const  rescriptCoreVersionRange  =  ">=0.5.0" ; 
14+ 
1115// Get __dirname in an ES6 module 
1216const  __filename  =  fileURLToPath ( import . meta. url ) ; 
1317const  __dirname  =  path . dirname ( __filename ) ; 
@@ -31,6 +35,12 @@ const templates = [
3135  // }, 
3236] ; 
3337
38+ async  function  getPackageVersions ( packageName ,  range )  { 
39+   const  {  stdout }  =  await  promisify ( exec ) ( `npm view ${ packageName }   versions --json` ) ; 
40+   const  versions  =  JSON . parse ( stdout ) ; 
41+   return  versions . filter ( v  =>  satisfies ( v ,  range ) ) . reverse ( ) ; 
42+ } 
43+ 
3444function  checkCancel ( value )  { 
3545  if  ( p . isCancel ( value ) )  { 
3646    p . cancel ( "Project creation cancelled." ) ; 
@@ -107,25 +117,33 @@ async function main() {
107117  } ) ; 
108118  checkCancel ( templateName ) ; 
109119
110-   const  rescriptVersion  =  await  p . text ( { 
111-     message : "ReScript version? (keep the default if unsure)" , 
112-     initialValue : "11.0.0-rc.7" , 
113-   } ) ; 
114-   checkCancel ( rescriptVersion ) ; 
120+   try  { 
121+     const  s  =  p . spinner ( ) ; 
115122
116-   const  rescriptCoreVersion  =  await  p . text ( { 
117-     message : "ReScript Core version? (keep the default if unsure)" , 
118-     initialValue : "0.6.0" , 
119-   } ) ; 
120-   checkCancel ( rescriptCoreVersion ) ; 
123+     s . start ( "Loading available versions..." ) ; 
124+     const  [ rescriptVersions ,  rescriptCoreVersions ]  =  await  Promise . all ( [ 
125+       getPackageVersions ( "rescript" ,  rescriptVersionRange ) , 
126+       getPackageVersions ( "@rescript/core" ,  rescriptCoreVersionRange ) , 
127+     ] ) ; 
128+     s . stop ( "Versions loaded." ) ; 
121129
122-   const  templatePath  =  path . join ( __dirname ,  "templates" ,  templateName ) ; 
123-   const  projectPath  =  path . join ( process . cwd ( ) ,  projectName ) ; 
130+     const  rescriptVersion  =  await  p . select ( { 
131+       message : "ReScript version?" , 
132+       options : rescriptVersions . map ( v  =>  ( {  value : v  } ) ) , 
133+     } ) ; 
134+     checkCancel ( rescriptVersion ) ; 
124135
125-   const  s  =  p . spinner ( ) ; 
126-   s . start ( "Creating project..." ) ; 
136+     const  rescriptCoreVersion  =  await  p . select ( { 
137+       message : "ReScript Core version?" , 
138+       options : rescriptCoreVersions . map ( v  =>  ( {  value : v  } ) ) , 
139+     } ) ; 
140+     checkCancel ( rescriptCoreVersion ) ; 
141+ 
142+     const  templatePath  =  path . join ( __dirname ,  "templates" ,  templateName ) ; 
143+     const  projectPath  =  path . join ( process . cwd ( ) ,  projectName ) ; 
144+ 
145+     s . start ( "Creating project..." ) ; 
127146
128-   try  { 
129147    await  fs . promises . cp ( templatePath ,  projectPath ,  {  recursive : true  } ) ; 
130148    process . chdir ( projectPath ) ; 
131149
0 commit comments