antler
Version:
Directory structure linter
71 lines • 2.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const antler_error_1 = require("./antler-error");
const antler_warning_1 = require("./antler-warning");
const constants_1 = require("./constants");
const MATCHES_LEADING_SLASH = /^\//;
function createNode(rootPath, fullPath) {
const parentFullPath = path_1.default.resolve(fullPath, '../');
const parentName = path_1.default.basename(parentFullPath);
const shortPath = fullPath
.replace(rootPath, '')
.replace(MATCHES_LEADING_SLASH, '');
const name = path_1.default.basename(fullPath);
let childNames = [];
let siblingNamesIncludingSelf = [];
let isDirectory = false;
if (fs_1.default.lstatSync(fullPath).isDirectory()) {
isDirectory = true;
childNames = fs_1.default.readdirSync(fullPath);
}
if (fs_1.default.lstatSync(parentFullPath).isDirectory()) {
siblingNamesIncludingSelf = fs_1.default.readdirSync(parentFullPath);
}
return {
parentName,
path: shortPath,
name,
fullPath,
isDirectory,
siblingNamesIncludingSelf,
childNames,
};
}
function crawl(rootPath, node, ruleInstances, indent, reportWarning, reportError) {
ruleInstances.forEach((instance) => {
try {
instance.run(node);
}
catch (error) {
const message = error && error.message ? error.message : error;
// eslint-disable-next-line no-console
console.error(`${constants_1.MESSAGE_PREFIX}${message}`);
if (error instanceof antler_warning_1.AntlerWarning) {
reportWarning();
}
else if (error instanceof antler_error_1.AntlerError) {
reportError();
}
else {
process.exit(1);
}
}
});
node.childNames.forEach((childName) => {
const fullPath = path_1.default.resolve(node.fullPath, childName);
const childNode = createNode(rootPath, fullPath);
crawl(rootPath, childNode, ruleInstances, ` ${indent}`, reportWarning, reportError);
});
}
function beginCrawl(fullPath, ruleInstances, reportWarning, reportError) {
const rootPath = path_1.default.resolve(fullPath, '../');
const node = createNode(rootPath, fullPath);
crawl(rootPath, node, ruleInstances, '', reportWarning, reportError);
}
exports.default = beginCrawl;
//# sourceMappingURL=index.js.map