Skip to content

Commit fd928dd

Browse files
See #7 - refactors CLI code from one big function.
Why: * Slightly easier to get as we get more local variables.
1 parent aff94d0 commit fd928dd

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

index.js

+69-69
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function cli(process) {
153153

154154
// ... and do the magic!
155155
if (commands.args.length > 0) {
156-
minify(commands.args);
156+
minify(process, options, debugMode, commands.args);
157157
} else {
158158
stdin = process.openStdin();
159159
stdin.setEncoding('utf-8');
@@ -162,93 +162,93 @@ function cli(process) {
162162
data += chunk;
163163
});
164164
stdin.on('end', function () {
165-
minify(data);
165+
minify(process, options, debugMode, data);
166166
});
167167
}
168+
}
168169

169-
function findArgumentTo(option, rawArgs, args) {
170-
var value = true;
171-
var optionAt = rawArgs.indexOf(option);
172-
var nextOption = rawArgs[optionAt + 1];
173-
var looksLikePath;
174-
var asArgumentAt;
175-
176-
if (!nextOption) {
177-
return value;
178-
}
179-
180-
looksLikePath = nextOption.indexOf('.css') > -1 ||
181-
/\//.test(nextOption) ||
182-
/\\[^\-]/.test(nextOption) ||
183-
/^https?:\/\//.test(nextOption);
184-
asArgumentAt = args.indexOf(nextOption);
170+
function findArgumentTo(option, rawArgs, args) {
171+
var value = true;
172+
var optionAt = rawArgs.indexOf(option);
173+
var nextOption = rawArgs[optionAt + 1];
174+
var looksLikePath;
175+
var asArgumentAt;
185176

186-
if (!looksLikePath) {
187-
value = nextOption;
188-
}
177+
if (!nextOption) {
178+
return value;
179+
}
189180

190-
if (!looksLikePath && asArgumentAt > -1) {
191-
args.splice(asArgumentAt, 1);
192-
}
181+
looksLikePath = nextOption.indexOf('.css') > -1 ||
182+
/\//.test(nextOption) ||
183+
/\\[^\-]/.test(nextOption) ||
184+
/^https?:\/\//.test(nextOption);
185+
asArgumentAt = args.indexOf(nextOption);
193186

194-
return value;
187+
if (!looksLikePath) {
188+
value = nextOption;
195189
}
196190

197-
function minify(data) {
198-
new CleanCSS(options).minify(data, function (errors, minified) {
199-
var mapFilename;
200-
201-
if (debugMode) {
202-
console.error('Original: %d bytes', minified.stats.originalSize);
203-
console.error('Minified: %d bytes', minified.stats.minifiedSize);
204-
console.error('Efficiency: %d%', ~~(minified.stats.efficiency * 10000) / 100.0);
205-
console.error('Time spent: %dms', minified.stats.timeSpent);
206-
207-
if (minified.inlinedStylesheets.length > 0) {
208-
console.error('Inlined stylesheets:');
209-
minified.inlinedStylesheets.forEach(function (uri) {
210-
console.error('- %s', uri);
211-
});
212-
}
213-
}
191+
if (!looksLikePath && asArgumentAt > -1) {
192+
args.splice(asArgumentAt, 1);
193+
}
214194

215-
outputFeedback(minified.errors, true);
216-
outputFeedback(minified.warnings);
195+
return value;
196+
}
217197

218-
if (minified.errors.length > 0) {
219-
process.exit(1);
198+
function minify(process, options, debugMode, data) {
199+
new CleanCSS(options).minify(data, function (errors, minified) {
200+
var mapFilename;
201+
202+
if (debugMode) {
203+
console.error('Original: %d bytes', minified.stats.originalSize);
204+
console.error('Minified: %d bytes', minified.stats.minifiedSize);
205+
console.error('Efficiency: %d%', ~~(minified.stats.efficiency * 10000) / 100.0);
206+
console.error('Time spent: %dms', minified.stats.timeSpent);
207+
208+
if (minified.inlinedStylesheets.length > 0) {
209+
console.error('Inlined stylesheets:');
210+
minified.inlinedStylesheets.forEach(function (uri) {
211+
console.error('- %s', uri);
212+
});
220213
}
214+
}
221215

222-
if (minified.sourceMap) {
223-
mapFilename = path.basename(options.output) + '.map';
224-
output(minified.styles + '/*# sourceMappingURL=' + mapFilename + ' */');
225-
outputMap(minified.sourceMap, mapFilename);
226-
} else {
227-
output(minified.styles);
228-
}
229-
});
230-
}
216+
outputFeedback(minified.errors, true);
217+
outputFeedback(minified.warnings);
218+
219+
if (minified.errors.length > 0) {
220+
process.exit(1);
221+
}
231222

232-
function output(minified) {
233-
if (options.output) {
234-
fs.writeFileSync(options.output, minified, 'utf8');
223+
if (minified.sourceMap) {
224+
mapFilename = path.basename(options.output) + '.map';
225+
output(process, options, minified.styles + '/*# sourceMappingURL=' + mapFilename + ' */');
226+
outputMap(options, minified.sourceMap, mapFilename);
235227
} else {
236-
process.stdout.write(minified);
228+
output(process, options, minified.styles);
237229
}
238-
}
230+
});
231+
}
239232

240-
function outputMap(sourceMap, mapFilename) {
241-
var mapPath = path.join(path.dirname(options.output), mapFilename);
242-
fs.writeFileSync(mapPath, sourceMap.toString(), 'utf-8');
243-
}
233+
function outputFeedback(messages, isError) {
234+
var prefix = isError ? '\x1B[31mERROR\x1B[39m:' : 'WARNING:';
244235

245-
function outputFeedback(messages, isError) {
246-
var prefix = isError ? '\x1B[31mERROR\x1B[39m:' : 'WARNING:';
236+
messages.forEach(function (message) {
237+
console.error('%s %s', prefix, message);
238+
});
239+
}
247240

248-
messages.forEach(function (message) {
249-
console.error('%s %s', prefix, message);
250-
});
241+
function output(process, options, minified) {
242+
if (options.output) {
243+
fs.writeFileSync(options.output, minified, 'utf8');
244+
} else {
245+
process.stdout.write(minified);
251246
}
252247
}
253248

249+
function outputMap(options, sourceMap, mapFilename) {
250+
var mapPath = path.join(path.dirname(options.output), mapFilename);
251+
fs.writeFileSync(mapPath, sourceMap.toString(), 'utf-8');
252+
}
253+
254254
module.exports = cli;

0 commit comments

Comments
 (0)