1 | const {castArray, pickBy, isNil, isString, isPlainObject} = require('lodash');
|
2 | const readPkgUp = require('read-pkg-up');
|
3 | const cosmiconfig = require('cosmiconfig');
|
4 | const resolveFrom = require('resolve-from');
|
5 | const debug = require('debug')('semantic-release:config');
|
6 | const {repoUrl} = require('./git');
|
7 | const PLUGINS_DEFINITIONS = require('./definitions/plugins');
|
8 | const plugins = require('./plugins');
|
9 | const {validatePlugin, parseConfig} = require('./plugins/utils');
|
10 |
|
11 | const CONFIG_NAME = 'release';
|
12 | const CONFIG_FILES = [
|
13 | 'package.json',
|
14 | `.${CONFIG_NAME}rc`,
|
15 | `.${CONFIG_NAME}rc.json`,
|
16 | `.${CONFIG_NAME}rc.yaml`,
|
17 | `.${CONFIG_NAME}rc.yml`,
|
18 | `.${CONFIG_NAME}rc.js`,
|
19 | `${CONFIG_NAME}.config.js`,
|
20 | ];
|
21 |
|
22 | module.exports = async (context, opts) => {
|
23 | const {cwd, env} = context;
|
24 | const {config, filepath} = (await cosmiconfig(CONFIG_NAME, {searchPlaces: CONFIG_FILES}).search(cwd)) || {};
|
25 |
|
26 | debug('load config from: %s', filepath);
|
27 |
|
28 |
|
29 | let options = {...config, ...opts};
|
30 | if (options.ci === false) {
|
31 | options.noCi = true;
|
32 | }
|
33 |
|
34 | const pluginsPath = {};
|
35 | let extendPaths;
|
36 | ({extends: extendPaths, ...options} = options);
|
37 | if (extendPaths) {
|
38 |
|
39 | options = {
|
40 | ...castArray(extendPaths).reduce((result, extendPath) => {
|
41 | const extendsOpts = require(resolveFrom.silent(__dirname, extendPath) || resolveFrom(cwd, extendPath));
|
42 |
|
43 |
|
44 |
|
45 | Object.entries(extendsOpts)
|
46 | .filter(([, value]) => Boolean(value))
|
47 | .reduce((pluginsPath, [option, value]) => {
|
48 | castArray(value).forEach(plugin => {
|
49 | if (option === 'plugins' && validatePlugin(plugin)) {
|
50 | pluginsPath[parseConfig(plugin)[0]] = extendPath;
|
51 | } else if (
|
52 | PLUGINS_DEFINITIONS[option] &&
|
53 | (isString(plugin) || (isPlainObject(plugin) && isString(plugin.path)))
|
54 | ) {
|
55 | pluginsPath[isString(plugin) ? plugin : plugin.path] = extendPath;
|
56 | }
|
57 | });
|
58 | return pluginsPath;
|
59 | }, pluginsPath);
|
60 |
|
61 | return {...result, ...extendsOpts};
|
62 | }, {}),
|
63 | ...options,
|
64 | };
|
65 | }
|
66 |
|
67 |
|
68 | options = {
|
69 | branch: 'master',
|
70 | repositoryUrl: (await pkgRepoUrl({normalize: false, cwd})) || (await repoUrl({cwd, env})),
|
71 | tagFormat: `v\${version}`,
|
72 | plugins: [
|
73 | '@semantic-release/commit-analyzer',
|
74 | '@semantic-release/release-notes-generator',
|
75 | '@semantic-release/npm',
|
76 | '@semantic-release/github',
|
77 | ],
|
78 |
|
79 | ...pickBy(options, option => !isNil(option)),
|
80 | };
|
81 |
|
82 | debug('options values: %O', options);
|
83 |
|
84 | return {options, plugins: await plugins({...context, options}, pluginsPath)};
|
85 | };
|
86 |
|
87 | async function pkgRepoUrl(opts) {
|
88 | const {packageJson} = (await readPkgUp(opts)) || {};
|
89 | return packageJson && (isPlainObject(packageJson.repository) ? packageJson.repository.url : packageJson.repository);
|
90 | }
|