Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Convert the dl script to ts (#289)

Sean Boult3c3ec5a87641
diff --git a/next.config.mjs b/next.config.mjs
index a0596351..78b04dc8 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -2,8 +2,7 @@
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
-await import('./src/env.mjs');
-await import('./scripts/dl-monaco.mjs').then(({ download }) => download());
+import './src/env.mjs';
/** @type {import("next").NextConfig} */
const config = {
webpack: (config) => {
diff --git a/package.json b/package.json
index ec76bfa9..e9ca2a89 100644
--- a/package.json
+++ b/package.json
@@ -13,8 +13,8 @@
"refresh": "pnpm reset && pnpm prisma db seed",
"typecheck": "tsc --noEmit",
"prepare": "husky install",
- "dl-monaco": "node scripts/dl-monaco.mjs",
- "dl-monaco:force": "node scripts/dl-monaco.mjs --force"
+ "dl-monaco": "tsx scripts/dl-monaco.ts",
+ "dl-monaco:force": "tsx scripts/dl-monaco.ts --force"
},
"type": "module",
"dependencies": {
diff --git a/scripts/dl-monaco.mjs b/scripts/dl-monaco.mjs
deleted file mode 100644
index 4e976508..00000000
--- a/scripts/dl-monaco.mjs
+++ /dev/null
@@ -1,87 +0,0 @@
-//@ts-check
-
-import url from 'node:url';
-import fs from 'node:fs/promises';
-import path from 'node:path';
-
-/** @link https://ktypescript.azureedge.net/indexes/releases.json */
-const typescriptVersion = '5.1.6';
-
-/** @link https://github.com/microsoft/TypeScript-Make-Monaco-Builds */
-const baseUrl = `https://typescript.azureedge.net/cdn/${typescriptVersion}/monaco`;
-
-/**
- * @param {{url: URL, options?: object, retries: number}} param0
- * @returns {Promise<Response>}
- */
-async function fetchFile({ url, options, retries }) {
- const fileResponse = await fetch(url, options);
-
- if (!fileResponse.ok && retries > 0) {
- const persistentRetryValue = retries - 1;
- return fetchFile({ url, options, retries: persistentRetryValue });
- }
-
- return fileResponse;
-}
-
-/**
- * @param {readonly [remoteUrl: string, diskPath:string]} param0
- */
-async function downloadAndSaveFile([remoteUrl, diskPath]) {
- const file = await (
- await fetchFile({
- url: new URL(baseUrl + remoteUrl),
- retries: 5,
- })
- ).text();
-
- const pathToSave = url.fileURLToPath(path.join(import.meta.url, '../../', diskPath));
-
- const dir = path.join(pathToSave, '../');
-
- await fs.mkdir(dir, { recursive: true });
-
- await fs.writeFile(pathToSave, file, {});
-}
-
-const files = /** @type {const} */ ([
- ['/esm/vs/editor/editor.api.d.ts', './monaco-editor.d.ts'],
- ['/min/vs/loader.js', './public/vs/loader.js'],
- ['/min/vs/editor/editor.main.js', './public/vs/editor/editor.main.js'],
- ['/min/vs/editor/editor.main.css', './public/vs/editor/editor.main.css'],
- ['/min/vs/editor/editor.main.nls.js', './public/vs/editor/editor.main.nls.js'],
- [
- '/min/vs/basic-languages/typescript/typescript.js',
- './public/vs/basic-languages/typescript/typescript.js',
- ],
- ['/min/vs/language/typescript/tsMode.js', './public/vs/language/typescript/tsMode.js'],
- ['/min/vs/base/worker/workerMain.js', './public/vs/base/worker/workerMain.js'],
- [
- '/min/vs/base/common/worker/simpleWorker.nls.js',
- './public/vs/base/common/worker/simpleWorker.nls.js',
- ],
- ['/min/vs/language/typescript/tsWorker.js', './public/vs/language/typescript/tsWorker.js'],
- [
- '/min/vs/base/common/worker/simpleWorker.nls.js',
- './public/vs/base/common/worker/simpleWorker.nls.js',
- ],
-]);
-
-export const download = async (force = false) => {
- if (
- force ||
- (await fs
- .open(url.fileURLToPath(path.join(import.meta.url, '../../', files[0][1])))
- .then((f) => f.close())
- .then(() => false)
- .catch(() => true))
- ) {
- console.log('downloading monaco-editor');
- return Promise.all(files.map(downloadAndSaveFile));
- }
-};
-
-if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
- download(process.argv[2] === '--force');
-}
diff --git a/scripts/dl-monaco.ts b/scripts/dl-monaco.ts
new file mode 100644
index 00000000..4692a96b
--- /dev/null
+++ b/scripts/dl-monaco.ts
@@ -0,0 +1,106 @@
+import url from 'node:url';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+
+/** @link https://typescript.azureedge.net/indexes/releases.json */
+const TYPESCRIPT_VERSION = '5.1.6';
+
+/** @link https://github.com/microsoft/TypeScript-Make-Monaco-Builds */
+const TS_CDN_BASE_URL = `https://typescript.azureedge.net/cdn/${TYPESCRIPT_VERSION}/monaco`;
+
+async function fetchFile({
+ url,
+ options,
+ retries,
+}: {
+ url: URL;
+ options?: object;
+ retries: number;
+}): Promise<Response> {
+ const fileResponse = await fetch(url, options);
+
+ if (!fileResponse.ok && retries > 0) {
+ const persistentRetryValue = retries - 1;
+ return fetchFile({ url, options, retries: persistentRetryValue });
+ }
+
+ return fileResponse;
+}
+
+async function downloadAndSaveFile([remoteUrl, diskPath, appendToStart = '']: readonly [
+ remoteUrl: string,
+ diskPath: string,
+ appendToStart?: string,
+]) {
+ const textToWrite = await (
+ await fetchFile({
+ url: new URL(TS_CDN_BASE_URL + remoteUrl),
+ retries: 5,
+ })
+ ).text();
+
+ const pathToSave = url.fileURLToPath(path.join(import.meta.url, '../../', diskPath));
+ const dir = path.join(pathToSave, '../');
+ await fs.mkdir(dir, { recursive: true });
+ await fs.writeFile(pathToSave, `${appendToStart}\n${textToWrite}`, {});
+}
+
+const files = [
+ [
+ '/esm/vs/editor/editor.api.d.ts',
+ './monaco-editor.d.ts',
+ `// GENERATED WITH TS: ${TYPESCRIPT_VERSION}\n`,
+ ],
+ ['/min/vs/loader.js', './public/vs/loader.js'],
+ ['/min/vs/editor/editor.main.js', './public/vs/editor/editor.main.js'],
+ ['/min/vs/editor/editor.main.css', './public/vs/editor/editor.main.css'],
+ ['/min/vs/editor/editor.main.nls.js', './public/vs/editor/editor.main.nls.js'],
+ [
+ '/min/vs/basic-languages/typescript/typescript.js',
+ './public/vs/basic-languages/typescript/typescript.js',
+ ],
+ ['/min/vs/language/typescript/tsMode.js', './public/vs/language/typescript/tsMode.js'],
+ ['/min/vs/base/worker/workerMain.js', './public/vs/base/worker/workerMain.js'],
+ [
+ '/min/vs/base/common/worker/simpleWorker.nls.js',
+ './public/vs/base/common/worker/simpleWorker.nls.js',
+ ],
+ ['/min/vs/language/typescript/tsWorker.js', './public/vs/language/typescript/tsWorker.js'],
+ [
+ '/min/vs/base/common/worker/simpleWorker.nls.js',
+ './public/vs/base/common/worker/simpleWorker.nls.js',
+ ],
+] as const;
+
+async function isTSversionCorrect(path: string) {
+ try {
+ const f = await fs.open(path, 'r');
+
+ const fileReadBuffer = (await f.read({ length: files[0][2].length })).buffer;
+ await f.close();
+
+ return fileReadBuffer.toString().startsWith(files[0][2]);
+ } catch {
+ return false;
+ }
+}
+
+// check if the force flag is present
+const force = process.argv.includes('--force');
+
+// get the path to the root monaco-editor.d.ts file
+const dTSPath = url.fileURLToPath(path.join(import.meta.url, '../../', files[0][1]));
+
+// is the version exciting && correct
+const isCorrectVersionInstalled = await isTSversionCorrect(dTSPath);
+
+if (force || !isCorrectVersionInstalled) {
+ if (force) {
+ console.log(`🚨 Using force ovrwrites public/vs and monaco-editor.d.ts`);
+ }
+ console.log(`🌍 Downloading monaco-editor with typescript: ${TYPESCRIPT_VERSION}`);
+ await Promise.all(files.map(downloadAndSaveFile));
+ process.exit(0);
+}
+
+console.log(`βœ… monaco-editor with typescript: ${TYPESCRIPT_VERSION} already installed`);
diff --git a/scripts/truncate.js b/scripts/truncate.ts
similarity index 100%
rename from scripts/truncate.js
rename to scripts/truncate.ts
diff --git a/tsconfig.json b/tsconfig.json
index 3b1ec233..086bf983 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -31,15 +31,10 @@
"next-env.d.ts",
"global.d.ts",
"*.ts",
- "./scripts/*.js",
- "./scripts/*.mjs",
- "./scripts/*.cjs",
+ "./scripts/*.ts",
"./src/**/*.ts",
"./src/**/*.tsx",
- "./src/**/*.cjs",
- "./src/**/*.mjs",
".next/types/**/*.ts",
- "scripts/dl-monaco.mjs",
"./prisma/*.ts"
],
"exclude": ["node_modules", ".next"]