2020-04-12 01:19:00 +08:00
#!/usr/bin/env node
2021-05-15 20:09:52 +08:00
import fs from "node:fs/promises" ;
import execa from "execa" ;
2020-04-12 01:19:00 +08:00
2021-05-15 20:09:52 +08:00
const CSPELL_CONFIG_FILE = new URL ( "../cspell.json" , import . meta . url );
2021-06-16 22:20:47 +08:00
const updateConfig = ( config ) =>
fs . writeFile ( CSPELL_CONFIG_FILE , JSON . stringify ( config , undefined , 4 ));
2022-01-03 22:17:14 +08:00
const runSpellcheck = ( options ) => {
const { yarnArgs , args , execaOptions } = {
yarnArgs : [],
args : [],
... options ,
};
return execa ( "yarn" , [... yarnArgs , "lint:spellcheck" , ... args ], execaOptions );
};
2020-04-12 01:19:00 +08:00
( async () => {
console . log ( "Empty words ..." );
2022-01-04 13:56:10 +08:00
const config = JSON . parse ( await fs . readFile ( CSPELL_CONFIG_FILE ));
2022-01-03 22:17:14 +08:00
const original = config . words ;
2021-06-16 22:20:47 +08:00
await updateConfig ({ ... config , words : [] });
2020-04-12 01:19:00 +08:00
console . log ( "Running spellcheck with empty words ..." );
2022-01-03 22:17:14 +08:00
const { stdout } = await runSpellcheck ({
yarnArgs : [ "--silent" ],
args : [ "--words-only" , "--unique" ],
execaOptions : { reject : false },
});
const words = stdout
. split ( "\n" )
2020-04-12 01:19:00 +08:00
// Remove upper case word, if lower case one already exists
2022-01-03 22:17:14 +08:00
. filter (( word , _ , words ) => {
2020-04-12 01:19:00 +08:00
const lowerCased = word . toLowerCase ();
return lowerCased === word || ! words . includes ( lowerCased );
2022-01-03 22:17:14 +08:00
})
2020-04-12 01:19:00 +08:00
// Compare function from https://github.com/streetsidesoftware/vscode-spell-checker/blob/2fde3bc5c658ee51da5a56580aa1370bf8174070/packages/client/src/settings/CSpellSettings.ts#L78
2022-01-03 22:17:14 +08:00
. sort (( a , b ) => a . toLowerCase (). localeCompare ( b . toLowerCase ()));
config . words = words ;
2020-04-12 01:19:00 +08:00
2022-01-03 22:17:14 +08:00
const removed = original . filter (( word ) => ! words . includes ( word ));
2021-06-16 22:20:47 +08:00
if ( removed . length > 0 ) {
console . log (
` ${ removed . length } words removed: \n ${ removed
. map (( word ) => ` - ${ word } ` )
. join ( "\n" ) } `
);
}
2022-01-03 22:17:14 +08:00
const added = words . filter (( word ) => ! original . includes ( word ));
2021-06-16 22:20:47 +08:00
if ( added . length > 0 ) {
console . log (
` ${ added . length } words added: \n ${ added
. map (( word ) => ` - ${ word } ` )
. join ( "\n" ) } `
);
}
2020-04-12 01:19:00 +08:00
console . log ( "Updating words ..." );
2021-06-16 22:20:47 +08:00
await updateConfig ( config );
2020-04-12 01:19:00 +08:00
console . log ( "Running spellcheck with new words ..." );
2022-01-03 22:17:14 +08:00
await runSpellcheck ({ execaOptions : { stdout : "inherit" } });
2020-04-12 01:19:00 +08:00
console . log ( "CSpell config file updated." );
2021-06-16 22:20:47 +08:00
})(). catch (( error ) => {
console . error ( error );
process . exit ( 1 );
});