forked from gluestack/gluestack-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutiple-release.js
258 lines (246 loc) · 7.11 KB
/
mutiple-release.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* eslint-disable no-unused-vars */
/* eslint-disable no-console */
const readline = require('readline');
const { edit } = require('external-editor');
const { spawnSync, execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ auth: process.env.GITHUB_PERSONAL_TOKEN });
let response;
let releaseBranchName;
const releasePackagesMap = {};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const packageNames = [
'actionsheet',
'alert-dialog',
'alert',
'avatar',
'button',
'checkbox',
'divider',
'fab',
'form-control',
'hooks',
'hstack',
'icon',
'icon-button',
'input',
'link',
'menu',
'modal',
'popover',
'pressable',
'progress',
'provider',
'radio',
'select',
'slider',
'spinner',
'switch',
'tabs',
'textarea',
'toast',
'tooltip',
'utils',
'vstack',
'overlay',
'stack',
'transitions',
'react-native-aria',
];
function createPRforGithub(title) {
octokit.rest.pulls.create({
owner: 'gluestack',
repo: 'gluestack-ui',
title: title,
base: 'main',
head: releaseBranchName,
});
console.log('Created PR!');
}
function releaseNext() {
rl.question('Do you want to release another package? (y/n) ', (answer) => {
if (answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes') {
releasePackage();
} else {
console.log(
"Here's the list of packages to be released:",
releasePackagesMap
);
for (let key in releasePackagesMap) {
runChangesetCommands(
`@gluestack-ui/${key}`,
releasePackagesMap[key].bumpVersion,
releasePackagesMap[key].summary
);
}
pushToGithub();
createPRforGithub(releaseBranchName);
}
});
}
function createBranch(branchName) {
console.log(`Creating Branch ${branchName}`);
execSync(`git checkout -b ${branchName}`);
console.log(`Created branch ${branchName}`);
}
function resolveVersion(currentVersion) {
const versionParts = currentVersion.split('.');
const major = parseInt(versionParts[0], 10);
const minor = parseInt(versionParts[1], 10);
const patch = parseInt(versionParts[2], 10);
return new Promise((resolve, reject) => {
rl.question('Is this a patch version change? (y/n) ', (answer) => {
if (answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes') {
const bumpVersionPatch = {
version: `${major}.${minor}.${patch + 1}`,
type: 'patch',
};
resolve(bumpVersionPatch);
return;
} else {
rl.question('Is this a minor version change? (y/n) ', (answer) => {
if (answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes') {
const bumpVersionMinor = {
version: `${major}.${minor + 1}.0`,
type: 'minor',
};
resolve(bumpVersionMinor);
return;
} else {
rl.question('Is this a major version change? (y/n) ', (answer) => {
if (
answer.toLowerCase() === 'y' ||
answer.toLowerCase() === 'yes'
) {
const bumpVersionMajor = {
version: `${major + 1}.0.0`,
type: 'major',
};
resolve(bumpVersionMajor);
return;
} else {
reject(new Error('Invalid input'));
rl.close();
}
});
}
});
}
});
});
}
function getPackageVersion(packageName) {
const command = `yarn list --depth=0 --pattern ${packageName} | grep ${packageName} | awk -F@ '{print $NF}'`;
const output = execSync(command, { encoding: 'utf-8' });
return output.trim();
}
function createChangesetFile(packageName, versionUpgradeType, summary) {
const changesetDir = path.join(process.cwd(), '.changeset');
if (!fs.existsSync(changesetDir)) {
fs.mkdirSync(changesetDir);
}
const fileName = `changeset-file.md`;
const filePath = path.join(changesetDir, fileName);
const content = `---
'${packageName}': ${versionUpgradeType}
---
${summary}
`;
fs.writeFileSync(filePath, content);
console.log(`Changeset file ${fileName} created at ${changesetDir}`);
}
function pushToGithub() {
console.log('Pushing Branch');
execSync(`git push origin ${releaseBranchName}`);
console.log('Creating PR');
}
function runChangesetCommands(packageName, bumpVersion, summary) {
console.log('Creating Changeset file');
createChangesetFile(packageName, bumpVersion.type, summary);
console.log('Running Changeset version');
const changesetVersionResult = spawnSync('yarn', ['changeset', 'version'], {
stdio: 'inherit',
});
console.log('Commit Changes');
const commitMessage = `chore: ${packageName} version bump to ${bumpVersion.version}`;
if (changesetVersionResult.status === 0) {
const gitAdd = spawnSync('git', ['add', '-A'], {
stdio: 'inherit',
});
if (gitAdd.status === 0) {
const gitCommit = spawnSync('git', ['commit', '-m', `${commitMessage}`], {
stdio: 'inherit',
});
if (gitCommit.status === 0) {
} else {
console.error('Failed to commit changes', gitCommit.stderr.toString());
}
} else {
console.error('Failed to commit changes', gitAdd.stderr.toString());
}
} else {
console.error(
'Failed to run yarn changeset version',
changesetVersionResult.stderr.toString()
);
}
}
const releasePackage = () => {
rl.question('Enter package name: ', (packageName) => {
packageName = packageName.trim();
if (packageName) {
if (packageNames.includes(packageName)) {
const currentVersion = getPackageVersion(
`@gluestack-ui/${packageName}`
);
resolveVersion(currentVersion)
.then((bumpVersion) => {
console.log(bumpVersion, 'bumpVersion');
rl.question('Enter summary of fixes: ', (summary) => {
if (summary === '') {
response = edit(
'\n\n# Please enter a summary for your changes.',
{
postfix: '.md',
}
);
}
console.log(`Package name: ${packageName}`);
console.log(`Summary of fixes: ${summary}`);
releasePackagesMap[packageName] = {
bumpVersion: bumpVersion,
summary: summary,
};
releaseNext();
});
})
.catch((error) => {
console.error(`Error: ${error.message}`);
});
} else {
console.log('Package name not found!');
}
} else {
console.log('Invalid package name format!');
rl.close();
}
});
};
rl.question('Enter Branch name: ', (branchName) => {
branchName = branchName.trim();
if (branchName) {
// create branch
releaseBranchName = branchName;
createBranch(branchName);
releasePackage();
} else {
console.log('Invalid branch name format!');
rl.close();
}
});