-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (130 loc) · 4.11 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
const inquirer = require('inquirer');
const glob = require("glob")
const inquirerTablePrompt = require("./inquirerTablePrompt");
const EnvFile = require('./envFile');
inquirer.registerPrompt("table", inquirerTablePrompt);
// TODO: cli - on first use, user can pass flag `--init` to be asked which envs they want
// (show list of common ones and option for custom). Check local files and compare to chosen envs - if there are any conflicts then ask user if they want to
// maintain local vars or wipe them and start fresh
const pattern = ".env.*";
const envPaths = glob.sync(pattern);
if (!envPaths.length) {
console.warn(`No ${pattern} files found. To get started, create multiple files of the following format: ".env.{environment_name}" (ex: ".env.development")`)
process.exit(0);
}
// get env names, filter out any encrypted envs
const envs = envPaths.map(filename => filename.split(".env.")[1]).filter(envName => !envName.includes(".enc"))
let envFiles = envs.map(env => new EnvFile(env));
const { rows, columns } = prepareInquirerData(envs);
runManager({ rows, columns }).then(() => {
// console.log("done!")
});
async function runManager({ rows, columns }) {
try {
const {environment: answers} = await inquirer.prompt([
{
type: "table",
name: "environment",
message: "View or edit environment variables",
rows,
columns
}
]);
// TODO: give user option to print changes directly
const allJsonContent = convertAnswerToJson(answers)
const envs = Object.keys(allJsonContent)
const confirmAnswers = await inquirer.prompt([
{
type: "confirm",
name: "answer",
message: `Write changes to files ".env.${envs.join(`", ".env.`)}"?`
}
])
if (confirmAnswers.answer === true) {
console.log("💾 Saving changes")
envFiles = envFiles.map(envFile => {
envFile.injectChanges(allJsonContent[envFile.getEnv()])
envFile.save()
return envFile;
})
}
else {
// TODO: instead of throwing away changes, re-render the UI (user can always quit easily using ctrl-c)
console.log("🗑 Throwing away changes")
}
}
catch(err) {
console.error("Error: ", err)
}
}
function convertAnswerToJson(answers) {
const allJsonContent = {}
answers.columns.forEach((col, i) => {
const jsonContent = {}
answers.rows.forEach(row => {
jsonContent[row.name] = row.values[i]
})
allJsonContent[col.value] = jsonContent
})
return allJsonContent;
}
function prepareInquirerData(envs) {
const allJsonContent = {};
const columns = [];
let varNames = [];
envFiles.forEach(envFile => {
const jsonContent = envFile.getJsonContent();
const env = envFile.getEnv();
varNames = [...varNames, ...Object.keys(jsonContent)]
allJsonContent[env] = jsonContent;
columns.push({ name: `${env.charAt(0).toUpperCase()}${env.slice(1)}`, value: env })
});
// remove duplicates
varNames = [...new Set(varNames)]
const rows = varNames.map(varName => {
const values = [];
envs.forEach((env) => {
values.push(allJsonContent[env][varName])
});
return {
name: varName,
values
}
})
return { rows, columns }
// return {
// columns: [
// {
// name: "Local",
// value: "local"
// },
// {
// name: "Development",
// value: "development"
// },
// {
// name: "Production",
// value: "production"
// },
// {
// name: "Test",
// value: "test"
// },
// ],
// rows: [
// {
// name: "NODE_ENV",
// values: ["development", "development", "production", "test"]
// },
// {
// name: "API_URL",
// values: ["http://locahost:8080", "https://example.com", "https://production.example.com", "http://locahost:8080"]
// },
// {
// name: "FB_ID",
// values: ["1234567890123456", "1234567890123456", "1234567890123456", "1234567890123456"]
// }
// ]
// }
}