-
Notifications
You must be signed in to change notification settings - Fork 2
/
new-release-tools.js
227 lines (197 loc) · 6.34 KB
/
new-release-tools.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// @ts-check
const { readFileSync, writeFileSync, existsSync, mkdirSync } = require("fs");
const semver = require("semver");
// TODO Diffs need to ignore pfx files and android/ios dirs
const path = require("path");
const { execSync } = require("child_process");
function runCmd(cmd, cwd) {
console.log("Running: " + cmd);
const opts = cwd ? { cwd: cwd, stdio: "inherit" } : { stdio: "inherit" };
execSync(cmd, opts);
}
/**
* @param {string} [newRelease] - version of windows/macos
* @param {string} [rnVersion] - version of react-native to use with release
* @param {'cpp' | 'cs' | 'mac'} [apptype] - Either language to use for windows, or use mac
*/
function createNewRelease(newRelease, rnVersion, apptype) {
if (apptype !== "cpp" && apptype !== "cs" && apptype !== "mac") {
throw new Error("Must specify cpp or cs app type");
}
const appName = "RnDiffApp";
const appBaseName = "app-base";
const wtAppPath = path.resolve(__dirname, "wt-app");
const appDir = path.resolve(__dirname, "wt-app", appName);
runCmd(`git worktree add wt-app ${appBaseName}`);
runCmd(`cd wt-app`);
// clear any existing stuff
try {
runCmd(`rmdir /S /Q ${appName}`, wtAppPath);
} catch {
// Ignore failures
}
runCmd(`git pull`, wtAppPath);
// make a new branch
const branchName = `release/${apptype}/${newRelease}`;
try {
runCmd(`git branch -D "${branchName}"`, wtAppPath);
} catch {
// Ignore failures
}
runCmd(`git checkout -b "${branchName}"`, wtAppPath);
runCmd(
`npx --yes @react-native-community/cli init "${appName}" --version ${rnVersion} --install-pods false --verbose --skip-git-init true`,
wtAppPath
);
if (apptype === "mac") {
runCmd(
`npx --yes react-native-macos-init --version ${newRelease} --overwrite`,
appDir
);
} else {
if (semver.compare(newRelease, '0.76.0-0') === 1) {
runCmd(
`yarn add react-native-windows@${newRelease}`,
appDir
);
runCmd(
`npx @react-native-community/cli init-windows --template old/uwp-${apptype}-app --overwrite`,
appDir
);
} else {
runCmd(
`npx --yes react-native-windows-init --version ${newRelease} --overwrite --language ${apptype}`,
appDir
);
}
// Modify some files to prevent new guids being generated and showing up in the diffs
runCmd(`node ../standardizeProj.js`, wtAppPath);
}
runCmd(`git add ${appName}/`, wtAppPath);
runCmd(`git commit -m "Release ${apptype}/${newRelease}"`, wtAppPath);
runCmd(
`git push origin --delete "${branchName}" || git push origin "${branchName}"`,
wtAppPath
);
runCmd(`git push --set-upstream origin "${branchName}"`, wtAppPath);
// go back to master
runCmd(`rmdir /S /Q wt-app`);
runCmd(`git worktree prune`);
}
let releasesWindows;
let releasesWindowsIgnored;
let releasesMac;
let releasesMacIgnored;
const releasesFileWindows = path.resolve(__dirname, "RELEASES");
const releasesIgnoredFileWindows = path.resolve(__dirname, "RELEASES_ignored");
const releasesFileMac = path.resolve(__dirname, "RELEASES_MAC");
const releasesIgnoredFileMac = path.resolve(__dirname, "RELEASES_MAC_ignored");
// To save space we do not keep all versions around forever
function getIgnoredReleases(isMac) {
if (!isMac) {
if (!releasesWindowsIgnored) {
releasesWindowsIgnored = readFileSync(releasesIgnoredFileWindows)
.toString()
.split("\n")
.map((_) => _.trim());
}
return releasesWindowsIgnored;
} else {
if (!releasesMacIgnored) {
releasesMacIgnored = readFileSync(releasesIgnoredFileMac)
.toString()
.split("\n")
.map((_) => _.trim());
}
return releasesMacIgnored;
}
}
/**
* @param {boolean} [isMac] - get Mac releases instead of windows
*/
function getReleases(isMac) {
if (!isMac) {
if (!releasesWindows) {
releasesWindows = readFileSync(releasesFileWindows)
.toString()
.split("\n")
.map((_) => _.trim());
}
return releasesWindows;
} else {
if (!releasesMac) {
releasesMac = readFileSync(releasesFileMac)
.toString()
.split("\n")
.map((_) => _.trim());
}
return releasesMac;
}
}
/**
* @param {string} [rnwVersion]
* @param {boolean} [mac] - mac vs windows
*/
function addReleaseToList(rnwVersion, mac) {
getReleases(mac).push(rnwVersion);
if (mac) {
releasesMac = releasesMac
.filter((a) => a)
.sort((a, b) => 0 - semver.compare(a, b));
writeFileSync(releasesFileMac, releasesMac.join("\n"));
} else {
releasesWindows = releasesWindows
.filter((a) => a)
.sort((a, b) => 0 - semver.compare(a, b));
writeFileSync(releasesFileWindows, releasesWindows.join("\n"));
}
}
/**
* @param {string} [rnwVersion]
* @param {boolean} [mac] - mac vs windows
*/
function versionAlreadyExists(rnwVersion, mac) {
return getReleases(mac).includes(rnwVersion) || getIgnoredReleases(mac).includes(rnwVersion);
}
/**
* @param {string} [rnwVersion]
* @param {'cs' | 'cpp' | 'mac'} [apptype]
*/
function generateDiffs(rnwVersion, apptype) {
const wtDiffsDir = path.resolve(__dirname, "wt-diffs");
if (!existsSync("wt-diffs")) {
runCmd("git worktree add wt-diffs diffs");
}
runCmd("git fetch origin diffs", wtDiffsDir);
runCmd("git checkout diffs --", wtDiffsDir);
runCmd("git pull", wtDiffsDir);
if (!existsSync(path.resolve(wtDiffsDir, `diffs/${apptype}`))) {
mkdirSync(path.resolve(wtDiffsDir, `diffs/${apptype}`), {
recursive: true,
});
}
const isMac = apptype === "mac";
for (let existingRelease of getReleases(isMac)) {
console.log("processing " + existingRelease);
if (existingRelease === rnwVersion) continue;
runCmd(
`git diff --binary origin/release/${apptype}/"${existingRelease}"..origin/release/${apptype}/"${rnwVersion}" > wt-diffs/diffs/${apptype}/"${existingRelease}".."${rnwVersion}".diff`
);
runCmd(
`git diff --binary origin/release/${apptype}/"${rnwVersion}"..origin/release/${apptype}/"${existingRelease}" > wt-diffs/diffs/${apptype}/"${rnwVersion}".."${existingRelease}".diff`
);
}
runCmd("git add .", wtDiffsDir);
runCmd(
`git commit -m "Add release ${rnwVersion} ${apptype} diffs"`,
wtDiffsDir
);
runCmd("git push", wtDiffsDir);
}
module.exports = {
addReleaseToList,
createNewRelease,
generateDiffs,
versionAlreadyExists,
getReleases,
};