UNPKG

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