UNPKG

@acot/cli

Version:
235 lines (234 loc) 8.52 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 config_1 = require("@acot/config"); const core_1 = require("@acot/core"); const find_chrome_1 = require("@acot/find-chrome"); const reporter_1 = require("@acot/reporter"); const runner_1 = require("@acot/runner"); const utils_1 = require("@acot/utils"); const is_ci_1 = __importDefault(require("is-ci")); const command_1 = require("../command"); const logging_1 = require("../logging"); const DEFAULT_REPORTER = '@acot/pretty'; const DEFAULT_RUNNER = '@acot'; exports.default = (0, command_1.createCommand)({ name: 'run', summary: 'Running an audit.', args: { paths: { type: 'string', array: true, description: 'Path list of the page to be audit.', }, }, options: { origin: { type: 'string', alias: 'o', description: 'Target server URL origin.', }, command: { type: 'string', alias: 'C', description: 'Command to launch the local server.', }, reporters: { type: 'array', description: `List of the reporter names. (default: "${DEFAULT_REPORTER}")`, }, runner: { type: 'string', description: `Name of the runner. (default: "${DEFAULT_RUNNER}")`, }, 'runner-with': { type: 'string', description: 'Runner options. Specify the JSON as a string.', }, parallel: { type: 'number', alias: 'p', default: os_1.default.cpus().length - 1, description: 'Number of parallel audit browsers. (default: "os.cpus().length - 1")', }, config: { type: 'string', alias: 'c', description: 'Provide path to a acot configuration file (e.g. "./acot.config.js")', }, viewport: { type: 'string', alias: 'V', description: 'Viewport used for browser access. One of JSON string or "<number>x<number>".', }, 'working-dir': { type: 'string', description: 'Directory path used by acot store temporary files. (default: ".acot")', }, 'max-warnings': { type: 'number', description: 'Warning threshold to be treated as an error.', }, 'connection-timeout': { type: 'number', default: 1000 * 60, description: 'Timeout ms for connecting to the host server.', }, 'browser-timeout': { type: 'number', default: 1000 * 30, description: 'Timeout ms to wait for pooled browsers.', }, 'ready-timeout': { type: 'number', default: 1000 * 30, description: 'Timeout ms waiting for page load.', }, 'chrome-channel': { type: 'string', description: 'Channel to search local Chromium. One of "puppeteer", "canary", "stable", "*". (default: "*")', }, 'chrome-executable-path': { type: 'string', description: 'Executable Chromium path.', }, 'launch-options': { type: 'string', description: 'JSON string of launch config for Puppeteer.', }, }, })(async ({ logger, cwd, args }) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; // configure let config; try { const base = await (0, config_1.loadConfig)((_a = args.config) !== null && _a !== void 0 ? _a : '.', { cwd }); const found = await (0, find_chrome_1.findChrome)({ executablePath: args['chrome-executable-path'], channel: ((_c = (_b = args['chrome-channel']) !== null && _b !== void 0 ? _b : base.chromeChannel) !== null && _c !== void 0 ? _c : '*'), }); if (found == null) { throw new Error('Executable Chromium was not found.'); } (0, logging_1.debug)('found chromium: %O', found); const launchOptions = args['launch-options'] ? JSON.parse(args['launch-options']) : base.launchOptions; const connection = (_d = base.connection) !== null && _d !== void 0 ? _d : {}; const override = { workingDir: path_1.default.resolve(cwd, (_f = (_e = args['working-dir']) !== null && _e !== void 0 ? _e : base.workingDir) !== null && _f !== void 0 ? _f : '.acot'), launchOptions: { ...launchOptions, executablePath: found.executablePath, }, connection: { ...connection, command: (_h = (_g = args['command']) !== null && _g !== void 0 ? _g : connection === null || connection === void 0 ? void 0 : connection.command) !== null && _h !== void 0 ? _h : undefined, timeout: (_k = (_j = args['connection-timeout']) !== null && _j !== void 0 ? _j : connection === null || connection === void 0 ? void 0 : connection.timeout) !== null && _k !== void 0 ? _k : undefined, }, paths: args.paths.length > 0 ? args.paths : [], rules: {}, }; if (args.origin) { override.origin = args.origin; } if (args.viewport) { override.viewport = (0, utils_1.parseViewport)(args.viewport); } config = (0, config_1.mergeConfig)(base, override); (0, logging_1.debug)('merged config: %O', config); } catch (e) { logger.error('invalid config:', e); return 1; } // core const acot = new core_1.Acot({ launchOptions: config.launchOptions, parallel: args.parallel, presets: config.presets, origin: config.origin, workingDir: config.workingDir, viewport: config.viewport, browserTimeout: args['browser-timeout'], readyTimeout: args['ready-timeout'], cwd, }); // reporter const reporters = []; try { const cfg = { stdout: logger.getStdout(), stderr: logger.getStderr(), verbose: !!args.verbose || is_ci_1.default, config, options: {}, }; if (args.reporters == null && config.reporters != null) { config.reporters.forEach((reporter) => { var _a; reporters.push(reporter.uses({ ...cfg, options: (_a = reporter.with) !== null && _a !== void 0 ? _a : {}, })); }); } else { const loader = new reporter_1.ReporterLoader(cwd); ((_l = args.reporters) !== null && _l !== void 0 ? _l : [DEFAULT_REPORTER]).forEach((reporter) => { if (typeof reporter === 'string') { reporters.push(loader.load(reporter)(cfg)); } }); } } catch (e) { logger.error(e); return 1; } // runner let runner; try { const cfg = { core: acot, config, options: { ...((_o = (_m = config.runner) === null || _m === void 0 ? void 0 : _m.with) !== null && _o !== void 0 ? _o : {}), ...JSON.parse((_p = args['runner-with']) !== null && _p !== void 0 ? _p : '{}'), }, }; if (args.runner == null && config.runner != null) { runner = config.runner.uses(cfg); } else { const loader = new runner_1.RunnerLoader(cwd); runner = loader.load(args.runner || DEFAULT_RUNNER)(cfg); } } catch (e) { logger.error(e); return 1; } // run try { reporters.forEach((report) => report(runner)); const summary = await runner.run(); const maxWarnings = args['max-warnings']; let code = summary.errorCount > 0 ? 1 : 0; if (code === 1 && maxWarnings != null && maxWarnings >= 0 && summary.warningCount > maxWarnings) { code = 1; } return code; } catch (e) { logger.error(e); return 1; } });