Skip to content

Commit

Permalink
Avoid code duplication by auto building the GUESSLANG_LANGUAGES list …
Browse files Browse the repository at this point in the history
…in langdetect-worker.js from the src/editor/languages.js:LANGUAGES list.
  • Loading branch information
heyman committed Feb 9, 2024
1 parent d591d7d commit a7e0f53
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
27 changes: 1 addition & 26 deletions public/langdetect-worker.js
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
16 changes: 13 additions & 3 deletions vite.config.ts → vite.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'node:fs'
import { rmSync } from 'node:fs'
import { join } from 'node:path'
import { defineConfig } from 'vite'
Expand All @@ -9,15 +10,14 @@ 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 })

const isDevelopment = process.env.NODE_ENV === "development" || !!process.env.VSCODE_DEBUG
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
Expand All @@ -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: {
Expand All @@ -48,6 +57,7 @@ export default defineConfig({
plugins: [
vue(),
updateReadmeKeybinds(),
updateGuesslangLanguagesInWebWorker(),
electron([
{
// Main-Process entry file of the Electron App.
Expand Down

0 comments on commit a7e0f53

Please sign in to comment.