mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
chore(CI): fixed release info script to use npm registry instead of git as file history source; (#7006)
This commit is contained in:
@@ -1,42 +1,134 @@
|
||||
import Handlebars from "handlebars";
|
||||
import fs from "fs/promises";
|
||||
import prettyBytes from 'pretty-bytes';
|
||||
import {gzipSize} from 'gzip-size';
|
||||
import {getBlobHistory} from './repo.js';
|
||||
import pacote from "pacote";
|
||||
import zlib from "zlib";
|
||||
import tar from "tar-stream";
|
||||
import { Readable } from "stream";
|
||||
|
||||
const generateFileReport = async (files) => {
|
||||
const stat = {};
|
||||
const FILE_SIZE_DIFF_THRESHOLD = 512; // 0.5KB
|
||||
|
||||
for(const [name, file] of Object.entries(files)) {
|
||||
const commits = await getBlobHistory(file);
|
||||
const readJSONFile = async (file) => JSON.parse(String(await fs.readFile(file)));
|
||||
|
||||
stat[file] = {
|
||||
const {version} = await readJSONFile('./package.json');
|
||||
|
||||
const parseVersion = (tag) => {
|
||||
const [, major, minor, patch] = /^v?(\d+)\.(\d+)\.(\d+)/.exec(tag) || [];
|
||||
return [major, minor, patch];
|
||||
}
|
||||
|
||||
const [MAJOR_NUMBER] = parseVersion(version);
|
||||
|
||||
async function getFilesFromNPM(pkg) {
|
||||
const tgzData = await pacote.tarball(pkg); // Buffer з npm
|
||||
const files = {};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const extract = tar.extract();
|
||||
|
||||
extract.on("entry", (header, stream, next) => {
|
||||
const buffers = [];
|
||||
|
||||
stream.on('data', (buffer) => {
|
||||
buffers.push(buffer);
|
||||
});
|
||||
|
||||
stream.on("end", () => {
|
||||
const content = Buffer.concat(buffers);
|
||||
|
||||
const gzipped = zlib.gzipSync(content);
|
||||
|
||||
files[header.name.replace(/^package\//, '')] = {
|
||||
gzip: gzipped.length,
|
||||
compressed: header.size ? gzipped.length / header.size : 1,
|
||||
...header
|
||||
};
|
||||
|
||||
next();
|
||||
});
|
||||
});
|
||||
|
||||
Readable.from(tgzData)
|
||||
.pipe(zlib.createGunzip())
|
||||
.pipe(extract)
|
||||
.on("error", reject)
|
||||
.on('finish', () => resolve(files));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const generateFileReport = async (files, historyCount = 3) => {
|
||||
const allFilesStat = {};
|
||||
const commits = (await getBlobHistory('package.json', historyCount)).filter(({tag}) => {
|
||||
return MAJOR_NUMBER === parseVersion(tag)[0];
|
||||
});
|
||||
const warns = [];
|
||||
|
||||
const npmHistory = {};
|
||||
|
||||
await Promise.all(commits.map(async ({tag}) => {
|
||||
npmHistory[tag] = await getFilesFromNPM(`axios@${tag.replace(/^v/, '')}`);
|
||||
}));
|
||||
|
||||
for(const [name, filename] of Object.entries(files)) {
|
||||
const file = await fs.stat(filename).catch(console.warn);
|
||||
const gzip = file ? zlib.gzipSync(await fs.readFile(filename)).length : 0;
|
||||
|
||||
const stat = allFilesStat[filename] = file ? {
|
||||
name,
|
||||
size: (await fs.stat(file)).size,
|
||||
path: file,
|
||||
gzip: await gzipSize(String(await fs.readFile(file))),
|
||||
commits,
|
||||
history: commits.map(({tag, size}) => `${prettyBytes(size)} (${tag})`).join(' ← ')
|
||||
size: file.size,
|
||||
path: filename,
|
||||
gzip,
|
||||
compressed: file.size ? gzip / file.size : 1,
|
||||
history: commits.map(({tag}) => {
|
||||
const files = npmHistory[tag];
|
||||
const file = files && files[filename] || null;
|
||||
|
||||
return {
|
||||
tag,
|
||||
...file
|
||||
};
|
||||
})
|
||||
} : null;
|
||||
|
||||
|
||||
|
||||
if(stat.history[0]) {
|
||||
const diff = stat.gzip - stat.history[0].gzip;
|
||||
|
||||
if (diff > FILE_SIZE_DIFF_THRESHOLD) {
|
||||
warns.push({
|
||||
filename,
|
||||
sizeReport: true,
|
||||
diff,
|
||||
percent: stat.gzip ? diff / stat.gzip : 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return stat;
|
||||
return {
|
||||
version,
|
||||
files: allFilesStat,
|
||||
warns
|
||||
};
|
||||
}
|
||||
|
||||
const generateBody = async ({files, template = './templates/pr.hbs'} = {}) => {
|
||||
const data = {
|
||||
files: await generateFileReport(files)
|
||||
};
|
||||
const data = await generateFileReport(files);
|
||||
|
||||
Handlebars.registerHelper('filesize', (bytes)=> prettyBytes(bytes));
|
||||
Handlebars.registerHelper('filesize', (bytes)=> bytes != null ? prettyBytes(bytes) : '<unknown>');
|
||||
Handlebars.registerHelper('percent', (value)=> Number.isFinite(value) ? `${(value * 100).toFixed(1)}%` : `---` );
|
||||
|
||||
return Handlebars.compile(String(await fs.readFile(template)))(data);
|
||||
}
|
||||
|
||||
console.log(await generateBody({
|
||||
files: {
|
||||
'Browser build (UMD)' : './dist/axios.min.js',
|
||||
'Browser build (ESM)' : './dist/esm/axios.min.js',
|
||||
'Browser build (UMD)' : 'dist/axios.min.js',
|
||||
'Browser build (ESM)' : 'dist/esm/axios.min.js',
|
||||
}
|
||||
}));
|
||||
|
||||
|
||||
Generated
+3809
-91
File diff suppressed because it is too large
Load Diff
+5
-4
@@ -91,6 +91,7 @@
|
||||
"@commitlint/cli": "^17.8.1",
|
||||
"@commitlint/config-conventional": "^17.8.1",
|
||||
"@release-it/conventional-changelog": "^5.1.1",
|
||||
"@rollup/plugin-alias": "^5.1.0",
|
||||
"@rollup/plugin-babel": "^5.3.1",
|
||||
"@rollup/plugin-commonjs": "^15.1.0",
|
||||
"@rollup/plugin-json": "^4.1.0",
|
||||
@@ -112,7 +113,6 @@
|
||||
"fs-extra": "^10.1.0",
|
||||
"get-stream": "^3.0.0",
|
||||
"gulp": "^4.0.2",
|
||||
"gzip-size": "^7.0.0",
|
||||
"handlebars": "^4.7.8",
|
||||
"husky": "^8.0.3",
|
||||
"istanbul-instrumenter-loader": "^3.0.1",
|
||||
@@ -131,6 +131,7 @@
|
||||
"minimist": "^1.2.8",
|
||||
"mocha": "^10.3.0",
|
||||
"multer": "^1.4.4",
|
||||
"pacote": "^20.0.0",
|
||||
"pretty-bytes": "^6.1.1",
|
||||
"release-it": "^15.11.0",
|
||||
"rollup": "^2.79.1",
|
||||
@@ -140,9 +141,9 @@
|
||||
"sinon": "^4.5.0",
|
||||
"stream-throttle": "^0.1.3",
|
||||
"string-replace-async": "^3.0.2",
|
||||
"tar-stream": "^3.1.7",
|
||||
"terser-webpack-plugin": "^4.2.3",
|
||||
"typescript": "^4.9.5",
|
||||
"@rollup/plugin-alias": "^5.1.0"
|
||||
"typescript": "^4.9.5"
|
||||
},
|
||||
"browser": {
|
||||
"./lib/adapters/http.js": "./lib/helpers/null.js",
|
||||
@@ -227,4 +228,4 @@
|
||||
"@commitlint/config-conventional"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+15
-4
@@ -1,10 +1,21 @@
|
||||
{{#if files}}
|
||||
### Build info
|
||||
### Build info (v{{version}})
|
||||
|
||||
Client bundles:
|
||||
{{#each files}}
|
||||
- {{ name}} ({{ path }}) {{ filesize size }} (**{{ filesize gzip }}** gzipped)
|
||||
{{#each commits}}
|
||||
- {{ tag }} - {{filesize size}}
|
||||
- {{ name}} ({{ path }})
|
||||
- **[ this ]** - {{ filesize size }} (**{{ filesize gzip }}** gzipped, {{percent compressed}})
|
||||
{{#each history}}
|
||||
- **[{{ tag }}]** - {{filesize size}} (**{{ filesize gzip }}** gzipped, {{percent compressed}})
|
||||
{{/each}}
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
|
||||
{{#if warns}}
|
||||
#### Warnings
|
||||
{{#each warns}}
|
||||
{{#if sizeReport}}
|
||||
- ⚠️ File `{{filename}}` has increased in size by `{{filesize diff}}` (`+{{percent percent}}`)
|
||||
{{/if}}
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
|
||||
Reference in New Issue
Block a user