forked from sindresorhus/np
/
util.js
101 lines (80 loc) · 2.35 KB
/
util.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
const readPkgUp = require('read-pkg-up');
const issueRegex = require('issue-regex');
const terminalLink = require('terminal-link');
const execa = require('execa');
const pMemoize = require('p-memoize');
const ow = require('ow');
const pkgDir = require('pkg-dir');
const gitUtil = require('./git-util');
const npmUtil = require('./npm/util');
exports.readPkg = packagePath => {
packagePath = packagePath ? pkgDir.sync(packagePath) : pkgDir.sync();
if (!packagePath) {
throw new Error('No `package.json` found. Make sure the current directory is a valid package.');
}
const {packageJson} = readPkgUp.sync({
cwd: packagePath
});
return packageJson;
};
exports.linkifyIssues = (url, message) => {
if (!(url && terminalLink.isSupported)) {
return message;
}
return message.replace(issueRegex(), issue => {
const issuePart = issue.replace('#', '/issues/');
if (issue.startsWith('#')) {
return terminalLink(issue, `${url}${issuePart}`);
}
return terminalLink(issue, `https://github.com/${issuePart}`);
});
};
exports.linkifyCommit = (url, commit) => {
if (!(url && terminalLink.isSupported)) {
return commit;
}
return terminalLink(commit, `${url}/commit/${commit}`);
};
exports.linkifyCommitRange = (url, commitRange) => {
if (!(url && terminalLink.isSupported)) {
return commitRange;
}
return terminalLink(commitRange, `${url}/compare/${commitRange}`);
};
exports.getTagVersionPrefix = pMemoize(async options => {
ow(options, ow.object.hasKeys('yarn'));
try {
if (options.yarn) {
const {stdout} = await execa('yarn', ['config', 'get', 'version-tag-prefix']);
return stdout;
}
const {stdout} = await execa('npm', ['config', 'get', 'tag-version-prefix']);
return stdout;
} catch (_) {
return 'v';
}
});
exports.getNewAndUnpublishedFiles = async pkg => {
const listNewFiles = await gitUtil.newFilesSinceLastRelease();
return npmUtil.getNewAndUnpublishedFiles(pkg, listNewFiles);
};
exports.getPreReleasePrefix = pMemoize(async options => {
ow(options, ow.object.hasKeys('yarn'));
try {
if (options.yarn) {
const {stdout} = await execa('yarn', ['config', 'get', 'preId']);
if (stdout !== 'undefined') {
return stdout;
}
return '';
}
const {stdout} = await execa('npm', ['config', 'get', 'preId']);
if (stdout !== 'undefined') {
return stdout;
}
return '';
} catch (_) {
return '';
}
});