1 | const {isPlainObject, isArray, template, castArray, uniq} = require('lodash');
|
2 | const micromatch = require('micromatch');
|
3 | const dirGlob = require('dir-glob');
|
4 | const pReduce = require('p-reduce');
|
5 | const debug = require('debug')('semantic-release:git');
|
6 | const resolveConfig = require('./resolve-config');
|
7 | const {getModifiedFiles, add, commit, push} = require('./git');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | module.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 |
|
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 | };
|