1 | 'use strict';
|
2 |
|
3 | const _ = require(`lodash`);
|
4 | const Bluebird = require(`bluebird`);
|
5 | const conventionalCommitsDetector = require(`conventional-commits-detector`);
|
6 | const debug = require(`debug`)(`semantic-release-gitlab`);
|
7 | const fs = require(`fs`);
|
8 | const gitlabNotifier = require(`semantic-release-gitlab-notifier`);
|
9 | const gitlabReleaser = require(`semantic-release-gitlab-releaser`);
|
10 | const latestSemverTag = Bluebird.promisify(require(`git-latest-semver-tag`));
|
11 | const rawCommitsStream = require(`git-raw-commits`);
|
12 | const recommendedBump = Bluebird.promisify(require(`conventional-recommended-bump`));
|
13 | const streamToArray = require(`stream-to-array`);
|
14 | const path = require(`path`);
|
15 | const semverIncrement = require(`shifted-semver-increment`);
|
16 | const shell = require(`shelljs`);
|
17 |
|
18 | module.exports = semanticRelease;
|
19 |
|
20 | function semanticRelease(packageOpts) {
|
21 | packageOpts = packageOpts || {};
|
22 | return latestSemverTag()
|
23 | .then(_.partial(debugAndReturn, `last tag`, _))
|
24 | .then(latestTag => streamToArray(rawCommitsStream({from: latestTag})))
|
25 | .then(_.partial(_.map, _, value => value.toString()))
|
26 | .then(_.partial(debugAndReturn, `commit messages - %O`, _))
|
27 | .then(commits => {
|
28 | if (commits.length === 0) {
|
29 | return debug(`no commits to release so skipping the other release steps`);
|
30 | }
|
31 |
|
32 | const config = {
|
33 | data: {
|
34 | commits,
|
35 | },
|
36 | pkg: JSON.parse(fs.readFileSync(path.join(process.cwd(), `package.json`))),
|
37 | options: {
|
38 | scmToken: process.env.GITLAB_AUTH_TOKEN,
|
39 | insecureApi: process.env.GITLAB_INSECURE_API === `true`,
|
40 | preset: packageOpts.preset || conventionalCommitsDetector(commits),
|
41 | },
|
42 | };
|
43 |
|
44 | debug(`detected ${config.options.preset} commit convention`);
|
45 |
|
46 | config.options.preset = config.options.preset === `unknown` ?
|
47 | `angular` : config.options.preset;
|
48 |
|
49 | debug(`using ${config.options.preset} commit convention`);
|
50 |
|
51 | return recommendedBump({ignoreReverted: false, preset: config.options.preset})
|
52 | .then(recommendation => {
|
53 | debug(`recommended version bump is - %O`, recommendation);
|
54 |
|
55 | if (recommendation.releaseType === undefined) {
|
56 | return debug(`no recommended release so skipping the other release steps`);
|
57 | }
|
58 |
|
59 | if (packageOpts.dryrun) {
|
60 | let ltag;
|
61 | return latestSemverTag()
|
62 | .then(_.partial(debugAndReturn, `last tag`, _))
|
63 | .then(latestTag => {latestTag === '' ? `1.0.0` : semverIncrement(latestTag, recommendation.releaseType); ltag = latestTag; return latestTag})
|
64 | .then(_.partial(debugAndReturn, `version to be released`, _))
|
65 | .then(_.partial(_.set, config, `data.version`, _))
|
66 |
|
67 |
|
68 |
|
69 | .then(() => [config.data.version, ltag]);
|
70 | } else {
|
71 | return latestSemverTag()
|
72 | .then(_.partial(debugAndReturn, `last tag`, _))
|
73 | .then(latestTag => latestTag === '' ? `1.0.0` : semverIncrement(latestTag, recommendation.releaseType))
|
74 | .then(_.partial(debugAndReturn, `version to be released`, _))
|
75 | .then(_.partial(_.set, config, `data.version`, _))
|
76 | .then(config => shell.exec(`git tag ${config.data.version}`))
|
77 | .then(_.partial(gitlabReleaser, config))
|
78 | .then(_.partial(gitlabNotifier, config))
|
79 | .then(() => [config.data.version]);
|
80 | }
|
81 | });
|
82 | });
|
83 | }
|
84 |
|
85 | function debugAndReturn(message, value) {
|
86 | debug(message, value);
|
87 | return value;
|
88 | }
|