-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (54 loc) · 1.48 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
const { create } = require('xmlbuilder2');
/**
* @callback VersionReader
* @param {import('xmlbuilder2/lib/interfaces').XMLBuilderCreateOptions} contents
* @returns {string}
*/
/**
* @callback VersionWriter
* @param {import('xmlbuilder2/lib/interfaces').XMLBuilderCreateOptions} contents
* @param {string} version
* @returns {string}
*/
/**
* @typedef {Object} Updater
* @property {VersionReader} readVersion
* @property {VersionWriter} writeVersion
*/
const isVersion = key => key === 'version' || key === 'Version';
/**
* @type {VersionReader}
*/
const readVersion = contents => {
const doc = create(contents);
const versionNode = doc.find(n => isVersion(n.node.nodeName), true, true);
return versionNode && versionNode.node.textContent;
};
/**
* @param {import('xmlbuilder2/lib/interfaces').XMLWriterOptions} writerOptions
* @returns {VersionWriter}
*/
const writeVersion = writerOptions => (contents, version) => {
const doc = create(contents);
doc.find(
n => {
if (isVersion(n.node.nodeName)) {
n.node.textContent = version;
return true;
}
},
true,
true
);
return doc.end(writerOptions || { prettyPrint: true });
};
/**
* @type {Updater & (writerOptions: import('xmlbuilder2/lib/interfaces').XMLWriterOptions) => Updater}
*/
const updater = writerOptions => ({
readVersion,
writeVersion: writeVersion(writerOptions),
});
updater.readVersion = readVersion;
updater.writeVersion = writeVersion();
module.exports = updater;