-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·107 lines (83 loc) · 2.41 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const fs = require('fs')
const crypto = require('crypto')
const ALGORITHM = process.env.MSCOENV_ALGORITHM || 'aes-256-cbc'
const HASH = process.env.MSCOENV_HASH || 'sha256'
const SECRET_KEY = process.env.MSCOENV_SECRET_KEY
const ENV_PATH = '.env'
const DEFAULT_ENVIRONMENT = 'development'
const key = crypto
.createHash(HASH)
.update(String(SECRET_KEY))
.digest('hex')
.substr(0, 32)
const createEnvDir = () => {
if (!fs.existsSync(ENV_PATH)) {
fs.mkdirSync(ENV_PATH)
console.info(`${ENV_PATH} folder created`)
}
}
const encrypt = (environment = DEFAULT_ENVIRONMENT) => {
createEnvDir()
const input = fs.createReadStream(`.env.${environment}`)
const output = fs.createWriteStream(`${ENV_PATH}/${environment}`)
const iv = Buffer.from(key, 'hex')
const cipher = crypto.createCipheriv(ALGORITHM, key, iv)
input.pipe(cipher).pipe(output)
output.on('finish', function() {
console.log('Encryption completed successfully!')
})
}
const decrypt = (environment = DEFAULT_ENVIRONMENT) => {
createEnvDir()
const input = fs.createReadStream(`${ENV_PATH}/${environment}`)
const output = fs.createWriteStream(`.env.${environment}`)
const iv = Buffer.from(key, 'hex')
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv)
input.pipe(decipher).pipe(output)
output.on('finish', function() {
console.log('Decryption completed successfully!')
})
}
const setup = (environment = DEFAULT_ENVIRONMENT) => {
createEnvDir()
const existsEncryptedEnv = fs.existsSync(`${ENV_PATH}/${environment}`)
const existsDecryptedEnv = fs.existsSync(`${ENV_PATH}.${environment}`)
if (existsEncryptedEnv && !existsDecryptedEnv) {
decrypt(environment)
} else if (!existsEncryptedEnv) {
console.warn(`Environment ${environment} not found`)
}
}
const main = command => {
const help = `
Help:\n
- To process the images, type
$ node env.js encrypt [environment]
- To get the current rules type
$ node env.js decrypt [environment]
`
switch (command) {
case 'encrypt': {
encrypt(process.argv[3])
break
}
case 'decrypt': {
decrypt(process.argv[3])
break
}
case 'setup': {
setup(process.argv[3])
break
}
case 'help': {
console.info(help)
break
}
default:
console.error(`\nUnknown command: ${command}.\n\n`)
console.info(help)
}
}
const command = process.argv[2]
main(command)