-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathupdateAPIVersion.js
executable file
·60 lines (52 loc) · 1.61 KB
/
updateAPIVersion.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
#!/usr/bin/env node
/**
* Reads the current API version from src/apiVersion.ts and updates all
* references to it in the types/ directory.
*/
/* eslint-disable no-sync,no-nested-ternary */
const fs = require('fs');
const path = require('path');
const read = (file) => fs.readFileSync(path.resolve(file)).toString();
const write = (file, str) => fs.writeFileSync(path.resolve(file), str);
const edit = (file, cb) => write(file, cb(read(file)));
const API_VERSION = '2[0-9][2-9][0-9]-[0-9]{2}-[0-9]{2}.[a-z]+';
const main = () => {
const matches = [
...read('src/apiVersion.ts').matchAll(/ApiVersion = '([^']*)'/g),
];
if (matches.length !== 1) {
throw new Error(
`Expected src/apiVersion.ts to include 1 match for ApiVersion = '...' but found ${matches.length}`
);
}
const apiVersion = matches[0][1];
const replaceAPIVersion = (file, pattern) =>
edit(file, (text) => {
const parts = pattern.split('API_VERSION');
return text.replace(
new RegExp(parts.map((x) => `(${x})`).join(API_VERSION), 'g'),
parts.length === 0
? apiVersion
: parts.length === 1
? `$1${apiVersion}`
: parts.length === 2
? `$1${apiVersion}$2`
: 'UNEXPECTED'
);
});
replaceAPIVersion(
'types/lib.d.ts',
'export type LatestApiVersion = [\'"]API_VERSION[\'"]'
);
replaceAPIVersion(
'types/test/typescriptTest.ts',
'///<reference types=["\']\\.\\./API_VERSION[\'"]'
);
replaceAPIVersion(
'types/test/typescriptTest.ts',
'apiVersion: [\'"]API_VERSION[\'"]'
);
};
if (require.main === module) {
main();
}