/
util.js
105 lines (91 loc) 路 3.04 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
102
103
104
105
import fs from 'node:fs';
import { EOL } from 'node:os';
import _ from 'lodash';
import gitUrlParse from 'git-url-parse';
import semver from 'semver';
import osName from 'os-name';
import Log from './log.js';
const readJSON = file => JSON.parse(fs.readFileSync(file, 'utf8'));
const pkg = readJSON(new URL('../package.json', import.meta.url));
const log = new Log();
const getSystemInfo = () => {
return {
'release-it': pkg.version,
node: process.version,
os: osName()
};
};
const format = (template = '', context = {}) => {
try {
return _.template(template)(context);
} catch (error) {
log.error(`Unable to render template with context:\n${template}\n${JSON.stringify(context)}`);
log.error(error);
throw error;
}
};
const truncateLines = (input, maxLines = 10, surplusText = null) => {
const lines = input.split(EOL);
const surplus = lines.length - maxLines;
const output = lines.slice(0, maxLines).join(EOL);
return surplus > 0 ? (surplusText ? `${output}${surplusText}` : `${output}${EOL}...and ${surplus} more`) : output;
};
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const rejectAfter = (ms, error) =>
wait(ms).then(() => {
throw error;
});
const parseGitUrl = remoteUrl => {
if (!remoteUrl) return { host: null, owner: null, project: null, protocol: null, remote: null, repository: null };
const normalizedUrl = (remoteUrl || '')
.replace(/^[A-Z]:\\\\/, 'file://') // Assume file protocol for Windows drive letters
.replace(/^\//, 'file://') // Assume file protocol if only /path is given
.replace(/\\+/g, '/'); // Replace forward with backslashes
const parsedUrl = gitUrlParse(normalizedUrl);
const { resource: host, name: project, protocol, href: remote } = parsedUrl;
const owner = protocol === 'file' ? _.last(parsedUrl.owner.split('/')) : parsedUrl.owner; // Fix owner for file protocol
const repository = `${owner}/${project}`;
return { host, owner, project, protocol, remote, repository };
};
const reduceUntil = async (collection, fn) => {
let result;
for (const item of collection) {
if (result) break;
result = await fn(item);
}
return result;
};
const hasAccess = path => {
try {
fs.accessSync(path);
return true;
} catch (err) {
return false;
}
};
const parseVersion = raw => {
if (raw == null) return { version: raw, isPreRelease: false, preReleaseId: null };
const version = semver.valid(raw) ? raw : semver.coerce(raw);
if (!version) return { version: raw, isPreRelease: false, preReleaseId: null };
const parsed = semver.parse(version);
const isPreRelease = parsed.prerelease.length > 0;
const preReleaseId = isPreRelease && isNaN(parsed.prerelease[0]) ? parsed.prerelease[0] : null;
return {
version: version.toString(),
isPreRelease,
preReleaseId
};
};
const e = (message, docs) => new Error(docs ? `${message}${EOL}Documentation: ${docs}${EOL}` : message);
export {
getSystemInfo,
format,
truncateLines,
rejectAfter,
reduceUntil,
parseGitUrl,
hasAccess,
parseVersion,
readJSON,
e
};