lib/utils/apply-extends.ts

79 LOC · 79 covered · 0 uncovered · 24 ranges · 1132 concepts · 12 introducers · 794 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > import {Dictionary, PlatformShim} from '../typings/common-types.js'; apply-extends.ts ×4
2 > import {YError} from '../yerror.js';
3 >
4 > let previouslyVisitedConfigs: string[] = [];
5 > let shim: PlatformShim;
6 > export function applyExtends(
7 > config: Dictionary, apply-extends.ts ×2
8 > cwd: string,
9 > mergeExtends: boolean,
10 > _shim: PlatformShim
11 > ): Dictionary {
12 > shim = _shim;
13 > let defaultConfig = {};
14 >
15 > if (Object.prototype.hasOwnProperty.call(config, 'extends')) {
16 > if (typeof config.extends !== 'string') return defaultConfig; apply-extends.ts ×3
17 > const isPath = /\.json|\..*rc$/.test(config.extends);
18 > let pathToDefault: string | null = null;
19 > if (!isPath) {
21 > pathToDefault = import.meta.resolve(config.extends);
22 > } catch (_err) {
23 > // maybe the module uses key for some other reason, apply-extends.ts ×1
24 > // err on side of caution.
25 > return config;
26 > }
27 > } else { apply-extends.ts ×3
28 > pathToDefault = getPathToDefaultConfig(cwd, config.extends); apply-extends.ts ×2
29 > }
31 > checkForCircularExtends(pathToDefault);
32 >
33 > previouslyVisitedConfigs.push(pathToDefault);
34 >
35 > defaultConfig = isPath
36 > ? JSON.parse(shim.readFileSync(pathToDefault, 'utf8'))
37 > : _shim.require(config.extends); apply-extends.ts ×3
38 > delete config.extends;
39 > defaultConfig = applyExtends(
40 > defaultConfig,
41 > shim.path.dirname(pathToDefault),
42 > mergeExtends,
43 > shim
44 > );
45 > }
47 > previouslyVisitedConfigs = [];
48 >
49 > return mergeExtends
50 > ? mergeDeep(defaultConfig, config)
51 > : Object.assign({}, defaultConfig, config); apply-extends.ts ×2
52 > }
54 > function checkForCircularExtends(cfgPath: string) { apply-extends.ts ×3
55 > if (previouslyVisitedConfigs.indexOf(cfgPath) > -1) {
56 > throw new YError(`Circular extended configurations: '${cfgPath}'.`); apply-extends.ts ×1
57 > }
60 > function getPathToDefaultConfig(cwd: string, pathToExtend: string) { apply-extends.ts ×2
61 > return shim.path.resolve(cwd, pathToExtend);
62 > }
64 > function mergeDeep(config1: Dictionary, config2: Dictionary) { apply-extends.ts ×3
65 > const target: Dictionary = {};
66 > function isObject(obj: Dictionary | any): obj is Dictionary {
67 > return obj && typeof obj === 'object' && !Array.isArray(obj); apply-extends.ts ×2
68 > }
69 > Object.assign(target, config1); apply-extends.ts ×3
70 > for (const key of Object.keys(config2)) {
71 > if (key === '__proto__') continue;
72 > if (isObject(config2[key]) && isObject(target[key])) {
73 > target[key] = mergeDeep(config1[key], config2[key]); apply-extends.ts ×1
74 > } else { apply-extends.ts ×2
75 > target[key] = config2[key];
76 > }
78 > return target;
79 > }