forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean-cspell.js
More file actions
64 lines (54 loc) · 1.97 KB
/
clean-cspell.js
File metadata and controls
64 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
import fs from "node:fs/promises";
import { execa } from "execa";
const CSPELL_CONFIG_FILE = new URL("../cspell.json", import.meta.url);
const updateConfig = (config) =>
fs.writeFile(CSPELL_CONFIG_FILE, JSON.stringify(config, undefined, 4) + "\n");
const runSpellcheck = (options) => {
const { yarnArgs, args, execaOptions } = {
yarnArgs: [],
args: [],
...options,
};
return execa("yarn", [...yarnArgs, "lint:spellcheck", ...args], execaOptions);
};
console.log("Empty words ...");
const config = JSON.parse(await fs.readFile(CSPELL_CONFIG_FILE));
const original = config.words;
await updateConfig({ ...config, words: [] });
console.log("Running spellcheck with empty words ...");
const { stdout } = await runSpellcheck({
args: ["--words-only", "--unique"],
execaOptions: { reject: false },
});
const words = stdout
.split("\n")
// Remove upper case word, if lower case one already exists
.filter((word, _, words) => {
const lowerCased = word.toLowerCase();
return lowerCased === word || !words.includes(lowerCased);
})
// Compare function from https://github.com/streetsidesoftware/vscode-spell-checker/blob/2fde3bc5c658ee51da5a56580aa1370bf8174070/packages/client/src/settings/CSpellSettings.ts#L78
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
config.words = words;
const removed = original.filter((word) => !words.includes(word));
if (removed.length > 0) {
console.log(
`${removed.length} words removed: \n${removed
.map((word) => ` - ${word}`)
.join("\n")}`,
);
}
const added = words.filter((word) => !original.includes(word));
if (added.length > 0) {
console.log(
`${added.length} words added: \n${added
.map((word) => ` - ${word}`)
.join("\n")}`,
);
}
console.log("Updating words ...");
await updateConfig(config);
console.log("Running spellcheck with new words ...");
await runSpellcheck({ execaOptions: { stdout: "inherit" } });
console.log("CSpell config file updated.");