UNPKG

2.59 kBJavaScriptView Raw
1const {isPlainObject, isArray, template, castArray, uniq} = require('lodash');
2const micromatch = require('micromatch');
3const dirGlob = require('dir-glob');
4const pReduce = require('p-reduce');
5const debug = require('debug')('semantic-release:git');
6const resolveConfig = require('./resolve-config');
7const {getModifiedFiles, add, commit, push} = require('./git');
8
9/**
10 * Prepare a release commit including configurable files.
11 *
12 * @param {Object} pluginConfig The plugin configuration.
13 * @param {String|Array<String>} [pluginConfig.assets] Files to include in the release commit. Can be files path or globs.
14 * @param {String} [pluginConfig.message] The message for the release commit.
15 * @param {Object} context semantic-release context.
16 * @param {Object} context.options `semantic-release` configuration.
17 * @param {Object} context.lastRelease The last release.
18 * @param {Object} context.nextRelease The next release.
19 * @param {Object} logger Global logger.
20 */
21module.exports = async (pluginConfig, context) => {
22 const {
23 env,
24 cwd,
25 branch,
26 options: {repositoryUrl},
27 lastRelease,
28 nextRelease,
29 logger,
30 } = context;
31 const {message, assets} = resolveConfig(pluginConfig, logger);
32
33 const modifiedFiles = await getModifiedFiles({env, cwd});
34
35 const filesToCommit = uniq(
36 await pReduce(
37 assets.map(asset => (!isArray(asset) && isPlainObject(asset) ? asset.path : asset)),
38 async (result, asset) => {
39 const glob = castArray(asset);
40 let nonegate;
41 // Skip solo negated pattern (avoid to include every non js file with `!**/*.js`)
42 if (glob.length <= 1 && glob[0].startsWith('!')) {
43 nonegate = true;
44 debug(
45 'skipping the negated glob %o as its alone in its group and would retrieve a large amount of files ',
46 glob[0]
47 );
48 }
49
50 return [
51 ...result,
52 ...micromatch(modifiedFiles, await dirGlob(glob, {cwd}), {dot: true, nonegate, cwd, expand: true}),
53 ];
54 },
55 []
56 )
57 );
58
59 if (filesToCommit.length > 0) {
60 logger.log('Found %d file(s) to commit', filesToCommit.length);
61 await add(filesToCommit, {env, cwd});
62 debug('commited files: %o', filesToCommit);
63 await commit(
64 message
65 ? template(message)({branch: branch.name, lastRelease, nextRelease})
66 : `chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}`,
67 {env, cwd}
68 );
69 await push(repositoryUrl, branch.name, {env, cwd});
70 logger.log('Prepared Git release: %s', nextRelease.gitTag);
71 }
72};