-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
268 lines (245 loc) · 10 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env node
var nginxBeautifier = require("./nginxbeautifier.js");
//define nodejs modules
var fs = require('fs');
var path = require('path');
var options = {
name: "nginxbeautifier",
spaces: 0,
tabs: 0,
dontJoinCurlyBracet: false,
align: false,
trailingBlankLines: false,
recursive: false,
inputPath: [],
outputPath: [],
extension: "conf"
};
var knownArguments = {
"--help": function (input) {
if (input == "desc")
return "Show this help text.";
console.log("Usage: " + options.name + " [OPTION]... [FILE]...");
console.log("Description: Formats nginx conf files into a more readable format by re-indenting the lines.");
console.log("");
console.log("Mandatory arguments to long options are mandatory for short options too, Arguments are case insensitive.");
var argPrint = {};
for (var key in knownArguments) {
var desc = knownArguments[key]("desc");
if (argPrint[desc] == null)
argPrint[desc] = '';
argPrint[desc] = key + ", " + argPrint[desc];
}
for (var desc in argPrint) {
console.log(argPrint[desc], desc);
}
console.log("");
console.log("Usage examples:");
console.log("(1)>" + options.name + " -s 4 -r sites-enabled/");
console.log("(2)>" + options.name + " -s 4 -r /etc/nginx/sites-enabled/");
console.log("(3)>" + options.name + " -s 4 -i /etc/nginx/sites-enabled/site.conf -o /etc/nginx/sites-enabled/newSite.conf ");
console.log("(4)>" + options.name + " -s 4 -i /etc/nginx/sites-enabled/site.conf");
console.log("(5)>" + options.name + " -s 4 -i /etc/nginx/sites-enabled/*");
console.log("(6)>" + options.name + " -t 4 -i /etc/nginx/sites-enabled/*");
console.log("(7)>" + options.name + " -t 4 /etc/nginx/sites-enabled/*");
console.log("(8)>" + options.name + " -t 4 -i /etc/nginx/sites-enabled/* -o /etc/nginx/new-sites-enabled/*");
return false;
},
"--space": function (number) {
if (number == "desc")
return "Amount of spaces to indent with, Can not be used if tabs are specified."
else if (isNaN(number)) {
knownArguments["--space"](1);
return true;
}
if (options.tabs > 0) {
console.log("Error! tabs were already defined, please choose one or the other.")
knownArguments["--help"]();
process.exit();
}
options.spaces = parseInt(number);
nginxBeautifier.modifyOptions({INDENTATION: " ".repeat(options.spaces)});
},
"--tabs": function (number) {
if (number == "desc")
return "Amount of tabs to indent with, Can not be used if spaces are specified."
else if (isNaN(number)) {
knownArguments["--tabs"](1);
return true;
}
if (options.spaces > 0) {
console.log("Error! spaces were already defined, please choose one or the other.")
knownArguments["--help"]();
process.exit();
}
options.tabs = parseInt(number);
nginxBeautifier.modifyOptions({INDENTATION: "\t".repeat(options.tabs)});
},
"--blank-lines": function (input) {
if (input == "desc")
return "if set to true, an empty line will be inserted after opening brackets";
options.trailingBlankLines = true;
},
"--dont-join": function (input) {
if (input == "desc")
return "if set to true, commands such as 'server' and '{' will be on a seperate line, false by default ('server {' )";
options.dontJoinCurlyBracet = true;
}
,
"--align": function (input) {
if (input == "desc")
return "if set to true, all applicable attribute values will be vertically aligned with each other";
options.align = true;
}
,
"--recursive": function (input) {
if (input == "desc")
return "scan the whole current folder, and all sub folders recursively.";
options.recursive = true;
return false;
}
,
"--input": function (file) {
var recursive = false;
if (file == "desc")
return "The file to input, is optional if you provide a path after all the arguments.";
if (file.startsWith("-"))
return true;
if (file.endsWith("*")) {
//knownArguments["--recursive"]();
recursive = true;
file = file.slice(0, file.length - 1);
file = file.length > 0 ? file : ".";
}
options.inputPath.push({name: file, recursive: recursive});
}
,
"--output": function (file) {
var recursive = false;
if (file == "desc")
return "The file to output to, is optional if you provide a path after all the arguments.";
if (file.startsWith("-"))
return true;
if (file.endsWith("*")) {
// knownArguments["--recursive"]();
recursive = true;
file = file.slice(0, file.length - 1);
file = file.length > 0 ? file : ".";
}
options.outputPath.push({name: file, recursive: recursive});
},
"--extension": function (ext) {
if (ext == "desc")
return "The extension of the config file to look for(.conf by default).";
if (ext.startsWith("-"))
return true;
options.extension = ext;
}
}
;
//shortcuts
knownArguments["-h"] = knownArguments["--help"];
knownArguments["-s"] = knownArguments["--space"];
knownArguments["-t"] = knownArguments["--tabs"];
knownArguments["-r"] = knownArguments["--recursive"];
knownArguments["-i"] = knownArguments["--input"];
knownArguments["-o"] = knownArguments["--output"];
knownArguments["-bl"] = knownArguments["--blank-lines"];
knownArguments["--dontjoin"] = knownArguments["--dont-join"];
knownArguments["-dj"] = knownArguments["--dont-join"];
knownArguments["-a"] = knownArguments["--align"];
knownArguments["-ext"] = knownArguments["--extension"];
knownArguments["-e"] = knownArguments["--extension"];
var wasFunc = null;
if (process.argv.length > 2) {
for (var key in process.argv) {
if (key >= 2) {
var arg = process.argv[key].toLowerCase();
if (arg.startsWith("-")) {
var argFunc = knownArguments[arg];
if (argFunc != null) {
//if its true, excpecting another arg next iteration
if (argFunc(arg))
wasFunc = argFunc;
}
}
else if (!isNaN(arg) && wasFunc != null) {
wasFunc(arg);
wasFunc = null;
}
else {
//its probably a file path
knownArguments["-i"](process.argv[key]);
}
}
}
}
else {
console.log("Error! no arguments were provided. I don't know what to do!")
knownArguments["--help"]();
process.exit()
}
/*
if (!options.recursive) {
if (!options.inputPath.endsWith(options.extension)) {
console.log("Error! folder was selected, but no recursive option has been activated.")
knownArguments["--help"]();
process.exit();
}
}
//options.outputPath.endsWith(options.extension)&&
else if (!fs.statSync(options.outputPath).isDirectory()) {
console.log("Error! input is recursive(multiple files), output is a single file? this makes no sense!")
knownArguments["--help"]();
process.exit();
}
*/
//if we got up here, all seems fine
//lets fetch the list of all the relevant files
var filesArr = [];
for (var index = 0, length = options.inputPath.length; index < length; index++) {
if (options.inputPath[index] != "") {
if ((options.inputPath[index].recursive || options.recursive) && fs.statSync(options.inputPath[index].name).isDirectory()) {
filesArr = filesArr.concat(nginxBeautifier.walkSync(options.inputPath[index].name, options.extension));
}
else if (fs.statSync(options.inputPath[index].name).isFile()) {
filesArr = filesArr.concat(options.inputPath[index].name);
}
else {
console.log("Error! folder was selected, but no recursive option(path/* or -r) has been activated.")
knownArguments["--help"]();
process.exit();
}
}
}
for (var index = 0, length = filesArr.length; index < length; index++) {
var file = filesArr[index];
console.log("Working on file: " + file);
var fileContents = fs.readFileSync(file, "utf8");
//splti the file into lines, clean spaces
var cleanLines = nginxBeautifier.clean_lines(fileContents);
//join opening bracket(if user wishes so) true by default
if (!options.dontJoinCurlyBracet)
cleanLines = nginxBeautifier.join_opening_bracket(cleanLines);
//perform the indentation
cleanLines = nginxBeautifier.perform_indentation(cleanLines);
// vertically align all eligible declarations
if (options.align) {
cleanLines = nginxBeautifier.perform_alignment(cleanLines);
}
//combine all the lines back together
var outputContents = cleanLines.join("\n");
//save all the contents to the file.
//if the user didnt choose output path, then the input file is used.
// if (options.inputPath == options.outputPath) {
if (options.outputPath.length > 0)
fs.writeFileSync(options.outputPath[0] + "/" + file, outputContents, 'utf8');
else
fs.writeFileSync(file, outputContents, 'utf8');
// }
//else we have to write to the chosen output file or folder
// else {
// }
}
console.log("Success.");
//console.log(finalOutput);