-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (151 loc) · 4.82 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const fs = require("fs");
const getArgs = (argName = null) => {
const args = {};
const argvs = process.argv;
for (let arg of argvs) {
const pos = arg.indexOf("=");
if (pos > -1) {
const key = arg.substring(0, pos);
const value = arg.substring(pos + 1);
args[key] = value;
}
}
if (argName) return args[argName];
return args;
};
const getDir = (path) => {
const currentDirectory = process.cwd();
return path[0] !== "/"
? currentDirectory + "/" + path
: currentDirectory + path;
};
const castObjToEnv = (parentKey, data) => {
let envData = "";
const recursiveEnv = (key, obj) => {
if (typeof obj !== "object") return (envData += `${key}=${obj}\n`);
if (Array.isArray(obj)) {
if (typeof obj[0] !== "object") {
return (envData += `${key}=${obj.toString()}\n`);
}
obj.forEach((r) => recursiveEnv(key, r));
} else {
Object.keys(obj).forEach((k) => recursiveEnv(`${key}.${k}`, obj[k]));
}
};
try {
recursiveEnv(parentKey, data);
return envData;
} catch (e) {
return `${parentKey}=${data}\n`;
}
};
const convertPathToJSON = (result, path, value) => {
const keys = path.split(".");
keys.reduce((acc, key, index) => {
if (index === keys.length - 1) {
value = parseFloat(value) ? parseFloat(value) : value;
value = value === "false" ? false : value === "true" ? true : value;
acc[key] = value;
} else {
acc[key] = acc[key] || {};
}
return acc[key];
}, result);
return result;
};
const convertJsonStringToEnv = (jsonString) => {
const records =
typeof jsonString === "string" ? JSON.parse(jsonString) : jsonString;
let jsonEnv = "";
for (let key in records) {
let value = records[key];
if (key === "" || value === "") continue;
if (typeof value === "object") {
jsonEnv += castObjToEnv(key, value);
} else {
jsonEnv += `${key}=${value}\n`;
}
}
return { jsonEnv, location: process.cwd() };
};
const convertJsonToEnv = (path) => {
const location = getDir(path);
if (!location.includes(".json")) throw new Error("File type must be a .json");
const json = fs.readFileSync(location, "utf8");
const { jsonEnv } = convertJsonStringToEnv(json);
return { jsonEnv, location };
};
const convertEnvStringToJson = (env) => {
const records = env.split(/\\n|;|\\r|\n/g);
const jsonEnv = {};
for (let i = 0; i < records.length; i++) {
const pos = records[i].indexOf("=");
const key = records[i].substring(0, pos)?.trim();
const value = records[i].substring(pos + 1)?.trim();
if (key === "" || value === "") continue;
if (key.includes(".")) {
convertPathToJSON(jsonEnv, key, value);
} else {
jsonEnv[key] = value;
}
}
return { jsonEnv, location: process.cwd() };
};
const convertEnvToJson = (path) => {
const location = getDir(path);
if (!location.includes(".env")) throw new Error("File type must be a .env");
const env = fs.readFileSync(location, "utf8");
const { jsonEnv } = convertEnvStringToJson(env);
return { jsonEnv, location };
};
const convertEnvJsonViaCMD = () => {
const argEnv = getArgs();
const filePath = argEnv["--file"]?.trim();
const envString = argEnv["--env"]?.trim();
const isConsole = argEnv["--csl"]?.trim();
const isWriteToRoot = argEnv["--wtr"]?.trim();
const outputPath = argEnv["--out"]?.trim();
if (!filePath && !envString) return;
const toEnv = filePath?.endsWith(".json");
let result = { location: null, jsonEnv: null };
if (toEnv) {
result = convertJsonToEnv(filePath);
} else {
result = envString
? convertEnvStringToJson(envString)
: convertEnvToJson(filePath);
}
const { jsonEnv, location } = result;
const data = toEnv ? jsonEnv.trim() : JSON.stringify(jsonEnv, null, 2);
const fileType = toEnv ? ".env" : ".json";
if (outputPath) {
if (!outputPath.includes(fileType))
throw new Error(`File type must be a ${fileType}`);
const ouputFullPath = getDir(outputPath);
fs.writeFileSync(ouputFullPath, data, { flag: "w", encoding: "utf8" });
process.exit(0);
}
if (isWriteToRoot === "true" || isWriteToRoot === "1") {
const newLocation = toEnv
? "." + location.replace(".json", "")
: location.replace(".env", "env") + ".json";
fs.writeFileSync(newLocation, data, { flag: "w", encoding: "utf8" });
process.exit(0);
}
if (isConsole !== "false" || isConsole !== "0") {
console.log(
"\n\n*******************************START*******************************\n\n" +
data +
"\n\n********************************END********************************\n\n"
);
process.exit(0);
}
};
module.exports = {
envFromPathToJson: convertEnvToJson,
envFromStringToJson: convertEnvStringToJson,
getArg: (name) => getArgs(name),
args: getArgs(),
jsonFromPathToEnv: convertJsonToEnv,
};
convertEnvJsonViaCMD();