Skip to content
Snippets Groups Projects

Task update2024

Merged Johannes Kreiner requested to merge task_update2024 into master
Compare and Show latest version
2 files
+ 55
28
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 45
26
@@ -43,58 +43,52 @@ export default class SizeGuard extends Task {
await this.createConfig(false);
const config = this.loadBranchConfig();
Object.keys(config).forEach((filename) => {
// Check if file exists
if (!fs.existsSync(config[filename].path)) {
return;
}
for (const filename of Object.keys(config)) {
const fileInfo = config[filename];
if (!fs.existsSync(fileInfo.path)) continue;
const stats = fs.statSync(config[filename].path);
const size = config[filename].filesize;
let diff = size - stats.size;
let diffPercentage = Math.round((1 - stats.size / size) * 10_000) / 100;
const fileExtension = filename.split('.').pop() || '';
const maxIncrease = (this._config.sizeGuard[fileExtension] || {}).maxIncrease || 0;
const stableSize = await this.getStableFileSize(fileInfo.path);
const oldSize = fileInfo.filesize;
const diff = stableSize - oldSize;
let diffPercentage = Math.round((1 - stableSize / oldSize) * 10_000) / 100;
// No changes
if (diff === 0) {
return;
continue;
}
// Size of file decreased
if (diff > 0) {
if (diff < 0) {
this._logger.success(
`${filename} decreased ${diffPercentage}% (${diff / 1000} kB) in size.`,
`${filename} decreased ${diffPercentage}% (${Math.abs(diff / 1000)} kB) in size.`,
);
this.acceptSize(filename, true);
return;
await this.acceptSize(filename, true);
continue;
}
// Size of file increased
diff = Math.abs(diff);
diffPercentage = Math.round((1 - size / stats.size) * 10_000) / 100;
// File got bigger
const fileExtension = path.extname(filename).replace('.', '');
const maxIncrease = (this._config.sizeGuard[fileExtension] || {}).maxIncrease || 0;
// Below the threshold
if (diff <= maxIncrease) {
this._logger.info(
`${filename} increased ${diffPercentage}% (${
`${filename} increased ${Math.abs(diffPercentage)}% (${
diff / 1000
} kB) and is below the threshold of ${maxIncrease / 1000} kB.`,
);
this.acceptSize(filename, true);
return;
await this.acceptSize(filename, true);
continue;
}
// Above the threshold
this._logger.error(
`${filename} increased ${diffPercentage}% (${
`${filename} increased ${Math.abs(diffPercentage)}% (${
diff / 1000
} kB) and is above the threshold (${
maxIncrease / 1000
} kB).\nPlease reduce your file size or accept this change with "sgc sizeGuard:accept ${filename}".`,
false,
);
});
}
}
/**
@@ -293,4 +287,29 @@ export default class SizeGuard extends Task {
});
return `${currentBranch}`.trim();
}
/**
* Gets the stable file size as sometimes the file contents are
* cleared temporarily, resulting in wrongly reported sizes.
*
* @param {string} filePath
* @param {number} retries
* @param {number} delayMs
* @returns {Promise<number>}
*/
async getStableFileSize(filePath, retries = 3, delayMs = 100) {
for (let index = 0; index < retries; index++) {
const stats = fs.statSync(filePath);
if (stats.size > 0 || index === retries - 1) {
return stats.size;
}
await new Promise((resolve) => setTimeout(resolve, delayMs));
}
return new Promise((resolve) => {
const stats = fs.statSync(filePath);
resolve(stats.size);
});
}
}
Loading