-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
153 lines (133 loc) · 4.4 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
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';
const noop = Promise.resolve();
const isString = value => typeof value === 'string';
const mimeTypesMap = {
'application/json': 'json',
'text/yaml': 'yaml',
'application/x-yaml': 'yaml',
'text/toml': 'toml',
'application/toml': 'toml',
'text/x-properties': 'ini'
};
const extensionsMap = {
json: 'json',
yml: 'yaml',
yaml: 'yaml',
toml: 'toml',
ini: 'ini'
};
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);
default:
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);
const version = isString(parsed) ? parsed.trim() : get(parsed, path);
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':
return writeFileSync(file, toml.stringify(parsed));
case 'ini':
return writeFileSync(file, ini.encode(parsed));
default:
const versionMatch = new RegExp(latestVersion || '', 'g');
const write = (parsed && !consumeWholeFile) ? parsed.replace(versionMatch, version) : version + EOL;
return writeFileSync(file, write);
}
})
);
}
}
export default Bumper;