@codechecks/client
Version:
Open source platform for code review automation
54 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../logger");
const path_1 = require("path");
const execution_1 = require("./execution");
const errors_1 = require("../utils/errors");
async function executeCodechecksJson(path, checkNameMapper = exports.standardNameMapper(path)) {
const json = loadJson(path);
return executeCodechecksJsonString(json, checkNameMapper);
}
exports.executeCodechecksJson = executeCodechecksJson;
function loadJson(path) {
return require(path);
}
exports.loadJson = loadJson;
async function executeCodechecksJsonString(json, checkNameMapper) {
const checks = json.checks;
for (const check of checks) {
const moduleName = checkNameMapper(check.name);
logger_1.logger.debug(`Executing ${check.name} => ${moduleName}`);
await execution_1.executeCodechecksFile(moduleName, check.options);
}
}
exports.executeCodechecksJsonString = executeCodechecksJsonString;
/**
* Maps "check" name to node_module dependency.
* 1. Check if @codechecks/CHECK_NAME exists
* 2. Check CHECK_NAME
*/
exports.standardNameMapper = (path) => (checkName) => {
if (checkIfModuleExists(`@codechecks/${checkName}`)) {
return `@codechecks/${checkName}`;
}
if (checkIfModuleExists(checkName)) {
return checkName;
}
if (checkIfModuleExists(path_1.join(path_1.dirname(path), checkName))) {
return path_1.join(path_1.dirname(path), checkName);
}
throw errors_1.crash(`Module ${checkName} couldn't be found. Tried:
- @codechecks/${checkName}
- ${checkName}
`);
};
function checkIfModuleExists(moduleName) {
try {
require.resolve(moduleName);
return true;
}
catch (_a) {
return false;
}
}
//# sourceMappingURL=executeJson.js.map