| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const fs = require('fs');
|
|---|
| 4 | const path = require('path');
|
|---|
| 5 | const {promisify} = require('util');
|
|---|
| 6 | const camelcase = require('camelcase');
|
|---|
| 7 | const findUp = require('find-up');
|
|---|
| 8 | const resolveFrom = require('resolve-from');
|
|---|
| 9 | const getPackageType = require('get-package-type');
|
|---|
| 10 |
|
|---|
| 11 | const readFile = promisify(fs.readFile);
|
|---|
| 12 |
|
|---|
| 13 | let loadActive = false;
|
|---|
| 14 |
|
|---|
| 15 | function isLoading() {
|
|---|
| 16 | return loadActive;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | const standardConfigFiles = [
|
|---|
| 20 | '.nycrc',
|
|---|
| 21 | '.nycrc.json',
|
|---|
| 22 | '.nycrc.yml',
|
|---|
| 23 | '.nycrc.yaml',
|
|---|
| 24 | 'nyc.config.js',
|
|---|
| 25 | 'nyc.config.cjs',
|
|---|
| 26 | 'nyc.config.mjs'
|
|---|
| 27 | ];
|
|---|
| 28 |
|
|---|
| 29 | function camelcasedConfig(config) {
|
|---|
| 30 | const results = {};
|
|---|
| 31 | for (const [field, value] of Object.entries(config)) {
|
|---|
| 32 | results[camelcase(field)] = value;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | return results;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | async function findPackage(options) {
|
|---|
| 39 | const cwd = options.cwd || process.env.NYC_CWD || process.cwd();
|
|---|
| 40 | const pkgPath = await findUp('package.json', {cwd});
|
|---|
| 41 | if (pkgPath) {
|
|---|
| 42 | const pkgConfig = JSON.parse(await readFile(pkgPath, 'utf8')).nyc || {};
|
|---|
| 43 | if ('cwd' in pkgConfig) {
|
|---|
| 44 | pkgConfig.cwd = path.resolve(path.dirname(pkgPath), pkgConfig.cwd);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | return {
|
|---|
| 48 | cwd: path.dirname(pkgPath),
|
|---|
| 49 | pkgConfig
|
|---|
| 50 | };
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return {
|
|---|
| 54 | cwd,
|
|---|
| 55 | pkgConfig: {}
|
|---|
| 56 | };
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | async function actualLoad(configFile) {
|
|---|
| 60 | if (!configFile) {
|
|---|
| 61 | return {};
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | const configExt = path.extname(configFile).toLowerCase();
|
|---|
| 65 | switch (configExt) {
|
|---|
| 66 | case '.js':
|
|---|
| 67 | /* istanbul ignore next: coverage for 13.2.0+ is shown in load-esm.js */
|
|---|
| 68 | if (await getPackageType(configFile) === 'module') {
|
|---|
| 69 | return require('./load-esm')(configFile);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /* fallthrough */
|
|---|
| 73 | case '.cjs':
|
|---|
| 74 | return require(configFile);
|
|---|
| 75 | /* istanbul ignore next: coverage for 13.2.0+ is shown in load-esm.js */
|
|---|
| 76 | case '.mjs':
|
|---|
| 77 | return require('./load-esm')(configFile);
|
|---|
| 78 | case '.yml':
|
|---|
| 79 | case '.yaml':
|
|---|
| 80 | return require('js-yaml').load(await readFile(configFile, 'utf8'));
|
|---|
| 81 | default:
|
|---|
| 82 | return JSON.parse(await readFile(configFile, 'utf8'));
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | async function loadFile(configFile) {
|
|---|
| 87 | /* This lets @istanbuljs/esm-loader-hook avoid circular initialization when loading
|
|---|
| 88 | * configuration. This should generally only happen when the loader hook is active
|
|---|
| 89 | * on the main nyc process. */
|
|---|
| 90 | loadActive = true;
|
|---|
| 91 |
|
|---|
| 92 | try {
|
|---|
| 93 | return await actualLoad(configFile);
|
|---|
| 94 | } finally {
|
|---|
| 95 | loadActive = false;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | async function applyExtends(config, filename, loopCheck = new Set()) {
|
|---|
| 100 | config = camelcasedConfig(config);
|
|---|
| 101 | if ('extends' in config) {
|
|---|
| 102 | const extConfigs = [].concat(config.extends);
|
|---|
| 103 | if (extConfigs.some(e => typeof e !== 'string')) {
|
|---|
| 104 | throw new TypeError(`${filename} contains an invalid 'extends' option`);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | delete config.extends;
|
|---|
| 108 | const filePath = path.dirname(filename);
|
|---|
| 109 | for (const extConfig of extConfigs) {
|
|---|
| 110 | const configFile = resolveFrom.silent(filePath, extConfig) ||
|
|---|
| 111 | resolveFrom.silent(filePath, './' + extConfig);
|
|---|
| 112 | if (!configFile) {
|
|---|
| 113 | throw new Error(`Could not resolve configuration file ${extConfig} from ${path.dirname(filename)}.`);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | if (loopCheck.has(configFile)) {
|
|---|
| 117 | throw new Error(`Circular extended configurations: '${configFile}'.`);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | loopCheck.add(configFile);
|
|---|
| 121 |
|
|---|
| 122 | // eslint-disable-next-line no-await-in-loop
|
|---|
| 123 | const configLoaded = await loadFile(configFile);
|
|---|
| 124 | if ('cwd' in configLoaded) {
|
|---|
| 125 | configLoaded.cwd = path.resolve(path.dirname(configFile), configLoaded.cwd);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | Object.assign(
|
|---|
| 129 | config,
|
|---|
| 130 | // eslint-disable-next-line no-await-in-loop
|
|---|
| 131 | await applyExtends(configLoaded, configFile, loopCheck)
|
|---|
| 132 | );
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return config;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | async function loadNycConfig(options = {}) {
|
|---|
| 140 | const {cwd, pkgConfig} = await findPackage(options);
|
|---|
| 141 | const configFiles = [].concat(options.nycrcPath || standardConfigFiles);
|
|---|
| 142 | const configFile = await findUp(configFiles, {cwd});
|
|---|
| 143 | if (options.nycrcPath && !configFile) {
|
|---|
| 144 | throw new Error(`Requested configuration file ${options.nycrcPath} not found`);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | const config = {
|
|---|
| 148 | cwd,
|
|---|
| 149 | ...(await applyExtends(pkgConfig, path.join(cwd, 'package.json'))),
|
|---|
| 150 | ...(await applyExtends(await loadFile(configFile), configFile))
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 | const arrayFields = ['require', 'extension', 'exclude', 'include'];
|
|---|
| 154 | for (const arrayField of arrayFields) {
|
|---|
| 155 | if (config[arrayField]) {
|
|---|
| 156 | config[arrayField] = [].concat(config[arrayField]);
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | return config;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | module.exports = {
|
|---|
| 164 | loadNycConfig,
|
|---|
| 165 | isLoading
|
|---|
| 166 | };
|
|---|