From f3282e4dade67f1241ec5a7672fee9da0f1a1a43 Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Wed, 14 Feb 2024 23:32:15 +0100 Subject: [PATCH] chore: sort cspell dictionaries on commit --- .cspell/contributors.txt | 2 +- .lintstagedrc.mjs | 2 +- scripts/fixCSpell.ts | 39 +++++++++++++++++++++++++-------------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/.cspell/contributors.txt b/.cspell/contributors.txt index 78affefa5..bd3ad9da2 100644 --- a/.cspell/contributors.txt +++ b/.cspell/contributors.txt @@ -1,8 +1,8 @@ # Contributors to mermaidjs, one per line +Ashish Jain cpettitt Dong Cai Nikolay Rozhkov Peng Xiao subhash-halder Vinod Sidharth -Ashish Jain diff --git a/.lintstagedrc.mjs b/.lintstagedrc.mjs index 231c91f8f..86af4f513 100644 --- a/.lintstagedrc.mjs +++ b/.lintstagedrc.mjs @@ -6,6 +6,6 @@ export default { // https://prettier.io/docs/en/cli.html#--cache 'prettier --write', ], - 'cSpell.json': ['tsx scripts/fixCSpell.ts'], + '.cspell/*.txt': ['tsx scripts/fixCSpell.ts'], '**/*.jison': ['pnpm -w run lint:jison'], }; diff --git a/scripts/fixCSpell.ts b/scripts/fixCSpell.ts index 1d15e2194..f35a615c2 100644 --- a/scripts/fixCSpell.ts +++ b/scripts/fixCSpell.ts @@ -5,20 +5,31 @@ * (i.e. the root of the Mermaid project). */ -import { readFileSync, writeFileSync } from 'node:fs'; -import prettier from 'prettier'; +import { readFileSync, writeFileSync, readdirSync } from 'node:fs'; +import { join } from 'node:path'; -const filepath = './cSpell.json'; -const cSpell: { words: string[] } = JSON.parse(readFileSync(filepath, 'utf8')); +const cSpellDictionaryDir = './.cspell'; -cSpell.words = [...new Set(cSpell.words.map((word) => word.toLowerCase()))]; -cSpell.words.sort((a, b) => a.localeCompare(b)); +function sortWordsInFile(filepath: string) { + const words = readFileSync(filepath, 'utf8') + .split('\n') + .map((word) => word.trim()) + .filter((word) => word); + words.sort((a, b) => a.localeCompare(b)); -const prettierConfig = prettier.resolveConfig.sync(filepath) ?? {}; -writeFileSync( - filepath, - prettier.format(JSON.stringify(cSpell), { - ...prettierConfig, - filepath, - }) -); + writeFileSync(filepath, words.join('\n') + '\n', 'utf8'); +} + +function findDictionaries() { + const files = readdirSync(cSpellDictionaryDir, { withFileTypes: true }) + .filter((dir) => dir.isFile()) + .filter((dir) => dir.name.endsWith('.txt')); + return files.map((file) => join(cSpellDictionaryDir, file.name)); +} + +function main() { + const files = findDictionaries(); + files.forEach(sortWordsInFile); +} + +main();