Skip to content

Commit 80f3c48

Browse files
committed
better code style
1 parent 560762a commit 80f3c48

3 files changed

Lines changed: 251 additions & 105 deletions

File tree

bin/convert-argv.js

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ module.exports = function(optimist, argv, convertOptions) {
1717
if(argv.d) {
1818
argv.debug = true;
1919
argv["output-pathinfo"] = true;
20-
if(!argv.devtool) argv.devtool = "sourcemap";
20+
if(!argv.devtool) {
21+
argv.devtool = "sourcemap";
22+
}
2123
}
2224
if(argv.p) {
2325
argv["optimize-minimize"] = true;
@@ -26,27 +28,40 @@ module.exports = function(optimist, argv, convertOptions) {
2628

2729
function ifArg(name, fn, init, finalize) {
2830
if(Array.isArray(argv[name])) {
29-
if(init) init();
31+
if(init) {
32+
init();
33+
}
3034
argv[name].forEach(fn);
31-
if(finalize) finalize();
35+
if(finalize) {
36+
finalize();
37+
}
3238
} else if(typeof argv[name] != "undefined") {
33-
if(init) init();
39+
if(init) {
40+
init();
41+
}
3442
fn(argv[name], -1);
35-
if(finalize) finalize();
43+
if(finalize) {
44+
finalize();
45+
}
3646
}
3747
}
3848

3949
function ifArgPair(name, fn, init, finalize) {
4050
ifArg(name, function(content, idx) {
4151
var i = content.indexOf("=");
42-
if(i < 0) return fn(null, content, idx);
43-
else return fn(content.substr(0, i), content.substr(i+1), idx);
52+
if(i < 0) {
53+
return fn(null, content, idx);
54+
} else {
55+
return fn(content.substr(0, i), content.substr(i+1), idx);
56+
}
4457
}, init, finalize);
4558
}
4659

4760
function ifBooleanArg(name, fn) {
4861
ifArg(name, function(bool) {
49-
if(bool) fn();
62+
if(bool) {
63+
fn();
64+
}
5065
});
5166
}
5267

@@ -58,7 +73,9 @@ module.exports = function(optimist, argv, convertOptions) {
5873

5974
function mapArgToBooleanInverse(name, optionName) {
6075
ifArg(name, function(bool) {
61-
if(!bool) options[optionName || name] = false;
76+
if(!bool) {
77+
options[optionName || name] = false;
78+
}
6279
});
6380
}
6481

@@ -69,14 +86,16 @@ module.exports = function(optimist, argv, convertOptions) {
6986
}
7087

7188
function loadPlugin(name) {
89+
var path;
7290
try {
73-
var path = resolve.sync(process.cwd(), name);
91+
path = resolve.sync(process.cwd(), name);
7492
} catch(e) {
7593
console.log("Cannot resolve plugin " + name + ".");
7694
process.exit(-1);
7795
}
96+
var Plugin;
7897
try {
79-
var Plugin = require(path);
98+
Plugin = require(path);
8099
} catch(e) {
81100
console.log("Cannot load plugin " + name + ". (" + path + ")");
82101
throw e;
@@ -90,29 +109,34 @@ module.exports = function(optimist, argv, convertOptions) {
90109
}
91110

92111
function ensureObject(parent, name) {
93-
if(typeof parent[name] != "object" || parent[name] === null)
112+
if(typeof parent[name] !== "object" || parent[name] === null) {
94113
parent[name] = {};
114+
}
95115
}
96116

97117
function ensureArray(parent, name) {
98-
if(!Array.isArray(parent[name]))
118+
if(!Array.isArray(parent[name])) {
99119
parent[name] = [];
120+
}
100121
}
101122

102123
if(argv.config) {
103124
options = require(path.resolve(argv.config));
104125
} else {
105126
var configPath = path.resolve("webpack.config.js");
106-
if(fs.existsSync(configPath))
127+
if(fs.existsSync(configPath)) {
107128
options = require(configPath);
129+
}
108130
}
109-
if(typeof options != "object" || options === null) {
131+
if(typeof options !== "object" || options === null) {
110132
console.log("Config did not export a object.");
111133
process.exit(-1);
112134
}
113135

114136
mapArgToPath("context", "context");
115-
if(!options.context) options.context = process.cwd();
137+
if(!options.context) {
138+
options.context = process.cwd();
139+
}
116140

117141
ifArgPair("entry", function(name, entry) {
118142
options.entry[name] = entry;
@@ -121,7 +145,9 @@ module.exports = function(optimist, argv, convertOptions) {
121145
});
122146

123147
ifArgPair("module-bind", function(name, binding) {
124-
if(name === null) name = binding;
148+
if(name === null) {
149+
name = binding;
150+
}
125151
options.module.loaders.push({
126152
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"),
127153
loader: binding
@@ -132,7 +158,9 @@ module.exports = function(optimist, argv, convertOptions) {
132158
});
133159

134160
ifArgPair("module-bind-pre", function(name, binding) {
135-
if(name === null) name = binding;
161+
if(name === null) {
162+
name = binding;
163+
}
136164
options.module.preLoaders.push({
137165
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"),
138166
loader: binding
@@ -143,7 +171,9 @@ module.exports = function(optimist, argv, convertOptions) {
143171
});
144172

145173
ifArgPair("module-bind-post", function(name, binding) {
146-
if(name === null) name = binding;
174+
if(name === null) {
175+
name = binding;
176+
}
147177
options.module.postLoaders.push({
148178
test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"),
149179
loader: binding
@@ -161,7 +191,7 @@ module.exports = function(optimist, argv, convertOptions) {
161191
}
162192
defineObject[name] = value;
163193
}, function() {
164-
defineObject = {}
194+
defineObject = {};
165195
}, function() {
166196
ensureArray(options, "plugins");
167197
var DefinePlugin = require("../lib/DefinePlugin");
@@ -252,20 +282,26 @@ module.exports = function(optimist, argv, convertOptions) {
252282
ifBooleanArg("progress", function() {
253283
var ProgressPlugin = require("../lib/ProgressPlugin");
254284
ensureArray(options, "plugins");
255-
var chars = 0, lastState;
285+
var chars = 0, lastState, lastStateTime;
256286
options.plugins.push(new ProgressPlugin(function(percentage, msg) {
257287
var state = msg;
258288
if(percentage < 1) {
259289
percentage = Math.floor(percentage * 100);
260290
msg = percentage + "% " + msg;
261-
if(percentage < 100) msg = " " + msg;
262-
if(percentage < 10) msg = " " + msg;
291+
if(percentage < 100) {
292+
msg = " " + msg;
293+
}
294+
if(percentage < 10) {
295+
msg = " " + msg;
296+
}
263297
}
264-
for(; chars > msg.length; chars--)
298+
for(; chars > msg.length; chars--) {
265299
process.stderr.write("\b \b");
300+
}
266301
chars = msg.length;
267-
for(var i = 0; i < chars; i++)
302+
for(var i = 0; i < chars; i++) {
268303
process.stderr.write("\b");
304+
}
269305
if(options.profile) {
270306
state = state.replace(/^\d+\/\d+\s+/, "");
271307
if(percentage === 0) {
@@ -289,14 +325,18 @@ module.exports = function(optimist, argv, convertOptions) {
289325
});
290326

291327
ifArgPair("resolve-alias", function(name, value) {
292-
if(!name) throw new Error("--resolve-alias <string>=<string>");
328+
if(!name) {
329+
throw new Error("--resolve-alias <string>=<string>");
330+
}
293331
ensureObject(options, "resolve");
294332
ensureObject(options.resolve, "alias");
295333
options.resolve.alias[name] = value;
296334
});
297335

298336
ifArgPair("resolve-loader-alias", function(name, value) {
299-
if(!name) throw new Error("--resolve-loader-alias <string>=<string>");
337+
if(!name) {
338+
throw new Error("--resolve-loader-alias <string>=<string>");
339+
}
300340
ensureObject(options, "resolveLoader");
301341
ensureObject(options.resolveLoader, "alias");
302342
options.resolveLoader.alias[name] = value;
@@ -343,11 +383,12 @@ module.exports = function(optimist, argv, convertOptions) {
343383
ifArg("provide", function(value) {
344384
ensureArray(options, "plugins");
345385
var idx = value.indexOf("=");
386+
var name;
346387
if(idx >= 0) {
347-
var name = value.substr(0, idx);
388+
name = value.substr(0, idx);
348389
value = value.substr(idx + 1);
349390
} else {
350-
var name = value;
391+
name = value;
351392
}
352393
var ProvidePlugin = require("../lib/ProvidePlugin");
353394
options.plugins.push(new ProvidePlugin(name, value));
@@ -387,8 +428,9 @@ module.exports = function(optimist, argv, convertOptions) {
387428
ensureObject(options, "entry");
388429
function addTo(name, entry) {
389430
if(options.entry[name]) {
390-
if(!Array.isArray(options.entry[name]))
431+
if(!Array.isArray(options.entry[name])) {
391432
options.entry[name] = [options.entry[name]];
433+
}
392434
options.entry[name].push(entry);
393435
} else {
394436
options.entry[name] = entry;
@@ -399,9 +441,14 @@ module.exports = function(optimist, argv, convertOptions) {
399441
var j = content.indexOf("?");
400442
if(i < 0 || (j >= 0 && j < i)) {
401443
var resolved = path.resolve(content);
402-
if(fs.existsSync(resolved)) addTo("main", resolved);
403-
else addTo("main", content);
404-
} else addTo(content.substr(0, i), content.substr(i+1))
444+
if(fs.existsSync(resolved)) {
445+
addTo("main", resolved);
446+
} else {
447+
addTo("main", content);
448+
}
449+
} else {
450+
addTo(content.substr(0, i), content.substr(i+1));
451+
}
405452
});
406453
}
407454

0 commit comments

Comments
 (0)