1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const { program } = require('commander');
|
11 |
|
12 | var analyzer = require('./../lib/index'),
|
13 | debug = require('debug')('analyze-css:bin'),
|
14 | runner = require('./../lib/runner'),
|
15 | runnerOpts = {};
|
16 |
|
17 |
|
18 | program
|
19 | .version(analyzer.version)
|
20 | .usage('--url <url> [options]')
|
21 |
|
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 |
|
32 | program.parse(process.argv);
|
33 |
|
34 | debug('analyze-css v%s', analyzer.version);
|
35 |
|
36 |
|
37 | if (process.argv.indexOf('-') > -1) {
|
38 | runnerOpts.stdin = true;
|
39 | }
|
40 |
|
41 | else if (program.url) {
|
42 | runnerOpts.url = program.url;
|
43 | }
|
44 |
|
45 | else if (program.file) {
|
46 | runnerOpts.file = program.file;
|
47 | }
|
48 |
|
49 | else {
|
50 | console.log(program.helpInformation());
|
51 | process.exit(analyzer.EXIT_NEED_OPTIONS);
|
52 | }
|
53 |
|
54 | runnerOpts.ignoreSslErrors = program['ignore-ssl-errors'];
|
55 | runnerOpts.noOffenders = program.offenders === false;
|
56 | runnerOpts.authUser = program['auth-user'];
|
57 | runnerOpts.authPass = program['auth-pass'];
|
58 | runnerOpts.proxy = program.proxy;
|
59 |
|
60 | debug('opts: %j', runnerOpts);
|
61 |
|
62 |
|
63 | runner(runnerOpts, function(err, res) {
|
64 | var output, exitCode;
|
65 |
|
66 |
|
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 |
|
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 |
|
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 | });
|