From a7e0f53c358983fc857cdc7905296ee8328c4b54 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Fri, 9 Feb 2024 15:52:00 +0100 Subject: [PATCH] Avoid code duplication by auto building the GUESSLANG_LANGUAGES list in langdetect-worker.js from the src/editor/languages.js:LANGUAGES list. --- public/langdetect-worker.js | 27 +-------------------------- tsconfig.node.json | 2 +- vite.config.ts => vite.config.mjs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 30 deletions(-) rename vite.config.ts => vite.config.mjs (83%) diff --git a/public/langdetect-worker.js b/public/langdetect-worker.js index 47ba638..95ca257 100644 --- a/public/langdetect-worker.js +++ b/public/langdetect-worker.js @@ -1,31 +1,6 @@ importScripts("guesslang.min.js") -GUESSLANG_LANGUAGES = [ - "json", - "py", - "js", - "ts", - "html", - "sql", - "java", - "cpp", - "php", - "css", - "xml", - "rs", - "md", - "cs", - "rb", - "sh", - "yaml", - "go", - "clj", - "erl", - "toml", - "swift", - "kt", - "groovy", -] +GUESSLANG_LANGUAGES = ["json","py","html","sql","md","java","php","css","xml","cpp","rs","cs","rb","sh","yaml","toml","go","clj","erl","js","ts","swift","kt","groovy","ps1"] const guessLang = new self.GuessLang() diff --git a/tsconfig.node.json b/tsconfig.node.json index 9d3c6d6..050be03 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -6,5 +6,5 @@ "resolveJsonModule": true, "allowSyntheticDefaultImports": true }, - "include": ["vite.config.ts", "package.json", "electron", "shared-utils"] + "include": ["vite.config.mjs", "package.json", "electron", "shared-utils"] } diff --git a/vite.config.ts b/vite.config.mjs similarity index 83% rename from vite.config.ts rename to vite.config.mjs index 2af59d5..a359119 100644 --- a/vite.config.ts +++ b/vite.config.mjs @@ -1,3 +1,4 @@ +import * as fs from 'node:fs' import { rmSync } from 'node:fs' import { join } from 'node:path' import { defineConfig } from 'vite' @@ -9,6 +10,7 @@ import pkg from './package.json' import path from 'path' import { keyHelpStr } from "./shared-utils/key-helper"; +import { LANGUAGES }  from "./src/editor/languages" rmSync('dist-electron', { recursive: true, force: true }) @@ -16,8 +18,6 @@ const isDevelopment = process.env.NODE_ENV === "development" || !!process.env.VS const isProduction = process.env.NODE_ENV === "production" const updateReadmeKeybinds = async () => { - const fs = require('fs') - const path = require('path') const readmePath = path.resolve(__dirname, 'README.md') let readme = fs.readFileSync(readmePath, 'utf-8') const keybindsRegex = /^(### What are the default keyboard shortcuts\?\s*).*?^(```\s+#)/gms @@ -32,11 +32,20 @@ ${keyHelpStr('darwin')} \`\`\` ${keyHelpStr('win32')} $2` - readme = readme.replace(keybindsRegex, shortcuts) fs.writeFileSync(readmePath, readme) } +const updateGuesslangLanguagesInWebWorker = async () => { + const langDetectWorkerPath = path.resolve(__dirname, 'public', 'langdetect-worker.js') + let workerFileContents = fs.readFileSync(langDetectWorkerPath, 'utf-8') + const langListRegex = /^GUESSLANG_LANGUAGES = \[[^\]]+\]/gms + const newLangList = JSON.stringify(LANGUAGES.map(l => l.guesslang).filter(l => l !== null)) + workerFileContents = workerFileContents.replace(langListRegex, `GUESSLANG_LANGUAGES = ${newLangList}`) + fs.writeFileSync(langDetectWorkerPath, workerFileContents) +} + + // https://vitejs.dev/config/ export default defineConfig({ resolve: { @@ -48,6 +57,7 @@ export default defineConfig({ plugins: [ vue(), updateReadmeKeybinds(), + updateGuesslangLanguagesInWebWorker(), electron([ { // Main-Process entry file of the Electron App.