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