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" ;
2025-02-25 08:07:03 +08:00
import spawn from "nano-spawn" ;
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 ) =>
2022-01-08 12:14:07 +08:00
fs . writeFile ( CSPELL_CONFIG_FILE , JSON . stringify ( config , undefined , 4 ) + "\n" );
2025-02-25 08:07:03 +08:00
const runSpellcheck = ( args , options ) =>
spawn ( "yarn" , [ "lint:spellcheck" , ... args ], options );
2020-04-12 01:19:00 +08:00
2022-04-03 01:42:20 +08:00
console . log ( "Empty words ..." );
const config = JSON . parse ( await fs . readFile ( CSPELL_CONFIG_FILE ));
const original = config . words ;
await updateConfig ({ ... config , words : [] });
2020-04-12 01:19:00 +08:00
2022-04-03 01:42:20 +08:00
console . log ( "Running spellcheck with empty words ..." );
2025-02-25 08:07:03 +08:00
let stdout = "" ;
try {
await runSpellcheck ([ "--words-only" , "--unique" ]);
} catch ( error ) {
({ stdout } = error );
}
2022-04-03 01:42:20 +08:00
const words = stdout
2025-02-25 08:07:03 +08:00
? 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 ()))
: [];
2022-04-03 01:42:20 +08:00
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 ..." );
2025-02-25 08:07:03 +08:00
await runSpellcheck ([], { stdio : "inherit" });
2022-04-03 01:42:20 +08:00
console . log ( "CSpell config file updated." );