-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·61 lines (56 loc) · 1.46 KB
/
cli.js
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
#!/usr/bin/env node
import {URL} from 'node:url'
import fs from 'node:fs'
import process from 'node:process'
import {soundex} from './index.js'
/** @type {import('type-fest').PackageJson} */
const pack = JSON.parse(
String(fs.readFileSync(new URL('package.json', import.meta.url)))
)
const argv = process.argv.slice(2)
if (argv.includes('--help') || argv.includes('-h')) {
console.log(help())
} else if (argv.includes('--version') || argv.includes('-v')) {
console.log(pack.version)
} else if (argv.length === 0) {
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (data) {
console.log(phonetics(String(data)))
})
} else {
console.log(phonetics(argv.join(' ')))
}
/** @param {string} values */
function phonetics(values) {
return values
.split(/\s+/g)
.map(function (value) {
return soundex(value)
})
.join(' ')
}
function help() {
return [
'',
' Usage: ' + pack.name + ' [options] <words...>',
'',
' ' + pack.description,
'',
' Options:',
'',
' -h, --help output usage information',
' -v, --version output version number',
'',
' Usage:',
'',
' # output phonetics',
' $ ' + pack.name + ' phonetics unicorn',
' ' + phonetics('phonetics unicorn'),
'',
' # output phonetics from stdin',
' $ echo "phonetics banana" | ' + pack.name,
' ' + phonetics('phonetics banana'),
''
].join('\n')
}