UNPKG

2.69 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/**
4 * analyze-css entry point
5 *
6 * @see https://github.com/macbre/analyze-css
7 */
8'use strict';
9
10const { program } = require('commander');
11
12var analyzer = require('./../lib/index'),
13 debug = require('debug')('analyze-css:bin'),
14 runner = require('./../lib/runner'),
15 runnerOpts = {};
16
17// parse options
18program
19 .version(analyzer.version)
20 .usage('--url <url> [options]')
21 // https://www.npmjs.com/package/commander#common-option-types-boolean-and-value
22 .option('--url <url>', 'Set URL of CSS to analyze')
23 .option('--file <file>', 'Set local CSS file to analyze')
24 .option('--ignore-ssl-errors', 'Ignores SSL errors, such as expired or self-signed certificate errors')
25 .option('-p, --pretty', 'Causes JSON with the results to be pretty-printed')
26 .option('-N, --no-offenders', 'Show only the metrics without the offenders part')
27 .option('--auth-user <user>', 'Sets the user name used for HTTP authentication')
28 .option('--auth-pass <pass>', 'Sets the password used for HTTP authentication')
29 .option('-x, --proxy <proxy>', 'Sets the HTTP proxy');
30
31// parse it
32program.parse(process.argv);
33
34debug('analyze-css v%s', analyzer.version);
35
36// support stdin (issue #28)
37if (process.argv.indexOf('-') > -1) {
38 runnerOpts.stdin = true;
39}
40// --url
41else if (program.url) {
42 runnerOpts.url = program.url;
43}
44// --file
45else if (program.file) {
46 runnerOpts.file = program.file;
47}
48// either --url or --file or - (stdin) needs to be provided
49else {
50 console.log(program.helpInformation());
51 process.exit(analyzer.EXIT_NEED_OPTIONS);
52}
53
54runnerOpts.ignoreSslErrors = program['ignore-ssl-errors'];
55runnerOpts.noOffenders = program.offenders === false;
56runnerOpts.authUser = program['auth-user'];
57runnerOpts.authPass = program['auth-pass'];
58runnerOpts.proxy = program.proxy;
59
60debug('opts: %j', runnerOpts);
61
62// run the analyzer
63runner(runnerOpts, function(err, res) {
64 var output, exitCode;
65
66 // emit an error and die
67 if (err) {
68 exitCode = err.code || 1;
69 debug('Exiting with exit code #%d', exitCode);
70
71 console.error(err.toString());
72 process.exit(exitCode);
73 }
74
75 // make offenders flat (and append position if possible - issue #25)
76 if (typeof res.offenders !== 'undefined') {
77 Object.keys(res.offenders).forEach(function(metricName) {
78 res.offenders[metricName] = res.offenders[metricName].map(function(offender) {
79 var position = offender.position && offender.position.start;
80 return offender.message + (position ? ' @ ' + position.line + ':' + position.column : '');
81 });
82 });
83 }
84
85 // format the results
86 if (program.pretty === true) {
87 output = JSON.stringify(res, null, ' ');
88 } else {
89 output = JSON.stringify(res);
90 }
91
92 process.stdout.write(output);
93});