-
Notifications
You must be signed in to change notification settings - Fork 3
/
postversion.mjs
60 lines (46 loc) · 1.92 KB
/
postversion.mjs
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
import fs from "fs";
import shelljs from "shelljs";
import pkgJson from "./package.json" assert { type: "json" };
import templatePkgJson from "./template/package.json" assert { type: "json" };
const versionFilePath = "./template/src/services/version.ts";
fs.copyFileSync(versionFilePath, "./version.mjs");
const { currentVersion } = await import("./version.mjs");
fs.rmSync("./version.mjs");
// Update template version in source code.
shelljs.sed("-i", currentVersion, pkgJson.version, versionFilePath);
// Add new version matrix entry in README.md if needed.
const readme = fs.readFileSync("./README.md", "utf8");
const versionRegex =
/(?<=<!-- start version-matrix -->\s+).*?(?=\s+<!-- end version-matrix -->)/gs;
const match = readme.match(versionRegex);
const versionMatrix = match[0]
.split("\n")
.slice(3)
.map((str) => str.slice(1, -1).replaceAll(" ", "").split("|"));
const reactNativeVersion = templatePkgJson.dependencies["react-native"];
const isAlreadySupported = versionMatrix.some(
([rnVersion]) => rnVersion === reactNativeVersion
);
if (!isAlreadySupported) {
const templateVersionRange = pkgJson.version
.split(".")
.slice(0, 2)
.concat("\\*")
.join(".");
versionMatrix.push([reactNativeVersion, templateVersionRange]);
const markdownHeader = [
"| React Native | Template |",
"| ------------ | -------- |",
];
const markdownRows = versionMatrix
.sort(([a], [b]) => b.localeCompare(a))
.map(([reactNative, template]) => {
const leftEmptySpace = " ".repeat(Math.max(0, 12 - reactNative.length));
const rightEmptySpace = " ".repeat(Math.max(0, 8 - template.length));
return `| ${reactNative}${leftEmptySpace} | ${template}${rightEmptySpace} |`;
});
const markdown = ["", ...markdownHeader, ...markdownRows].join("\n");
const newReadme = readme.replace(versionRegex, markdown);
// Write markdown to README.md
fs.writeFileSync("./README.md", newReadme);
}