UNPKG

metal-soy-critic

Version:
106 lines (105 loc) 4.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path = require("path"); const chalk = require("chalk"); const program = require("commander"); const validate_file_1 = require("./validate-file"); const glob = require("glob"); const pkg = require('../package.json'); function main(argv) { return __awaiter(this, void 0, void 0, function* () { const cli = program .version(pkg.version) .usage('mcritic [options] <path>') .option('-i, --ignore <ignore>', 'A glob to ignore files, if passed a directory') .option('-v, --verbose', 'Output for all files') .parse(argv); if (!program.args.length) { cli.help(); } const filePath = program.args[0]; try { var files = yield getSoyFiles(filePath, program.ignore); } catch (e) { console.log(chalk.red(`Failed to find Soy files using path: "${filePath}"`)); return process.exit(1); } const validations = yield Promise.all(files.map(validate)); const failed = validations.filter(([_, result]) => !result.status); const passed = validations.filter(([_, result]) => result.status); console.log(chalk[failed.length ? 'red' : 'green'](`${failed.length} out of ${validations.length} file(s) have problems:\n`)); if (program.verbose && passed.length) { passed.forEach(printValidation); } if (failed.length) { failed.forEach(printValidation); return process.exit(1); } return process.exit(0); }); } exports.main = main; function getSoyFiles(filePath, ignore) { return __awaiter(this, void 0, void 0, function* () { const isDir = yield pathIsDir(filePath); if (isDir) { return new Promise((resolve, reject) => { glob(path.resolve(filePath, '**', '*.soy'), { ignore }, (err, matches) => { if (err) { return reject(err); } resolve(matches.map(filePath => path.relative('', filePath))); }); }); } return Promise.resolve([filePath]); }); } function pathIsDir(filePath) { return new Promise((resolve, reject) => { fs.stat(filePath, (err, stats) => { if (err) { console.log(err); return reject(err); } resolve(stats.isDirectory()); }); }); } function printValidation([filePath, result]) { const { messages, status } = result; printHeader(`File - ${filePath}`); if (status) { printIndented('No problems were found.'); } else { messages.forEach(message => printIndented(message)); } } function printIndented(message = '', indentSize = 2, symbol = ' ') { const indentStr = chalk.black(symbol.repeat(indentSize)); console.log(); message .split('\n') .forEach(line => console.log(indentStr, ' ', line)); console.log('\n'); } function printHeader(content = '') { console.log(chalk.yellow(content)); } function validate(filePath) { return __awaiter(this, void 0, void 0, function* () { const result = yield validate_file_1.default(filePath); return [filePath, result]; }); }