forked from sindresorhus/np
/
util.js
80 lines (63 loc) · 2 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
'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 gitUtil = require('./git-util');
const npmUtil = require('./npm/util');
exports.readPkg = () => {
const {packageJson} = readPkgUp.sync();
if (!packageJson) {
throw new Error('No package.json found. Make sure you\'re in the correct project.');
}
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';
}
});
function splitFileNameList(stdout) {
if (typeof stdout !== 'string') {
throw new TypeError('expected parameter stdout of type string');
}
return stdout.trim().split('\n').map(row => row.replace(/[|+-]/g, '').trim());
}
exports.getNewFilesIgnoredByNpm = async pkg => {
const ListNewFiles = splitFileNameList(await gitUtil.newFilesSinceLastRelease());
return npmUtil.checkNewFiles(ListNewFiles, pkg.files);
};