@pokujs/c8
Version:
92 lines (91 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfig = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const jsonc_min_1 = require("jsonc.min");
const toml_min_1 = require("toml.min");
const yaml_min_1 = require("yaml.min");
const scriptExtensions = new Set([
'.js',
'.mjs',
'.cjs',
'.ts',
'.mts',
'.cts',
]);
const isScript = (path) => scriptExtensions.has(getExtension(path));
const isToml = (path) => getExtension(path) === '.toml';
const isYaml = (path) => {
const ext = getExtension(path);
return ext === '.yml' || ext === '.yaml';
};
const getExtension = (filePath) => {
const dotIndex = filePath.lastIndexOf('.');
if (dotIndex === -1)
return '';
return filePath.slice(dotIndex);
};
const parseConfig = (content, filePath) => {
if (isToml(filePath))
return (0, toml_min_1.parse)(content);
if (isYaml(filePath))
return (0, yaml_min_1.parse)(content);
return jsonc_min_1.JSONC.parse(content);
};
const kebabMap = {
'reports-dir': 'reportsDirectory',
'report-dir': 'reportsDirectory',
'temp-directory': 'tempDirectory',
'check-coverage': 'checkCoverage',
'per-file': 'perFile',
'skip-full': 'skipFull',
'exclude-after-remap': 'excludeAfterRemap',
'merge-async': 'mergeAsync',
};
const mapKeys = (raw) => {
const result = {};
for (const [key, value] of Object.entries(raw)) {
if (key === 'experimental-monocart') {
if (value)
result.experimental = ['monocart'];
continue;
}
result[kebabMap[key] ?? key] = value;
}
return result;
};
const loadConfig = (cwd, customPath) => {
if (customPath === false)
return Object.create(null);
const expectedFiles = customPath
? [customPath]
: [
'.c8rc',
'.c8rc.json',
'.c8rc.jsonc',
'.c8rc.toml',
'.c8rc.yaml',
'.c8rc.yml',
'.nycrc',
'.nycrc.json',
'.nycrc.jsonc',
'.nycrc.toml',
'.nycrc.yaml',
'.nycrc.yml',
];
for (const file of expectedFiles) {
if (isScript(file))
continue;
const filePath = (0, node_path_1.join)(cwd, file);
if (!(0, node_fs_1.existsSync)(filePath))
continue;
try {
const content = (0, node_fs_1.readFileSync)(filePath, 'utf8');
return mapKeys(parseConfig(content, file));
}
catch { }
}
return Object.create(null);
};
exports.loadConfig = loadConfig;