UNPKG

4.6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
8
9var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
10
11exports.default = resolveExtends;
12
13var _path = require('path');
14
15var _path2 = _interopRequireDefault(_path);
16
17require('resolve-global');
18
19var _requireUncached = require('require-uncached');
20
21var _requireUncached2 = _interopRequireDefault(_requireUncached);
22
23var _resolveFrom = require('resolve-from');
24
25var _resolveFrom2 = _interopRequireDefault(_resolveFrom);
26
27var _lodash = require('lodash.merge');
28
29var _lodash2 = _interopRequireDefault(_lodash);
30
31var _lodash3 = require('lodash.omit');
32
33var _lodash4 = _interopRequireDefault(_lodash3);
34
35function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36
37// Resolve extend configs
38// eslint-disable-line import/no-unassigned-import
39function resolveExtends() {
40 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
41 var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
42 var e = config.extends;
43
44 var extended = loadExtends(config, context).reduceRight(function (r, c) {
45 return (0, _lodash2.default)(r, (0, _lodash4.default)(c, 'extends'));
46 }, e ? { extends: e } : {});
47
48 // Remove deprecation warning in version 3
49 if (typeof config === 'object' && 'wildcards' in config) {
50 console.warn(`'wildcards' found in top-level configuration ignored. Remove them from your config to silence this warning.`);
51 }
52
53 return (0, _lodash2.default)({}, extended, config);
54}
55
56// (any, string, string, Function) => any[];
57function loadExtends() {
58 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
59 var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
60
61 return (config.extends || []).reduce(function (configs, raw) {
62 var load = context.require || require;
63 var resolved = resolveConfig(raw, context);
64 var c = load(resolved);
65 var cwd = _path2.default.dirname(resolved);
66
67 // Remove deprecation warning in version 3
68 if (typeof c === 'object' && 'wildcards' in c) {
69 console.warn(`'wildcards' found in '${raw}' ignored. To silence this warning raise an issue at 'npm repo ${raw}' to remove the wildcards.`);
70 }
71
72 var ctx = (0, _lodash2.default)({}, context, { cwd });
73
74 // Resolve parser preset if none was present before
75 if (!context.parserPreset && typeof c === 'object' && typeof c.parserPreset === 'string') {
76 var resolvedParserPreset = (0, _resolveFrom2.default)(cwd, c.parserPreset);
77 var parserPreset = {
78 name: c.parserPreset,
79 path: `./${_path2.default.relative(process.cwd(), resolvedParserPreset)}`.split(_path2.default.sep).join('/'),
80 parserOpts: require(resolvedParserPreset)
81 };
82
83 ctx.parserPreset = parserPreset;
84 config.parserPreset = parserPreset;
85 }
86
87 return [].concat((0, _toConsumableArray3.default)(configs), [c], (0, _toConsumableArray3.default)(loadExtends(c, ctx)));
88 }, []);
89}
90
91function getId() {
92 var raw = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
93 var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
94
95 var first = raw.charAt(0);
96 var scoped = first === '@';
97 var relative = first === '.';
98 return scoped || relative ? raw : [prefix, raw].filter(String).join('-');
99}
100
101function resolveConfig(raw) {
102 var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
103
104 var resolve = context.resolve || resolveId;
105 var id = getId(raw, context.prefix);
106
107 try {
108 return resolve(id, context);
109 } catch (err) {
110 var legacy = getId(raw, 'conventional-changelog-lint-config');
111 var resolved = resolve(legacy, context);
112 console.warn(`Resolving ${raw} to legacy config ${legacy}. To silence this warning raise an issue at 'npm repo ${legacy}' to rename to ${id}.`);
113 return resolved;
114 }
115}
116
117function resolveId(id) {
118 var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
119
120 var cwd = context.cwd || process.cwd();
121 var localPath = _resolveFrom2.default.silent(cwd, id);
122
123 if (typeof localPath === 'string') {
124 return localPath;
125 }
126
127 var resolveGlobal = (0, _requireUncached2.default)('resolve-global');
128 var globalPath = resolveGlobal.silent(id);
129
130 if (typeof globalPath === 'string') {
131 return globalPath;
132 }
133
134 var err = new Error(`Cannot find module "${id}" from "${cwd}"`);
135 err.code = 'MODULE_NOT_FOUND';
136 throw err;
137}
138module.exports = exports['default'];
139//# sourceMappingURL=index.js.map
\No newline at end of file