UNPKG

@acot/cli

Version:
86 lines (85 loc) 2.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const document_1 = require("@acot/document"); const chalk_1 = __importDefault(require("chalk")); const portfinder_1 = require("portfinder"); const command_1 = require("../command"); const constants_1 = require("../constants"); const debug = require('debug')('acot:cli'); exports.default = (0, command_1.createCommand)({ name: 'test', summary: 'Test the rules provided by the preset according to the documentation.', args: { pattern: { type: 'string', description: 'Rule name pattern to include in the test target.', }, }, options: { project: { type: 'string', alias: 'p', default: '.', description: 'Directory path that contains the package.json that makes up the preset.', }, docs: { type: 'string', alias: 'd', default: path_1.default.join('docs', 'rules'), description: 'Directory path that contains the rule documentation.', }, port: { type: 'number', description: 'Port number for preview server.', }, parallel: { type: 'number', default: os_1.default.cpus().length - 1, description: 'Number of parallel audit browsers. (default: "os.cpus().length - 1")', }, }, })(async ({ cwd, logger, args }) => { const proj = path_1.default.resolve(cwd, args.project); let project; try { const loader = new document_1.DocProjectLoader({ docs: args.docs }); project = await loader.load(proj); } catch (e) { logger.error('DocProjectLoader#load Error:', e); return 1; } if (project.codes.length === 0) { logger.print('Can\'t find the document files. (from: "%s")', args.docs); return 1; } const port = args.port || (await (0, portfinder_1.getPortPromise)({ port: constants_1.DEFAULT_PORT })); const server = new document_1.DocServer({ dev: false, port, }); const reporter = new document_1.DocReporter({ origin: `http://localhost:${port}`, color: chalk_1.default.supportsColor !== false, stdout: logger.getStdout(), stderr: logger.getStderr(), }); const runner = new document_1.DocRunner(server, reporter, { project, parallel: args.parallel, }); try { const result = await runner.run(args.pattern); debug('result: %O', result); return result.errors.length > 0 ? 1 : 0; } catch (e) { logger.error('DocTester#test Error:', e); return 1; } });