UNPKG

1.91 kBJavaScriptView Raw
1'use strict';
2const pluralize = require('pluralize'),
3 getStdin = require('get-stdin'),
4 options = require('./cli-options'),
5 linter = require('../lib/cmd/lint'),
6 reporter = require('../lib/reporters');
7
8function builder(yargs) {
9 return yargs
10 .usage('Usage: $0 lint [url]')
11 .example('$0 lint domain.com/_components/foo', 'Lint component')
12 .example('$0 lint domain.com/_pages/foo', 'Lint page')
13 .example('$0 lint domain.com/some-slug', 'Lint public url')
14 .example('$0 lint < path/to/schema.yml', 'Lint schema file')
15 .option('c', options.concurrency)
16 .option('r', options.reporter);
17}
18
19function handler(argv) {
20 const log = reporter.log(argv.reporter, 'lint');
21
22 return getStdin().then((str) => {
23 if (str) { // lint schema from stdin
24 log('Linting schema...');
25 return linter.lintSchema(str)
26 // no dot logging of individual schema linting, since it's just a single dot
27 .toArray(reporter.logSummary(argv.reporter, 'lint', (successes, errors) => {
28 if (errors) {
29 return { success: false, message: `Schema has ${pluralize('error', errors, true)}` };
30 } else {
31 return { success: true, message: 'Schema has no issues' };
32 }
33 }));
34 } else { // lint url
35 log('Linting url...');
36 return linter.lintUrl(argv.url)
37 .map(reporter.logAction(argv.reporter, 'lint'))
38 .toArray(reporter.logSummary(argv.reporter, 'lint', (successes, errors) => {
39 if (errors) {
40 return { success: false, message: `Missing ${pluralize('reference', errors, true)}`};
41 } else {
42 return { success: true, message: `All references exist! (checked ${pluralize('uri', successes, true)})` };
43 }
44 }));
45 }
46 });
47}
48
49module.exports = {
50 command: 'lint [url]',
51 describe: 'Lint urls or schemas',
52 aliases: ['linter', 'l'],
53 builder,
54 handler
55};