-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·209 lines (185 loc) · 5.55 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const yargs = require("yargs");
const version = "1.0.0";
const argv = yargs
.option("version", {
alias: "v",
description: "Print the installed version",
type: "boolean",
})
.option("no-dotenv", {
description: "Dont' use dotenv",
type: "boolean",
})
.option("env-path", {
alias: "p",
description: "Path to env file",
type: "string",
})
.option("config", {
alias: "c",
description: "Path to config file",
type: "string",
})
.help()
.alias("help", "h").argv;
if (argv.version) {
console.log(version);
}
let config;
try {
const configFilePath = path.normalize(
argv.config ? argv.config : "./elm-constants.json"
);
if (!fs.existsSync(configFilePath)) {
throw new Error(
"I couldn't find the config file at \"" + configFilePath + '"'
);
}
const configFile = fs.readFileSync(configFilePath);
config = JSON.parse(configFile);
// Validations
if (!config.path) {
throw new Error('I couldn\'t the field "path" in the config file.');
}
if (typeof config.path !== "string") {
throw new Error('The field "path" in the config file wasn\'t a string.');
}
if (!config.moduleName) {
throw new Error('I couldn\'t the field "moduleName" in the config file.');
}
if (typeof config.moduleName !== "string") {
throw new Error(
'The field "moduleName" in the config file wasn\'t a string.'
);
}
if (!config.values) {
throw new Error('I couldn\'t the field "values" in the config file.');
}
if (!Array.isArray(config.values)) {
throw new Error('The field "values" in the config file wasn\'t an array.');
}
if (config.values.length === 0) {
throw new Error(
'The field "values" in the config file was empty. ' +
"There's nothing for me to do!"
);
}
if (typeof argv.dotenv !== "boolean" || argv.dotenv !== false) {
let envPath = path.resolve(process.cwd(), ".env");
if (typeof argv.envPath === "string") {
const PATH_REGEXP = /^.*\.(env)($|\..+$)/;
if (!fs.existsSync(argv.envPath) || !PATH_REGEXP.test(argv.envPath)) {
throw new Error(`I couldn't find an env file at "${argv.envPath}"`);
}
envPath = argv.envPath;
}
if (process.env.NODE_ENV !== "production") {
require("dotenv").config({
path: envPath,
});
}
}
const isValidElmVar = (str) => new RegExp(/^[a-z]\w+$/).test(str);
const capitalizeFirstLetter = (string) =>
string.charAt(0).toUpperCase() + string.slice(1);
const lowercaseFirstLetter = (string) =>
string.charAt(0).toLowerCase() + string.slice(1);
const values = config.values.map((value) => {
if (Array.isArray(value)) {
if (value.length !== 2) {
throw new Error(
"The array to alias a value can only have two elements."
);
}
if (typeof value[0] !== "string") {
throw new Error('"' + value[0] + '" is not a string');
}
if (typeof value[1] !== "string") {
throw new Error('"' + value[1] + '" is not a string');
}
if (!isValidElmVar(value[1])) {
throw new Error('"' + value[1] + '" is not a valid Elm variable.');
}
return value;
}
if (typeof value === "string") {
let transformed = value
.toLowerCase()
.split("_")
.map(capitalizeFirstLetter)
.join("");
transformed = lowercaseFirstLetter(transformed);
if (!isValidElmVar(transformed)) {
throw new Error(
"The env var you passed could not be automatically converted " +
"into a valid Elm variable name. Try aliasing it instead."
);
}
return [value, transformed];
} else {
throw new Error(
"If you're specifying a value, it must a string " +
"that's the name of ENV variable."
);
}
});
// Generate declarations
const toElmValue = (name, value) =>
`${name} : String\n` + `${name} =\n` + ` "${value}"\n`;
const [exposingValues, elmValues] = values.reduce(
(acc, cur) => {
const [envValueName, elmName] = Array.isArray(cur) ? cur : [cur, cur];
const envValue = process.env[envValueName];
if (envValue === null || envValue === undefined) {
throw new Error(
`The env variable "${envValueName}" wasn't found. ` +
`Maybe you forgot to set it?`
);
}
const [accExposing, accElmValue] = acc;
return [
[elmName].concat(accExposing),
[toElmValue(elmName, envValue)].concat(accElmValue),
];
},
[[], []]
);
if (exposingValues.length === 0) {
throw new Error(
"I couldn't find any of the environment variable you specified, " +
"so I have nothing to generate!"
);
}
const generated =
`module ${config.moduleName} exposing (${exposingValues.join(", ")})\n` +
"\n" +
"\n" +
elmValues.join("\n\n");
// Write to file
const fileToGenerate = path.normalize(
`${config.path}/${config.moduleName}.elm`
);
fs.writeFileSync(fileToGenerate, generated);
console.log(
`${exposingValues.length} constants written to ${fileToGenerate}`
);
process.exit(0);
} catch (e) {
if (e && typeof e.errno === "number") {
if (e.errno === -2) {
console.log(
"I couldn't find the path " +
(config && config.path ? '"' + config.path + '" ' : "") +
"you specified in the config. " +
" Maybe you forgot to create it?"
);
process.exit(1);
return;
}
}
console.log(e.message);
process.exit(1);
}