@lint-todo/utils
Version:
 [](https://badge.fury.io/js/%40lint-todo%2Futils) [](h
149 lines • 5.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateConfig = exports.getTodoConfig = void 0;
const tslib_1 = require("tslib");
const find_up_1 = tslib_1.__importDefault(require("find-up"));
/**
* Gets the todo configuration from one of a number of locations.
*
* @example
* Using the package.json
* ```json
* {
* "lintTodo": {
* "some-engine": {
* "daysToDecay": {
* "warn": 5,
* "error": 10
* },
* "daysToDecayByRule": {
* "no-bare-strings": { "warn": 10, "error": 20 }
* }
* }
* }
* }
* ```
*
* @example
* Using the .lint-todorc.js file
* ```js
* module.exports = {
* "some-engine": {
* "daysToDecay": {
* "warn": 5,
* "error": 10
* },
* "daysToDecayByRule": {
* "no-bare-strings": { "warn": 10, "error": 20 }
* }
* }
* }
* ```
*
* @example
* Using environment variables (`TODO_DAYS_TO_WARN` or `TODO_DAYS_TO_ERROR`)
* - Env vars override package.json config
*
* @example
* Passed in directly, such as from command line options.
* - Passed in options override both env vars and package.json config
*
* @param baseDir - The base directory that contains the project's package.json.
* @param engine - The engine for this configuration, eg. eslint
* @param customDaysToDecay - The optional custom days to decay configuration.
* @returns - The todo config object.
*/
function getTodoConfig(baseDir, engine, customDaysToDecay = {}) {
let todoConfig = getFromConfigFile(baseDir, engine);
const daysToDecayEnvVars = getFromEnvVars();
let mergedDaysToDecay = Object.assign({}, todoConfig === null || todoConfig === void 0 ? void 0 : todoConfig.daysToDecay, daysToDecayEnvVars, customDaysToDecay);
// we set a default config if the mergedConfig is an empty object, meaning either or both warn and error aren't
// defined and the package.json doesn't explicitly define an empty config (they're opting out of defining a todoConfig)
if (Object.keys(mergedDaysToDecay).length === 0 && typeof todoConfig === 'undefined') {
mergedDaysToDecay = {
warn: 30,
error: 60,
};
}
if (typeof mergedDaysToDecay.warn === 'number' &&
typeof mergedDaysToDecay.error === 'number' &&
mergedDaysToDecay.warn >= mergedDaysToDecay.error) {
throw new Error(`The provided todo configuration contains invalid values. The \`warn\` value (${mergedDaysToDecay.warn}) must be less than the \`error\` value (${mergedDaysToDecay.error}).`);
}
if (!todoConfig) {
todoConfig = {};
}
todoConfig.daysToDecay = mergedDaysToDecay;
return todoConfig;
}
exports.getTodoConfig = getTodoConfig;
/**
* Validates whether we have a unique config in a single location.
*
* @param baseDir - The base directory that contains the project's package.json.
* @returns A ConfigValidationResult that indicates whether a config is unique
*/
function validateConfig(baseDir) {
const pkg = requireFile(baseDir, 'package.json');
const lintTodorc = requireFile(baseDir, '.lint-todorc.js');
const validationResult = {
pkg,
lintTodorc,
isValid: !((pkg === null || pkg === void 0 ? void 0 : pkg.lintTodo) && lintTodorc),
};
if (!validationResult.isValid) {
validationResult.message =
'You cannot have todo configurations in both package.json and .lint-todorc.js. Please move the configuration from the package.json to the .lint-todorc.js';
}
return validationResult;
}
exports.validateConfig = validateConfig;
function isTodoConfig(config) {
return (Object.keys(config).length === 0 || Object.prototype.hasOwnProperty.call(config, 'daysToDecay'));
}
function getFromConfigFile(baseDir, engine) {
var _a, _b;
const result = validateConfig(baseDir);
if (!result.isValid) {
throw new Error(result.message);
}
const todoConfig = (_a = result.lintTodorc) !== null && _a !== void 0 ? _a : (_b = result.pkg) === null || _b === void 0 ? void 0 : _b.lintTodo;
if (todoConfig === undefined) {
return;
}
// either an empty config or a legacy config where the object only had a top-level daysToDecay property
if (isTodoConfig(todoConfig)) {
return todoConfig;
}
return todoConfig[engine];
}
function requireFile(baseDir, fileName) {
try {
const filePath = find_up_1.default.sync(fileName, {
cwd: baseDir,
});
return filePath && require(filePath); // eslint-disable-line unicorn/prefer-module
}
catch (_a) {
return;
}
}
function getFromEnvVars() {
const config = {};
const warn = getEnvVar('TODO_DAYS_TO_WARN');
const error = getEnvVar('TODO_DAYS_TO_ERROR');
if (Number.isInteger(warn)) {
config.warn = warn;
}
if (Number.isInteger(error)) {
config.error = error;
}
return config;
}
function getEnvVar(name) {
if (process.env[name]) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return Number.parseInt(process.env[name], 10);
}
}
//# sourceMappingURL=todo-config.js.map