1
1
import chalk from "chalk" ;
2
2
import {
3
+ isObject ,
3
4
parseSemanticVersion ,
4
5
removeLinesBetweenMarkers ,
5
6
removeLinesMatching ,
@@ -42,7 +43,7 @@ import {
42
43
import { execShell , execShellString } from "../../exec.js" ;
43
44
import { initGitRepository } from "../../git.js" ;
44
45
45
- export function createProject (
46
+ export async function createProject (
46
47
projectName : string ,
47
48
authorName : string | undefined ,
48
49
projectPath : string ,
@@ -55,7 +56,7 @@ export function createProject(
55
56
typeScript : boolean ,
56
57
dev : boolean ,
57
58
verbose : boolean ,
58
- ) : void {
59
+ ) : Promise < void > {
59
60
if ( createNewDir ) {
60
61
makeDirectory ( projectPath ) ;
61
62
}
@@ -66,7 +67,7 @@ export function createProject(
66
67
}
67
68
68
69
copyStaticFiles ( projectPath , typeScript ) ;
69
- copyDynamicFiles (
70
+ await copyDynamicFiles (
70
71
projectName ,
71
72
authorName ,
72
73
projectPath ,
@@ -130,7 +131,7 @@ function copyTemplateDirectoryWithoutOverwriting(
130
131
}
131
132
132
133
/** Copy files that need to have text replaced inside of them. */
133
- function copyDynamicFiles (
134
+ async function copyDynamicFiles (
134
135
projectName : string ,
135
136
authorName : string | undefined ,
136
137
projectPath : string ,
@@ -187,7 +188,7 @@ function copyDynamicFiles(
187
188
188
189
// `package.json`
189
190
{
190
- // There are two versions of the template, one for TypeScript, and one for IsaacScript mods.
191
+ // There are two versions of the template: one for TypeScript and one for IsaacScript mods.
191
192
const packageJSONTemplateFileName = typeScript
192
193
? "package.ts.json"
193
194
: "package.mod.json" ;
@@ -197,10 +198,40 @@ function copyDynamicFiles(
197
198
) ;
198
199
const template = readFile ( templatePath ) ;
199
200
200
- const packageJSON = template
201
+ let packageJSON = template
201
202
. replaceAll ( "PROJECT_NAME" , projectName )
202
203
. replaceAll ( "AUTHOR_NAME" , authorName ?? "unknown" ) ;
203
204
205
+ if ( ! typeScript ) {
206
+ // We need the get the version of TypeScript that corresponds to the latest version of
207
+ // TypeScriptToLua.
208
+ const response = await fetch (
209
+ "https://raw.githubusercontent.com/TypeScriptToLua/TypeScriptToLua/refs/heads/master/package.json" ,
210
+ ) ;
211
+ const TSTLPackageJSON = await response . json ( ) ;
212
+ if ( ! isObject ( TSTLPackageJSON ) ) {
213
+ throw new Error ( "Failed to parse the TSTL package.json file." ) ;
214
+ }
215
+ const { peerDependencies } = TSTLPackageJSON ;
216
+ if ( ! isObject ( peerDependencies ) ) {
217
+ throw new Error (
218
+ "Failed to parse the peer dependencies in the TSTL package.json file." ,
219
+ ) ;
220
+ }
221
+
222
+ const TSTLTypeScriptVersion = peerDependencies [ "typescript" ] ;
223
+ if ( typeof TSTLTypeScriptVersion !== "string" ) {
224
+ throw new TypeError (
225
+ 'Failed to parse the "typescript" peer dependency in the TSTL package.json file.' ,
226
+ ) ;
227
+ }
228
+
229
+ packageJSON = packageJSON . replaceAll (
230
+ '"typescript": "0.0.1"' ,
231
+ `"typescript": "${ TSTLTypeScriptVersion } "` ,
232
+ ) ;
233
+ }
234
+
204
235
const destinationPath = path . join ( projectPath , "package.json" ) ;
205
236
writeFile ( destinationPath , packageJSON ) ;
206
237
}
0 commit comments