Avoid code duplication by auto building the GUESSLANG_LANGUAGES list in langdetect-worker.js from the src/editor/languages.js:LANGUAGES list.

This commit is contained in:
Jonatan Heyman 2024-02-09 15:52:00 +01:00
parent d591d7ddb1
commit a7e0f53c35
3 changed files with 15 additions and 30 deletions

View File

@ -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()

View File

@ -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"]
}

View File

@ -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.