Skip to content

Commit

Permalink
Add, update and remove spellers in the app when speller files are cha…
Browse files Browse the repository at this point in the history
…nged
  • Loading branch information
albbas committed Mar 12, 2021
1 parent 31a6a47 commit cbaa052
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
"use strict";
const path = require("path");
const fs = require("fs");
const l = require("lodash");
const SpellChecker = require("divvunspell");
const Promise = require("bluebird");
const chokidar = require("chokidar")

const H = require('./helpers');

// - - -

const langDirectory = process.env.CKOSPELL_LANG_PATH || path.resolve(process.cwd(), "etc");

const langFiles = fs.readdirSync(langDirectory)
.filter((filename) => path.extname(filename) === ".zhfst");

const filenameMap = {}
const localeNameMap = {}
const spellers = {}

langFiles.forEach(file => {
const fp = path.join(langDirectory, file)
const addFile = fp => {
try {
const speller = new SpellChecker(fp)
const locale = speller.locale

filenameMap[fp] = locale
localeNameMap[locale] = speller.localeName
spellers[locale] = speller
} catch(error) {
process.stdout.write(`Cannot load ${fp}\n`)
process.stdout.write(`${error.message}`)
process.stdout.write(`${error.message}\n`)
}
})
}

const removeFile = fp => {
const locale = filenameMap[fp]

delete filenameMap[fp]
delete localeNameMap[locale]
delete spellers[locale]
}

chokidar.watch(langDirectory + '/*.zhfst', {awaitWriteFinish: true})
.on('add', path => {
process.stdout.write(`Add, ${path}\n`)
addFile(path)
})
.on('change', path => {
process.stdout.write(`Update, ${path}\n`)
addFile(path)
})
.on('unlink', path => {
process.stdout.write(`Remove, ${path}\n`)
removeFile(path)
});

/**
* Get Banner
Expand Down

0 comments on commit cbaa052

Please sign in to comment.