metal-soy-critic
Version:
A metal-soy code validation utility.
114 lines (113 loc) • 4.29 kB
JavaScript
;
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 config_1 = require("./config");
const pkg = require('../package.json');
function main(argv) {
return __awaiter(this, void 0, void 0, function* () {
const cli = program
.version(pkg.version)
.usage('[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);
}
try {
var config = config_1.readConfig();
}
catch (e) {
console.log(chalk.red(`Failed to read config: ${e.message}`));
return process.exit(1);
}
const validations = yield Promise.all(files.map(file => validate(file, config)));
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, config) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield validate_file_1.default(filePath, config);
return [filePath, result];
});
}