forked from ts-essentials/ts-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-ts-version.js
41 lines (37 loc) · 1.08 KB
/
sync-ts-version.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
// @ts-check
const fs = require("fs");
const FIRST_TYPESCRIPT_LINE = /typescript\@\^?(\d+.\d+(.\d+)?):/;
const TYPESCRIPT_VERSION_LINE = /version "(\d+.\d+)(.\d+)?"/;
/**
* Finds TypeScript version in a filename
*
* @param {string} filename
* @returns {string | undefined}
*/
const findTsVersion = (filename) => {
try {
const data = fs.readFileSync(filename, "utf8");
const lines = data.split("\n").map((line) => line.trim());
const typescriptLineIndex = lines.findIndex((line) => FIRST_TYPESCRIPT_LINE.test(line));
const matched = lines[typescriptLineIndex + 1].match(TYPESCRIPT_VERSION_LINE);
if (matched !== null) {
return matched[1];
}
return undefined;
} catch {
return undefined;
}
};
/**
*
*
* @param {string} tsVersion
*/
const writeTsVersion = (tsVersion, filename) => {
fs.writeFileSync(filename, `export type TsVersion = "${tsVersion}";\n`);
};
const tsVersion = findTsVersion("yarn.lock");
if (tsVersion === undefined) {
throw new Error("Cannot find TypeScript version in yarn.lock");
}
writeTsVersion(tsVersion, "test/ts-version.ts");