-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·88 lines (76 loc) · 2.17 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
const fs = require('fs')
const minimist = require('minimist')
const { version } = require('./package.json')
const getParameters = require('./get-parameters')
const opts = minimist(process.argv.slice(2))
const paths = opts._
if (opts.v || opts.version) {
logInfo(`v${version}`)
}
if (opts.h || opts.help) {
logInfo(`Usage: psenv [OPTION]... <PATH>...
Options:
--output=FILENAME write to a file (e.g. --output=.env)
--to-upper-case convert the name to upper case (e.g. name to NAME)
--recursive retrieve all parameters within a hierarchy
--is-dotenv output with the format NAME=value
--is-cmd output for Windows Command Prompt (cmd.exe)
-h, --help print this message
-v, --version print the current version of psenv`)
}
if (paths.length === 0) {
logInfo(`Usage: psenv [OPTION]... <PATH>...
Try 'psenv --help' for more information.`)
}
// Every path must starts with a slash ('/')
for (let path of paths) {
if (!path.startsWith('/'))
logError(`Invalid path: ${path}`)
}
getParameters(paths, opts.recursive)
.then(processParameters)
.catch(() => logError('Unable to get parameters.'))
function processParameters(parameters) {
const variables = parameters
.flat() // The initial 'parameters' value will be a array of arrays
.map(({ Name, Value }) => toVariable(Name, Value))
.join('\n')
if (opts.output) {
fs.writeFile(opts.output, raw, (error) => {
if (error) logError(`Unable to create the file.`)
logInfo('File is created successfully.')
})
} else {
logInfo(variables)
}
}
function toVariable(name, value) {
name = getName(name)
if (opts.toUpperCase)
name = name.toUpperCase()
if (opts.output || opts.isDotenv)
return `${name}=${value}`
if (opts.isCmd)
return `set "${name}=${value}"`
return `export ${name}=${value}`
}
/*
* Get the "environment variable" name from path
* @example
* getName('/foo/bar/BAZ')
* // 'BAZ'
*/
function getName(path) {
return path
.split('/')
.reverse()[0]
}
function logInfo(message) {
console.log(message)
process.exit(0)
}
function logError(message) {
console.error('[error] %s', message)
process.exit(1)
}