forked from gradio-app/gradio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs.js
More file actions
228 lines (200 loc) · 5.69 KB
/
cs.js
File metadata and controls
228 lines (200 loc) · 5.69 KB
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
import { readFileSync, writeFileSync, readdirSync } from "fs";
import path from "path";
import { execSync } from "child_process";
import { getPackages } from "@manypkg/get-packages";
const changsetsFolder = path.join(process.cwd(), ".changeset");
const files = readdirSync(changsetsFolder);
const mdFiles = files.filter(
(file) => file.endsWith(".md") && !file.startsWith("README.md")
);
const getGitInfo = (filePath) => {
const gitInfo = execSync(
`git log -n 1 --pretty=format:"%H -bingboong- %s" -- ${filePath}`,
{
encoding: "utf8"
}
);
return gitInfo;
};
const changsets = mdFiles
.map((file) => {
const filePath = path.join(changsetsFolder, file);
const fileContent = readFileSync(filePath, "utf8");
const [sha, message] = getGitInfo(
path.join(process.cwd(), ".changeset", file)
).split(" -bingboong- ");
return {
file,
sha,
short_sha: sha.slice(0, 7),
pr: message.match(/(#\d+)/)?.[1],
content: fileContent
};
})
.filter((c) => c.pr);
const changedPackages = changsets.reduce(
(acc, { content, sha, short_sha, pr }) => {
const [, frontmatter, body] = content.split("---");
const type_index = body.indexOf(":");
const type = body.slice(0, type_index).trim();
const _content = body.slice(type_index + 1).trim();
if (frontmatter) {
const packages = frontmatter.split("\n").filter((line) => !!line.trim());
if (packages) {
packages.forEach((_package) => {
const [name, version] = _package.split(":");
if (!acc[name]) {
acc[name] = {
version: version.trim(),
reamde_content: {
feat: "",
fix: "",
highlight: "",
[type]: format_readme_content(pr, short_sha, sha, _content)
}
};
} else {
acc[name].version = getMaximumBump(
version.trim(),
acc[name].version
);
acc[name].reamde_content[type] += `\n${format_readme_content(
pr,
short_sha,
sha,
_content
)}`;
}
});
}
}
return acc;
},
{}
);
function getMaximumBump(newVersion, oldVersion) {
const versionOrder = ["patch", "minor", "major"];
const newVersionIndex = versionOrder.indexOf(newVersion);
const oldVersionIndex = versionOrder.indexOf(oldVersion);
return versionOrder[Math.max(newVersionIndex, oldVersionIndex)];
}
function format_readme_content(pr, short_sha, sha, _content) {
return `- [${pr}](https://github.com/gradio-app/gradio/pull/${pr.replace(
"#",
""
)}) [\`${short_sha}\`](https://github.com/gradio-app/gradio/commit/${sha}) - ${_content.trim()}`;
}
const { packages } = await getPackages(process.cwd());
const packages_to_write = [];
for (const pkg of packages) {
if (`"${pkg.packageJson.name}"` in changedPackages) {
const current_version = pkg.packageJson.version;
const bump = changedPackages[`"${pkg.packageJson.name}"`].version;
const new_version = get_new_version(current_version, bump);
const new_package_json = {
...pkg.packageJson,
version: new_version
};
let deps_updated = [];
for (const dep in new_package_json.dependencies) {
if (`"${dep}"` in changedPackages) {
console.log(
dep,
changedPackages[`"${dep}"`].version,
new_package_json.dependencies[dep]
);
const dep_version = packages.find((p) => p.packageJson.name === dep)
?.packageJson.version;
deps_updated.push([
dep,
get_new_version(dep_version, changedPackages[`"${dep}"`].version)
]);
}
}
if (
deps_updated.length > 0 &&
!(`"${pkg.packageJson.name}"` in changedPackages)
) {
new_package_json.version = get_new_version(current_version, "patch");
packages_to_write.push({
name: pkg.packageJson.name,
dir: pkg.dir,
version: new_version,
packge_json: new_package_json,
changelog: readFileSync(`${pkg.dir}/CHANGELOG.md`, "utf8").replace(
`# ${pkg.packageJson.name}`,
make_changelog(
pkg.packageJson.name,
new_version,
changedPackages[`"${pkg.packageJson.name}"`].reamde_content,
deps_updated
)
)
});
} else if (`"${pkg.packageJson.name}"` in changedPackages) {
new_package_json.version = new_version;
packages_to_write.push({
name: pkg.packageJson.name,
dir: pkg.dir,
version: new_version,
packge_json: new_package_json,
changelog: readFileSync(`${pkg.dir}/CHANGELOG.md`, "utf8").replace(
`# ${pkg.packageJson.name}`,
make_changelog(
pkg.packageJson.name,
new_version,
changedPackages[`"${pkg.packageJson.name}"`].reamde_content,
deps_updated
)
)
});
}
}
}
function make_changelog(name, version, changes, deps_updated) {
const { feat, fix, highlight } = changes;
let changelog = `# ${name}
## ${version}`;
if (highlight) {
changelog += `\n\n### Highlights\n\n${highlight}`;
}
if (feat) {
changelog += `\n\n### Features\n\n${feat}`;
}
if (fix) {
changelog += `\n\n### Fixes\n\n${fix}`;
}
if (deps_updated.length > 0) {
changelog += `\n\n### Dependencies\n\n${deps_updated
.map(([dep, version]) => `- ${dep}@${version}`)
.join("\n")}`;
}
return changelog;
}
function get_new_version(version, bump) {
const [_major, _minor, _patch, prerelease] = version.split(".");
const major = parseInt(_major);
const minor = parseInt(_minor);
const patch = parseInt(_patch);
if (prerelease) {
return `${major}.${minor}.${patch}`;
}
switch (bump) {
case "major":
return `${major + 1}.0.0`;
case "minor":
return `${major}.${minor + 1}.0`;
case "patch":
return `${major}.${minor}.${patch + 1}`;
default:
return version;
}
}
console.log(packages_to_write);
for (const pkg of packages_to_write) {
writeFileSync(
`${pkg.dir}/package.json`,
JSON.stringify(pkg.packge_json, null, "\t")
);
writeFileSync(`${pkg.dir}/CHANGELOG.md`, pkg.changelog);
}