-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathupdateVersion.js
139 lines (128 loc) · 5.22 KB
/
updateVersion.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
/* eslint-disable no-console */
const exec = require("child_process").exec;
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const generateChangelog = require("./generateChangelog");
const branchName = process.argv[2];
const dryRun = process.argv[3];
const config = require(path.resolve("./.build/config.json"));
const baseDirectory = path.resolve(".");
async function runCommand(command) {
return new Promise((resolve, reject) => {
console.log(command);
exec(command, function (error, stdout, stderr) {
if (error || typeof stderr !== "string") {
console.log(error);
return reject(error || stderr);
}
console.log(stderr || stdout);
return resolve(stderr || stdout);
});
});
}
const getNewVersion = () => {
// get @dev/core package.json
const rawdata = fs.readFileSync(path.join(baseDirectory, "packages", "public", "umd", "babylonjs", "package.json"), "utf-8");
const packageJson = JSON.parse(rawdata);
const version = packageJson.version;
return version;
};
const updateEngineVersion = async (version) => {
// get thinEngine.ts
const abstractEngineFile = path.join(baseDirectory, "packages", "dev", "core", "src", "Engines", "abstractEngine.ts");
const abstractEngineData = fs.readFileSync(abstractEngineFile, "utf-8");
const array = /"babylonjs@(.*)"/.exec(abstractEngineData);
if (!array) {
throw new Error("Could not find babylonjs version in abstractEngine.ts");
}
const regexp = new RegExp(array[1] + '"', "g");
const newAbstractEngineData = abstractEngineData.replace(regexp, version + '"');
fs.writeFileSync(abstractEngineFile, newAbstractEngineData);
};
const updateSinceTag = (version) => {
// get all typescript files in the dev folder
const files = glob.globSync(path.join(baseDirectory, "packages", "dev", "**", "*.ts"));
files.forEach((file) => {
try {
// check if file contains @since\n
const data = fs.readFileSync(file, "utf-8").replace(/\r/gm, "");
if (data.indexOf("* @since\n") !== -1) {
console.log(`Updating @since tag in ${file} to ${version}`);
// replace @since with @since version
const newData = data.replace(
/\* @since\n/gm,
`* @since ${version}
`
);
// write file
fs.writeFileSync(file, newData);
}
} catch (e) {
console.log(e);
}
});
// run formatter to make sure the package.json files are formatted
runCommand("npx prettier --write packages/public/**/package.json");
};
const updatePeerDependencies = async (version) => {
// get all package.json files in the dev folder
const files = glob.globSync(path.join(baseDirectory, "packages", "public", "**", "package.json"));
files.forEach((file) => {
try {
// check if file contains @since\n
const data = fs.readFileSync(file, "utf-8").replace(/\r/gm, "");
const packageJson = JSON.parse(data);
// check each peer dependency, if it is babylon, update it with the new version
let changed = false;
if (packageJson.peerDependencies) {
Object.keys(packageJson.peerDependencies).forEach((dependency) => {
if (dependency.startsWith("babylonjs") || dependency.startsWith("@babylonjs")) {
packageJson.peerDependencies[dependency] = version;
changed = true;
}
});
}
if (changed) {
console.log(`Updating Babylon peerDependencies in ${file} to ${version}`);
// write file
fs.writeFileSync(file, JSON.stringify(packageJson, null, 4));
}
} catch (e) {
console.log(e);
}
});
};
async function runTagsUpdate() {
await runCommand(
`npx lerna version ${config.versionDefinition} --yes --no-push --conventional-prerelease --force-publish --no-private --no-git-tag-version ${
config.preid ? "--preid " + config.preid : ""
}`
);
// update package-json
const version = getNewVersion();
// // update engine version
await updateEngineVersion(version);
// generate changelog
await generateChangelog(version);
// update since tags
updateSinceTag(version);
// if major, update peer dependencies
if (config.versionDefinition === "major") {
await updatePeerDependencies(`^${version}`);
}
if (dryRun) {
console.log("skipping", `git commit -m "Version update ${version}"`);
console.log("skipping", `git tag -a ${version} -m ${version}`);
} else {
await runCommand("git add .");
await runCommand(`git commit -m "Version update ${version}"`);
await runCommand(`git tag -a ${version} -m ${version}`);
}
}
if (!branchName) {
console.log("Please provide a branch name");
process.exit(1);
} else {
runTagsUpdate();
}