forked from finos/SymphonyElectron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafterPackHoock.js
More file actions
95 lines (83 loc) · 2.88 KB
/
afterPackHoock.js
File metadata and controls
95 lines (83 loc) · 2.88 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
const path = require('path');
const { flipFuses, FuseVersion, FuseV1Options } = require('@electron/fuses');
const fs = require('fs');
const {
saveMetaData,
getTotalSize,
formatSize,
getFolderSizes,
} = require('./helpers/script/script-helper');
const {
compareAndPrint,
} = require('../scripts/helpers/terminal/terminal-helper');
const buildDir = './dist/win-unpacked';
const folderMetadataFile = './dist/bundle-analytics/folder-size-metadata.json';
const currentFolderMetaDataFile =
'./dist/bundle-analytics/folder-size-metadata.json';
const folderComparison = () => {
console.log(
'====================Build Size Changes By Folders====================\n',
);
const folderSizes = getFolderSizes(buildDir);
let previousBuildSizeTotal = 0;
let previousFolderSizes = {};
if (fs.existsSync(folderMetadataFile)) {
previousFolderSizes = JSON.parse(fs.readFileSync(folderMetadataFile));
previousBuildSizeTotal = Object.values(previousFolderSizes).reduce(
(prev, next) => prev + next,
0,
);
console.log('Comparing with previous metadata...\n');
compareAndPrint(folderSizes, previousFolderSizes, 0.01);
return previousBuildSizeTotal;
} else {
console.log('No previous metadata found. Creating one...');
}
saveMetaData(folderSizes, currentFolderMetaDataFile);
};
const analyzeBundle = (context, totalPreviousBuildSize) => {
console.log('\n====================Bundle overall====================\n');
const appDir = context.appOutDir;
console.log(`📦 Analyzing size of unpacked Electron app at: ${appDir}\n`);
const entries = fs.readdirSync(appDir);
let total = 0;
for (const entry of entries) {
// nosemgrep
const fullPath = path.join(appDir, entry);
const size = fs.statSync(fullPath).isDirectory()
? getTotalSize(fullPath, entry)
: fs.statSync(fullPath).size;
total += size;
console.log(`${formatSize(size).padEnd(10)} ${entry}`);
}
console.log(`\n🧮 Total pre-package size: ${formatSize(total)}\n`);
console.log(
`\n🧮 Overall Size Different: ${
total - totalPreviousBuildSize > 0 ? 'INCREASED by' : 'DECREASED by'
} ${formatSize(Math.abs(total - totalPreviousBuildSize))}\n`,
);
};
module.exports = async function afterPack(context) {
const {
appOutDir,
packager: { appInfo },
} = context;
const ext = {
darwin: '.app',
win32: '.exe',
}[context.electronPlatformName];
const electronBinaryPath = path.join(
appOutDir,
`${appInfo.productFilename}${ext}`,
);
await flipFuses(electronBinaryPath, {
version: FuseVersion.V1,
[FuseV1Options.EnableNodeCliInspectArguments]: false,
});
if (context.electronPlatformName === 'win32') {
// Run comparison for folder changes
const totalPreviousBuildSize = folderComparison();
// Run comparison for bundle changes
totalPreviousBuildSize && analyzeBundle(context, totalPreviousBuildSize);
}
};