-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
94 lines (83 loc) · 2.81 KB
/
cli.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
import { Command } from 'commander'
import { generateDictionary } from './utils.js'
import { LockfileDictionariesConfig } from './config.js'
import { readFileSync } from 'fs'
import { fileURLToPath } from 'url'
import { dirname, resolve } from 'path'
// Get package version from package.json
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const packageJson = JSON.parse(
readFileSync(resolve(__dirname, '../package.json'), 'utf8')
)
const program = new Command()
program
.name('cspell-lockfile-dicts')
.description('Generate dictionary files from package lockfiles')
.version(packageJson.version)
program
.option('-d, --debug', 'Enable debug logging')
.option(
'-p, --path <path>',
'Path to save the dictionary file',
'.cspell/lockfile-words.txt'
)
.option(
'-l, --lockfiles <files...>',
'Specific lockfiles to process (space-separated). When specified, auto-detection is disabled.'
)
.option(
'-a, --auto-detect-patterns <patterns...>',
'Glob patterns for auto-detecting lockfiles (space-separated). Only used when --lockfiles is not specified.',
[
'**/package-lock.json',
'**/yarn.lock',
'**/Gemfile.lock',
'**/composer.lock',
'**/Cargo.lock',
]
)
program.action(async (options) => {
console.log('🔍 Generating dictionary from lockfiles...')
// Auto-detection is enabled by default, but disabled when lockfiles are explicitly specified
const autoDetect = options.lockfiles ? false : true
const config: LockfileDictionariesConfig = {
debug: options.debug || false,
dictionaryPath: options.path,
autoDetect: autoDetect,
lockfiles: options.lockfiles,
autoDetectPatterns: options.autoDetectPatterns,
}
try {
const words = await generateDictionary(config)
if (words.length > 0) {
console.log(
`✅ Dictionary generated with ${words.length} words at ${config.dictionaryPath}`
)
console.log('')
console.log('To use this dictionary in your cspell configuration:')
console.log('')
console.log('1. Add the following to your cspell.json file:')
console.log('')
console.log('{')
console.log(' "dictionaryDefinitions": [')
console.log(' {')
console.log(' "name": "lockfile-words",')
console.log(' "path": "./.cspell/lockfile-words.txt",')
console.log(
' "description": "Dictionary of words extracted from lockfiles"'
)
console.log(' }')
console.log(' ],')
console.log(' "dictionaries": ["lockfile-words"]')
console.log('}')
} else {
console.log('❌ No words were found in lockfiles')
}
} catch (error) {
console.error('❌ Error generating dictionary:', error)
process.exit(1)
}
})
program.parse(process.argv)