This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
/
jarbuncli.js
executable file
·95 lines (80 loc) · 3.14 KB
/
jarbuncli.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
#!/usr/bin/env node
// Jarbunq CLI
const fs = require("fs")
const logger = require("anyhow")
logger.setup("console")
// Action help: outputs the help to the console.
const help = () => {
console.log("")
console.log("The Jarbunq CLI")
console.log("------------------------------------------------------------------")
console.log("Usage: jarbuncli.js [action]")
console.log("------------------------------------------------------------------")
console.log("Actions:")
console.log("")
console.log(" encrypt : encrypt database and settings.private.json files")
console.log(" decrypt : decrypt database and settings.private.json files")
console.log("")
console.log("------------------------------------------------------------------")
}
// Action encrypt: encrypts the settings.private.json file.
const encrypt = () => {
const setmeup = require("setmeup")
try {
setmeup.encrypt("settings.private.json")
logger.info("Jarbunq CLI", "Encrypted values on settings.private.json")
} catch (ex) {
logger.error("Jarbunq CLI", "encrypt", ex)
}
}
// Action decrypt: decrypts the settings.private.json file.
const decrypt = () => {
const setmeup = require("setmeup")
logger.setup()
try {
setmeup.decrypt("settings.private.json")
logger.info("Jarbunq CLI", "Decrypted values on settings.private.json")
} catch (ex) {
logger.error("Jarbunq CLI", "decrypt", ex)
}
}
// Action build-settings-sample: generates a sample of the current settings.private.json file.
const buildSettingsSample = () => {
const privateSettings = JSON.parse(fs.readFileSync(__dirname + "/settings.private.json", {encoding: "utf8"}))
const privateSettingsSample = JSON.parse(fs.readFileSync(__dirname + "/settings.private.json.sample", {encoding: "utf8"}))
// Recursive function to replace private values with type strings.
const settingsReplacer = function (source, target) {
if (source != null) {
const keys = Object.keys(source)
for (let key of keys) {
const objType = typeof source[key]
if (target == null) {
target = {}
}
if (objType == "object") {
settingsReplacer(source[key], target[key])
} else if (target[key] != null && target[key].toString().includes(":")) {
source[key] = target[key]
} else {
source[key] = objType
}
}
}
}
settingsReplacer(privateSettings, privateSettingsSample)
// Write sample settings.
fs.writeFileSync(__dirname + "/settings.private.json.sample", JSON.stringify(privateSettings, null, 4))
logger.info("Jarbunq CLI", "Generated / updated the settings.private.json.sample file")
}
// Parse CLI arguments...
for (let arg of process.argv) {
if (arg == "help") {
return help()
} else if (arg == "encrypt") {
return encrypt()
} else if (arg == "decrypt") {
return decrypt()
} else if (arg == "build-settings-sample") {
return buildSettingsSample()
}
}