UNPKG

2.94 kBJavaScriptView Raw
1#!/usr/bin/env node
2/**
3 * Copyright 2017 Google Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18const puppeteer = require('../..');
19const path = require('path');
20const SourceFactory = require('./SourceFactory');
21
22const PROJECT_DIR = path.join(__dirname, '..', '..');
23
24const RED_COLOR = '\x1b[31m';
25const YELLOW_COLOR = '\x1b[33m';
26const RESET_COLOR = '\x1b[0m';
27
28run();
29
30async function run() {
31 const startTime = Date.now();
32
33 const sourceFactory = new SourceFactory();
34 /** @type {!Array<!Message>} */
35 const messages = [];
36
37 // Documentation checks.
38 {
39 const apiSource = await sourceFactory.readFile(path.join(PROJECT_DIR, 'docs', 'api.md'));
40 const mdSources = [apiSource];
41
42 const toc = require('./toc');
43 messages.push(...await toc(mdSources));
44
45 const preprocessor = require('./preprocessor');
46 messages.push(...await preprocessor(mdSources));
47
48 const browser = await puppeteer.launch({args: ['--no-sandbox']});
49 const page = await browser.newPage();
50 const checkPublicAPI = require('./check_public_api');
51 const jsSources = await sourceFactory.readdir(path.join(PROJECT_DIR, 'lib'), '.js');
52 messages.push(...await checkPublicAPI(page, mdSources, jsSources));
53 await browser.close();
54 }
55
56 // Report results.
57 const errors = messages.filter(message => message.type === 'error');
58 if (errors.length) {
59 console.log('DocLint Failures:');
60 for (let i = 0; i < errors.length; ++i) {
61 let error = errors[i].text;
62 error = error.split('\n').join('\n ');
63 console.log(` ${i + 1}) ${RED_COLOR}${error}${RESET_COLOR}`);
64 }
65 }
66 const warnings = messages.filter(message => message.type === 'warning');
67 if (warnings.length) {
68 console.log('DocLint Warnings:');
69 for (let i = 0; i < warnings.length; ++i) {
70 let warning = warnings[i].text;
71 warning = warning.split('\n').join('\n ');
72 console.log(` ${i + 1}) ${YELLOW_COLOR}${warning}${RESET_COLOR}`);
73 }
74 }
75 let clearExit = messages.length === 0;
76 if (await sourceFactory.saveChangedSources()) {
77 if (clearExit)
78 console.log(`${YELLOW_COLOR}Some files were updated.${RESET_COLOR}`);
79 clearExit = false;
80 }
81 console.log(`${errors.length} failures, ${warnings.length} warnings.`);
82 const runningTime = Date.now() - startTime;
83 console.log(`DocLint Finished in ${runningTime / 1000} seconds`);
84 process.exit(clearExit ? 0 : 1);
85}