UNPKG

5.03 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3let fs = require('fs');
4let npm = require('./package.json');
5let crypto = require('crypto');
6let program = require('commander');
7
8// _____ ______ _______ _______ _____ _ _ _____ _____
9// / ____| | ____| |__ __| |__ __| |_ _| | \ | | / ____| / ____|
10// | (___ | |__ | | | | | | | \| | | | __ | (___
11// \___ \ | __| | | | | | | | . ` | | | |_ | \___ \
12// ____) | | |____ | | | | _| |_ | |\ | | |__| | ____) |
13// |_____/ |______| |_| |_| |_____| |_| \_| \_____| |_____/
14//
15
16//
17// The CLI options for this app.
18//
19program
20 .version(npm.version)
21 .parse(process.argv);
22
23//
24// React when the user needs help
25//
26program.on('--help', function() {
27
28 //
29 // Just add an empty line at the end of the help to make the text more
30 // clear to the user
31 //
32 console.log("");
33
34});
35
36//
37// Pass the user input to the commander module
38//
39program.parse(process.argv);
40
41// __ __ _____ _ _
42// | \/ | /\ |_ _| | \ | |
43// | \ / | / \ | | | \| |
44// | |\/| | / /\ \ | | | . ` |
45// | | | | / ____ \ _| |_ | |\ |
46// |_| |_| /_/ \_\ |_____| |_| \_|
47//
48
49//
50// Open the app.json file.
51//
52fs.readFile('app.json', 'utf8', function(err, data) {
53
54 //
55 // 1. Display Error if any
56 //
57 err && console.log(err.message)
58
59 //
60 // 2. Convert the content of the file in to a JS Object
61 //
62 let parsed = JSON.parse(data).env;
63
64 //
65 // 3. Create a variable to store the
66 //
67 let line = "";
68
69 //
70 // 4. Loop over the environment variables and convert them in to a file
71 //
72 for(env_var in parsed)
73 {
74 //
75 // 1. Save the default value in a clear variable
76 //
77 let value = parsed[env_var].value;
78
79 //
80 // 2. Save the description in a clear variable
81 //
82 let description = parsed[env_var].description;
83
84 //
85 // 3. Save the generator in a clear variable
86 //
87 let generator = parsed[env_var].generator;
88
89 //
90 // 4. Chop the description in to a specific line length
91 //
92 let comment = limit_80_array(description, "", 80);
93
94 //
95 // 5. Create the default empty env name
96 //
97 let env_name = env_var + "=";
98
99 //
100 // 6. If the file specifies a secret, we create one.
101 //
102 if(!value && generator === "secret")
103 {
104 env_name += crypto.randomBytes(16).toString('hex');
105 }
106
107 //
108 // 7. If there is a default value for the env var, we append it
109 //
110 if(value && !generator)
111 {
112 env_name += value;
113 }
114
115 //
116 // 8. Combine the comment section with the environment variable
117 //
118 line += comment + "\n" + env_name + "\n\n";
119 }
120
121 //
122 // 5. Remove the last two new line characters
123 //
124 let file = line.substring(0, line.length - 2);
125
126 //
127 // 6. Save the data in to the .env file.
128 //
129 fs.writeFile('.env', file, (err) => {
130
131 //
132 // 1. Display Error if any
133 //
134 err && console.log(err.message)
135
136 //
137 // 2. Let the user know what happened
138 //
139 console.log("The file .env was created.");
140
141 });
142
143});
144
145//
146// The main function that is responsible in chopping the comment to a specific
147// length.
148//
149// string <- The string to chop
150// fragment <- The variable holding the chopped string
151// length <- Line length
152//
153// Return -> Chopped and formated string
154//
155function limit_80_array(string, fragment, length)
156{
157 //
158 // 1. Split the string in to an array
159 //
160 let array = string.split(" ");
161
162 //
163 // 2. Make a copy of the array. We are going to use this array as a
164 // container that will hold the words that need to be still proceed
165 //
166 let array_copy = array.slice();
167
168 //
169 // 3. A temp Array that is going to hold one line of text at each
170 // iteration
171 //
172 let tmp = [];
173
174 //
175 // 4. Variable that helps us track how many character do we have in one
176 // line already
177 //
178 let size = 0;
179
180 //
181 // 5. Main loop that append words until they are less then the length
182 // passed in the function
183 //
184 for(let index in array)
185 {
186 //
187 // 1. Add the word to our array
188 //
189 tmp.push(array[index])
190
191 //
192 // 2. Store the size of the word, plus 1 for the extra space
193 //
194 size += array[index].length + 1;
195
196 //
197 // 3. Remove the first element from the array
198 //
199 array_copy.shift();
200
201 //
202 // 4. Check the future
203 //
204 let position = parseInt(index) + 1;
205
206 //
207 // 5. Make sure the future holds something for us
208 //
209 if(array[position])
210 {
211 //
212 // 1. Calculate the future size
213 //
214 let future = size + array[position].length;
215
216 //
217 // 2. If the future will be to big for us, lets run away
218 //
219 if(future >= length - 2)
220 {
221 break;
222 }
223 }
224 }
225
226 //
227 // 6. Check if there are some word left in the array
228 //
229 if(array_copy.length > 0)
230 {
231 //
232 // 1. Combine what we have
233 //
234 fragment += "# " + tmp.join(" ") + "\n";
235
236 //
237 // 2. Process the left overs again
238 //
239 return limit_80_array(array_copy.join(" "), fragment, length)
240 }
241
242 //
243 // 7. If nothing left, combine what we have
244 //
245 fragment += "# " + tmp.join(" ") + "\n";
246
247 //
248 // -> Return our master peace
249 //
250 return fragment.substring(0, fragment.length - 1);
251}
252
253