chore: sort cspell dictionaries on commit

This commit is contained in:
Jason Dent 2024-02-14 23:32:15 +01:00
parent 4a7489a7b6
commit f3282e4dad
3 changed files with 27 additions and 16 deletions

View File

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

View File

@ -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'],
};

View File

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