antler
Version:
Directory structure linter
73 lines • 3.24 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 constants_1 = require("./constants");
const rules = __importStar(require("./rules"));
function getConfig(fullPath) {
let directoryPath = fullPath;
let directoryName = path_1.default.basename(directoryPath);
let filePath = path_1.default.resolve(directoryPath, constants_1.CONFIG_FILE_NAME);
while (directoryName) {
if (fs_1.default.existsSync(filePath) && fs_1.default.lstatSync(filePath).isFile()) {
let config;
try {
config = JSON.parse(fs_1.default.readFileSync(filePath, constants_1.UTF8));
}
catch (error) {
const message = error && error.message ? error.message : error;
throw new Error(`Error reading config file at ${filePath} - ${message}`);
}
if (!config || typeof config !== 'object' || Array.isArray(config)) {
throw new Error('Invalid config - must be an object');
}
if (!('rules' in config)) {
throw new Error('Invalid config - no rules key');
}
if (!Object.keys(config.rules).length) {
throw new Error('Invalid config - no rules configured');
}
for (const key in config.rules) {
if (!(key in rules)) {
throw new Error(`Unknown rule - ${key}`);
}
}
return {
configPath: filePath,
config,
};
}
directoryPath = path_1.default.resolve(directoryPath, '../');
directoryName = path_1.default.basename(directoryPath);
filePath = path_1.default.resolve(directoryPath, constants_1.CONFIG_FILE_NAME);
}
throw new Error(`Could not find an ${constants_1.CONFIG_FILE_NAME} file anywhere in path ${fullPath}`);
}
exports.default = getConfig;
//# sourceMappingURL=config.js.map