1 | 'use strict';
|
2 | const readPkgUp = require('read-pkg-up');
|
3 | const issueRegex = require('issue-regex');
|
4 | const terminalLink = require('terminal-link');
|
5 | const execa = require('execa');
|
6 | const pMemoize = require('p-memoize');
|
7 | const ow = require('ow');
|
8 |
|
9 | exports.readPkg = () => {
|
10 | const {package: pkg} = readPkgUp.sync();
|
11 |
|
12 | if (!pkg) {
|
13 | throw new Error('No package.json found. Make sure you\'re in the correct project.');
|
14 | }
|
15 |
|
16 | return pkg;
|
17 | };
|
18 |
|
19 | exports.linkifyIssues = (url, message) => {
|
20 | if (!(url && terminalLink.isSupported)) {
|
21 | return message;
|
22 | }
|
23 |
|
24 | return message.replace(issueRegex(), issue => {
|
25 | const issuePart = issue.replace('#', '/issues/');
|
26 |
|
27 | if (issue.startsWith('#')) {
|
28 | return terminalLink(issue, `${url}${issuePart}`);
|
29 | }
|
30 |
|
31 | return terminalLink(issue, `https://github.com/${issuePart}`);
|
32 | });
|
33 | };
|
34 |
|
35 | exports.linkifyCommit = (url, commit) => {
|
36 | if (!(url && terminalLink.isSupported)) {
|
37 | return commit;
|
38 | }
|
39 |
|
40 | return terminalLink(commit, `${url}/commit/${commit}`);
|
41 | };
|
42 |
|
43 | exports.linkifyCommitRange = (url, commitRange) => {
|
44 | if (!(url && terminalLink.isSupported)) {
|
45 | return commitRange;
|
46 | }
|
47 |
|
48 | return terminalLink(commitRange, `${url}/compare/${commitRange}`);
|
49 | };
|
50 |
|
51 | exports.getTagVersionPrefix = pMemoize(async options => {
|
52 | ow(options, ow.object.hasKeys('yarn'));
|
53 |
|
54 | try {
|
55 | if (options.yarn) {
|
56 | const {stdout} = await execa('yarn', ['config', 'get', 'version-tag-prefix']);
|
57 | return stdout;
|
58 | }
|
59 |
|
60 | const {stdout} = await execa('npm', ['config', 'get', 'tag-version-prefix']);
|
61 | return stdout;
|
62 | } catch (_) {
|
63 | return 'v';
|
64 | }
|
65 | });
|