2019-04-26 23:12:00 +03:00
|
|
|
#!/usr/bin/env node
|
2021-08-12 00:10:47 +03:00
|
|
|
import { join } from 'path'
|
|
|
|
import { table } from 'table'
|
|
|
|
import { dirname } from 'path'
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
import filesize from 'filesize'
|
|
|
|
import fs from 'fs'
|
|
|
|
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
2019-04-26 23:12:00 +03:00
|
|
|
|
|
|
|
// ensure that K and B values line up vertically
|
|
|
|
const filesizeConfig = {symbols: {KB: 'K'}}
|
|
|
|
const prettySize = bytes => filesize(bytes, filesizeConfig)
|
2019-04-26 23:56:26 +03:00
|
|
|
|
2021-04-09 07:18:03 +03:00
|
|
|
function getBundles(path) {
|
2021-08-12 00:10:47 +03:00
|
|
|
const meta = JSON.parse(fs.readFileSync(join(path, './dist/meta.json')))
|
2021-04-09 07:18:03 +03:00
|
|
|
let metaBundles = Object.values(meta.bundles)
|
2019-04-26 23:56:26 +03:00
|
|
|
|
2022-03-08 07:29:10 +03:00
|
|
|
// filter out support bundles, since they don't generate CSS
|
2021-04-09 07:18:03 +03:00
|
|
|
metaBundles = metaBundles.filter(bundle => !isSupportBundleName(bundle.name))
|
|
|
|
const bundles = {}
|
|
|
|
for (const bundle of metaBundles) {
|
2021-08-12 00:10:47 +03:00
|
|
|
const stats = JSON.parse(fs.readFileSync(join(path, `./${bundle.stats}`)))
|
2019-04-26 23:12:00 +03:00
|
|
|
const entry = {
|
2019-04-26 23:56:26 +03:00
|
|
|
name: bundle.name,
|
2019-04-26 23:12:00 +03:00
|
|
|
path: bundle.css,
|
2021-08-12 00:10:47 +03:00
|
|
|
stats: stats
|
2019-04-26 23:12:00 +03:00
|
|
|
}
|
2021-04-09 07:18:03 +03:00
|
|
|
bundles[bundle.name] = entry
|
2019-04-26 23:12:00 +03:00
|
|
|
}
|
|
|
|
|
2021-04-09 07:18:03 +03:00
|
|
|
return bundles
|
|
|
|
}
|
2019-04-26 23:12:00 +03:00
|
|
|
|
2021-04-09 07:18:03 +03:00
|
|
|
const tableOptions = {
|
2021-04-10 23:36:52 +03:00
|
|
|
singleLine: true,
|
|
|
|
border: {
|
|
|
|
topBody: '',
|
|
|
|
topJoin: '',
|
|
|
|
topLeft: '',
|
|
|
|
topRight: '',
|
|
|
|
|
|
|
|
bottomBody: '',
|
|
|
|
bottomJoin: '',
|
|
|
|
bottomLeft: '',
|
|
|
|
bottomRight: '',
|
|
|
|
|
|
|
|
bodyLeft: '|',
|
|
|
|
bodyRight: '|',
|
|
|
|
bodyJoin: '|',
|
|
|
|
|
|
|
|
joinBody: '',
|
|
|
|
joinLeft: '',
|
|
|
|
joinRight: '',
|
|
|
|
joinJoin: ''
|
2019-04-26 23:12:00 +03:00
|
|
|
}
|
2021-04-09 07:18:03 +03:00
|
|
|
}
|
2019-04-26 23:12:00 +03:00
|
|
|
|
2021-04-09 07:18:03 +03:00
|
|
|
// const sortByName = (a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)
|
|
|
|
const sortByGZipSize = (a, b) => b[3] - a[3]
|
|
|
|
// const sortByRawSize = (a, b) => b[5] - a[5]
|
|
|
|
const posNeg = v => (v > 0 ? '+ ' : v < 0 ? '- ' : '')
|
|
|
|
|
|
|
|
;(async () => {
|
|
|
|
const currentBundles = getBundles(join(__dirname, '../'))
|
|
|
|
const latestBundles = getBundles(join(__dirname, '../tmp/node_modules/@primer/css/'))
|
|
|
|
|
|
|
|
let data = []
|
|
|
|
|
|
|
|
// Build the rows
|
|
|
|
for (const name of Object.keys(currentBundles)) {
|
2021-09-17 04:54:24 +03:00
|
|
|
const {
|
|
|
|
stats: {
|
|
|
|
selectors: {
|
|
|
|
total: current_selectors_total
|
|
|
|
},
|
|
|
|
gzipSize: current_gzip_size,
|
|
|
|
size: current_size
|
|
|
|
}
|
|
|
|
} = currentBundles[name] || { stats: { selectors: { total: 0 }, gzipSize: 0, size: 0 } }
|
|
|
|
|
|
|
|
const {
|
|
|
|
stats: {
|
|
|
|
selectors: {
|
|
|
|
total: latest_selectors_total
|
|
|
|
},
|
|
|
|
gzipSize: latest_gzip_size,
|
|
|
|
size: latest_size
|
|
|
|
}
|
|
|
|
} = latestBundles[name] || { stats: { selectors: { total: 0 }, gzipSize: 0, size: 0 } }
|
2021-04-10 23:53:25 +03:00
|
|
|
|
|
|
|
const delta = [
|
2021-09-17 04:54:24 +03:00
|
|
|
current_selectors_total - latest_selectors_total,
|
|
|
|
current_gzip_size - latest_gzip_size,
|
|
|
|
current_size - latest_size
|
2021-04-10 23:53:25 +03:00
|
|
|
].reduce((a, b) => a + b, 0)
|
|
|
|
|
|
|
|
if (delta !== 0) {
|
|
|
|
data.push([
|
2021-09-17 04:54:24 +03:00
|
|
|
name,
|
|
|
|
current_selectors_total,
|
|
|
|
current_selectors_total - latest_selectors_total,
|
|
|
|
current_gzip_size,
|
|
|
|
current_gzip_size - latest_gzip_size,
|
|
|
|
current_size,
|
|
|
|
current_size - latest_size
|
2021-04-10 23:53:25 +03:00
|
|
|
])
|
|
|
|
}
|
2019-04-26 23:56:26 +03:00
|
|
|
}
|
|
|
|
|
2021-04-09 07:18:03 +03:00
|
|
|
// Sort the data
|
|
|
|
data = data.sort(sortByGZipSize)
|
|
|
|
|
|
|
|
// Beautify the data
|
|
|
|
data = data.map(row => {
|
|
|
|
row[2] = posNeg(row[2]) + `${row[2]}`.replace('-', '')
|
|
|
|
row[3] = prettySize(row[3])
|
|
|
|
row[4] = posNeg(row[4]) + `${prettySize(row[4])}`.replace('-', '')
|
|
|
|
row[5] = prettySize(row[5])
|
|
|
|
row[6] = posNeg(row[6]) + `${prettySize(row[6])}`.replace('-', '')
|
|
|
|
return row
|
|
|
|
})
|
2019-04-26 23:56:26 +03:00
|
|
|
|
2021-04-09 07:18:03 +03:00
|
|
|
// Adding header
|
2021-04-10 23:36:52 +03:00
|
|
|
data = [
|
2021-04-10 23:53:25 +03:00
|
|
|
['name', 'selectors', '±', 'gzip size', '±', 'raw size', '±'],
|
|
|
|
[':-', '-:', '-:', '-:', '-:', '-:', '-:']
|
2021-04-10 23:36:52 +03:00
|
|
|
].concat(data)
|
2019-04-26 23:56:26 +03:00
|
|
|
|
2021-04-09 07:18:03 +03:00
|
|
|
console.log(table(data, tableOptions))
|
|
|
|
})()
|
2019-04-26 23:56:26 +03:00
|
|
|
|
2021-04-02 21:06:31 +03:00
|
|
|
function isSupportBundleName(bundleName) {
|
2019-04-26 23:56:26 +03:00
|
|
|
// "support", "marketing-support", and any future ones?
|
2021-04-02 21:06:31 +03:00
|
|
|
return bundleName.endsWith('support')
|
2019-04-26 23:56:26 +03:00
|
|
|
}
|