-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.mjs
153 lines (123 loc) · 3.08 KB
/
util.mjs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// @ts-check
import { execSync } from 'node:child_process';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
/**
* Checks if a file exists
*
* @param {string} filename
* @returns {Promise<boolean>}
*/
export async function exists(filename) {
try {
await fs.stat(filename)
} catch (err) {
if (err.code === 'ENOENT') return false;
throw err;
}
return true;
}
/**
* Returns the version property of the package.json file in the current
* directory.
*
* @returns {Promise<string>}
*/
export async function readPackageVersion() {
if (!await exists('package.json')) {
throw new Error('package.json does not exists in the current directory');
}
const json = JSON.parse(
await fs.readFile(
'package.json',
'utf-8'
)
);
return json.version;
}
/**
* Wraps a line over multiple lines.
*
* @param {string} input
* @param {number} secondLineOffset
* @param {number} lineLength
*/
export function wrap(input, secondLineOffset = 0, lineLength = 79) {
const words = input.split(' ');
const lines = [];
for(const word of words) {
if (!lines.length) {
// First line
lines.push(word);
continue;
}
const maxLength = lines.length > 1 ? lineLength - secondLineOffset : lineLength;
const potentialNewLine = [lines.at(-1),word].join(' ');
if (potentialNewLine.length>maxLength) {
lines.push(word);
} else {
lines[lines.length-1] = potentialNewLine;
}
}
return lines.join('\n' + ' '.repeat(secondLineOffset));
}
/**
* @param {string} prevVersion
* @param {'patch'|'minor'|'major'} changeType
* @returns {string}
*/
export function calculateNextVersion(prevVersion, changeType = 'patch') {
// This function only currently understands 1 format, but this may change
// in the future.
if (!prevVersion.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
throw new Error(`Could not automatically determine the next ${changeType} version from ${prevVersion}. You might want to request a new feature to support this`);
}
const parts = prevVersion.split('.').map( part => +part);
switch(changeType) {
case 'major' :
parts[0]++;
parts[1]=0;
parts[2]=0;
break;
case 'minor' :
parts[1]++;
parts[2]=0;
break;
case 'patch' :
parts[2]++;
break;
}
return parts.join('.');
}
/**
* Returns true if we're in a git-powered directory
*
* @returns {Promise<boolean>}
*/
export async function isGit() {
let currentPath = process.cwd();
while(currentPath!=='/') {
if (await exists(path.join(currentPath,'.git'))) {
return true;
}
currentPath = path.dirname(currentPath);
}
return false;
}
/**
* @param {string} command
* @returns {string}
*/
export function runCommand(command) {
process.stderr.write(command + '\n');
return execSync(command).toString('utf-8');
}
/**
* Returns true if the current working directory is clean.
*
* @returns {boolean}
*/
export function isGitClean() {
const result = execSync('git status --porcelain=v1').toString('utf-8');
return result.trim().length === 0;
}