generated from feremabraz/script-executable-publishable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (57 loc) · 1.64 KB
/
index.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
62
63
64
#!/usr/bin/env node
import fs from 'fs'
import path from 'path'
import url from 'url'
const args = process.argv.slice(2)
const argv = {}
args.forEach((arg) => {
if (arg.indexOf('--') === 0) {
arg = arg.substring(2)
const index = arg.indexOf('=')
if (index > 0) {
argv[arg.substring(0, index)] = arg.substring(index + 1)
} else {
argv[arg] = true
}
}
})
if (argv.debug) {
console.log(argv)
console.log(args)
args.splice(args.indexOf('--debug'), 1)
}
if (argv.help) {
console.log('This is a CLI template.')
console.log('')
console.log('Usage: cli-executable-publishable [options]')
console.log('')
console.log('Options:')
console.log(' --help Shows this help.')
console.log(' --debug Prints passed options and their arguments.')
console.log(' --version Current version.')
console.log(' --hello Echoes the given argument.')
console.log('')
console.log('Examples:')
console.log(' $ cli-executable-publishable --hello=world')
console.log(' $ cli-executable-publishable --hello world')
process.exit(0)
}
if (argv.version) {
const filename = url.fileURLToPath(import.meta.url)
const packagePath = `${path.dirname(filename)}/package.json`
const packageJSON = fs.readFileSync(packagePath)
const version = JSON.parse(packageJSON).version
console.log(version)
process.exit(0)
}
if (argv.hello) {
const value = args[args.findIndex((item) => item === '--hello') + 1]
if (value.indexOf('=') > 0) {
console.log(`Hello ${value.substring(value.indexOf('=') + 1)}!`)
} else {
console.log(`Hello ${value}!`)
}
process.exit(0)
}
console.log('Unknown command.')
process.exit(1)