Skip to content

Commit c2ce1d5

Browse files
committed
feat(cli): automatic detection of TSTL TypeScript version
1 parent 413bdc7 commit c2ce1d5

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

packages/isaacscript-cli/file-templates/dynamic/package.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"devDependencies": {
2121
"isaacscript": "^0.0.1",
2222
"isaacscript-lint": "^0.0.1",
23-
"typescript": "5.5.2",
23+
"typescript": "0.0.1",
2424
"typescript-to-lua": "^0.0.1"
2525
}
2626
}

packages/isaacscript-cli/src/commands/init/createProject.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import chalk from "chalk";
22
import {
3+
isObject,
34
parseSemanticVersion,
45
removeLinesBetweenMarkers,
56
removeLinesMatching,
@@ -42,7 +43,7 @@ import {
4243
import { execShell, execShellString } from "../../exec.js";
4344
import { initGitRepository } from "../../git.js";
4445

45-
export function createProject(
46+
export async function createProject(
4647
projectName: string,
4748
authorName: string | undefined,
4849
projectPath: string,
@@ -55,7 +56,7 @@ export function createProject(
5556
typeScript: boolean,
5657
dev: boolean,
5758
verbose: boolean,
58-
): void {
59+
): Promise<void> {
5960
if (createNewDir) {
6061
makeDirectory(projectPath);
6162
}
@@ -66,7 +67,7 @@ export function createProject(
6667
}
6768

6869
copyStaticFiles(projectPath, typeScript);
69-
copyDynamicFiles(
70+
await copyDynamicFiles(
7071
projectName,
7172
authorName,
7273
projectPath,
@@ -130,7 +131,7 @@ function copyTemplateDirectoryWithoutOverwriting(
130131
}
131132

132133
/** Copy files that need to have text replaced inside of them. */
133-
function copyDynamicFiles(
134+
async function copyDynamicFiles(
134135
projectName: string,
135136
authorName: string | undefined,
136137
projectPath: string,
@@ -187,7 +188,7 @@ function copyDynamicFiles(
187188

188189
// `package.json`
189190
{
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.
191192
const packageJSONTemplateFileName = typeScript
192193
? "package.ts.json"
193194
: "package.mod.json";
@@ -197,10 +198,40 @@ function copyDynamicFiles(
197198
);
198199
const template = readFile(templatePath);
199200

200-
const packageJSON = template
201+
let packageJSON = template
201202
.replaceAll("PROJECT_NAME", projectName)
202203
.replaceAll("AUTHOR_NAME", authorName ?? "unknown");
203204

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+
204235
const destinationPath = path.join(projectPath, "package.json");
205236
writeFile(destinationPath, packageJSON);
206237
}

packages/isaacscript-cli/src/commands/init/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ async function init(
194194
: await getSaveSlot(saveSlotOption, yes);
195195

196196
// Now that we have asked the user all of the questions we need, we can create the project.
197-
createProject(
197+
await createProject(
198198
projectName,
199199
authorName,
200200
projectPath,

0 commit comments

Comments
 (0)