-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
210 lines (186 loc) · 6.31 KB
/
index.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { readFileSync, writeFileSync } from 'fs';
import { EOL } from 'os';
import glob from 'fast-glob';
import { castArray, get, set } from 'lodash-es';
import detectIndent from 'detect-indent';
import yaml from 'js-yaml';
import toml from '@iarna/toml';
import ini from 'ini';
import semver from 'semver';
import { Plugin } from 'release-it';
import * as cheerio from 'cheerio';
const noop = Promise.resolve();
const isString = value => typeof value === 'string';
const mimeTypesMap = {
'application/json': 'json',
'application/yaml': 'yaml',
'application/x-yaml': 'yaml',
'text/yaml': 'yaml',
'application/toml': 'toml',
'text/toml': 'toml',
'text/x-properties': 'ini',
'application/xml': 'xml',
'text/xml': 'xml',
'application/xhtml+xml': 'html',
'text/html': 'html',
'text/plain': 'text'
};
const extensionsMap = {
json: 'json',
yml: 'yaml',
yaml: 'yaml',
toml: 'toml',
ini: 'ini',
xml: 'xml',
html: 'html',
xhtml: 'html',
txt: 'text'
};
const parseFileOption = option => {
const file = isString(option) ? option : option.file;
const mimeType = typeof option !== 'string' ? option.type : null;
const path = (typeof option !== 'string' && option.path) || 'version';
const consumeWholeFile = typeof option !== 'string' ? option.consumeWholeFile : false;
const versionPrefix = typeof option !== 'string' ? option.versionPrefix : null;
return { file, mimeType, path, consumeWholeFile, versionPrefix };
};
const getFileType = (file, mimeType) => {
if (mimeType) return mimeTypesMap[mimeType] || 'text';
const ext = file.split('.').pop();
return extensionsMap[ext] || 'text';
};
const parse = async (data, type) => {
switch (type) {
case 'json':
return JSON.parse(data);
case 'yaml':
return yaml.load(data);
case 'toml':
return toml.parse(data);
case 'ini':
return ini.parse(data);
case 'xml':
return cheerio.load(data, { xmlMode: true });
case 'html':
return cheerio.load(data);
default: // text
return (data || '').toString();
}
};
class Bumper extends Plugin {
async getLatestVersion() {
const { in: option } = this.options;
if (!option) return;
const { file, mimeType, path, consumeWholeFile } = parseFileOption(option);
if (file) {
const type = getFileType(file, mimeType);
let data;
try {
data = readFileSync(file, 'utf8');
} catch (error) {
data = '{}';
}
const parsed = await parse(data, type);
let version = undefined;
switch (type) {
case 'json':
case 'yaml':
case 'toml':
case 'ini':
version = get(parsed, path);
break;
case 'xml':
case 'html':
const element = parsed(path);
if (!element) {
throw new Error(`Failed to find the element with the provided selector: ${path}`);
}
version = element.text();
break;
default: // text
version = parsed.trim();
}
const parsedVersion = semver.parse(version);
return parsedVersion ? parsedVersion.toString() : null;
}
return null;
}
async bump(version) {
const { out } = this.options;
const { isDryRun } = this.config;
const { latestVersion } = this.config.getContext();
if (!out) return;
const expandedOptions = castArray(out).map(options => (isString(options) ? { file: options } : options));
const options = [];
for (const option of expandedOptions) {
if (glob.isDynamicPattern(option.file)) {
const files = await glob(option.file, {
onlyFiles: true,
unique: true
});
options.push(
...files.map(file => ({
...option,
file
}))
);
} else {
options.push(option);
}
}
return Promise.all(
options.map(async out => {
const { file, mimeType, path, consumeWholeFile, versionPrefix = '' } = parseFileOption(out);
this.log.exec(`Writing version to ${file}`, isDryRun);
if (isDryRun) return noop;
const type = getFileType(file, mimeType);
let data;
try {
data = readFileSync(file, 'utf8');
} catch (error) {
data = type === 'text' ? latestVersion : '{}';
}
const parsed = await parse(data, type);
const indent = isString(data) ? detectIndent(data).indent || ' ' : null;
if (typeof parsed !== 'string') {
castArray(path).forEach(path => set(parsed, path, versionPrefix + version));
}
switch (type) {
case 'json':
return writeFileSync(file, JSON.stringify(parsed, null, indent) + '\n');
case 'yaml':
return writeFileSync(file, yaml.dump(parsed, { indent: indent.length }));
case 'toml':
const tomlString = toml.stringify(parsed);
// handle empty objects for sections
const tomlContent = tomlString.replace(/^([a-zA-Z0-9\.\-]+) \= \{ \}/g, '[$1]');
return writeFileSync(file, tomlContent);
case 'ini':
return writeFileSync(file, ini.encode(parsed));
case 'xml':
const xmlElement = parsed(path);
if (!xmlElement) {
throw new Error(`Failed to find the element with the provided selector: ${path}`);
}
xmlElement.text(version);
return writeFileSync(file, parsed.xml());
case 'html':
const htmlElement = parsed(path);
if (!htmlElement) {
throw new Error(`Failed to find the element with the provided selector: ${path}`);
}
// We are taking this approach as cheerio will modify the original html doc type and head formatting if we just used the parsed.html() function.
const previousContents = htmlElement.prop('outerHTML');
htmlElement.text(version);
const html = data.replace(previousContents.trim(), htmlElement.prop('outerHTML').trim());
return writeFileSync(file, html);
default:
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = parsed && !consumeWholeFile ? parsed.replace(versionMatch, version) : version + EOL;
return writeFileSync(file, write);
}
})
);
}
}
export default Bumper;