1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/development/build/display.js

151 lines
4.2 KiB
JavaScript
Raw Normal View History

Build system refactor (#8140) * build - start static asset task cleanup * build - simplify manifest tasks * build - refactor + rename some tasks * build - various cleanups * manifest - fix ref from controller * build - drop gulp for simple async tasks * build - breakout gulpfile into multiple files * build - rename some tasks * build - use task fn refs instead of string names * build - bundle all scripts first, except for contentscript * build - improve task timeline * deps - update lock * build - improve task time printout * build/scripts - remove intermediate named task * build - use 'yarn build' for task entry points * build - properly run tasks via runTask for timeline display * development/announcer - fix manifest path + clean * build - lint fix * build - make all defined tasks possible entry points * build/task - properly report errors during task * ci - fix sesify/lavamoat-viz build command * build/scripts - run each bundle in separate processes * lint fix * build - forward childProcess logs to console * build/task - fix parallel/series stream end event * build/scripts refactor contentscript+inpage into a single task * build/static - use the fs for 150x speedup zomg * lint fix * build/static - fix css copy * Update development/build/scripts.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Update development/build/scripts.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Update development/build/index.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * deps - remove redundant mkdirp * deps - remove unused pumpify * deps - remove redundant merge-deep * deps - prefer is-stream of isstream * deps - remove clone for lodash.cloneDeep * clean - remove commented code * build/static - use fs.copy + fast-glob instead of linux cp for better platform support * build/manifest - standardize task naming * build/display - clean - remove unused code * bugfix - fix fs.promises import * build - create "clean" as named task for use as entrypoint * build/static - fix for copying dirs * Update development/build/task.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Update development/build/display.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Update development/build/display.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Update development/build/display.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * build - use task refs, tasks only return promises not streams, etc * lint fi bad merge + lint * build - one last cleanup + refactor * build - add comments introducing file * build/manifest - fix bug + subtasks dont beed to be named * Update package.json Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * build/task - remove unused fn * Update package.json Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Update development/build/styles.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> * Update development/build/styles.js Co-Authored-By: Mark Stacey <markjstacey@gmail.com> Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2020-03-09 01:55:02 +01:00
const randomColor = require('randomcolor')
const chalk = require('chalk')
module.exports = { setupTaskDisplay, displayChart }
const SYMBOLS = {
Empty: '',
Space: ' ',
Full: '█',
SevenEighths: '▉',
ThreeQuarters: '▊',
FiveEighths: '▋',
Half: '▌',
ThreeEighths: '▍',
Quarter: '▎',
Eighth: '▏',
RightHalf: '▐',
RightEigth: '▕',
}
function setupTaskDisplay (taskEvents) {
const taskData = []
taskEvents.on('start', ([name]) => {
console.log(`Starting '${name}'...`)
})
taskEvents.on('end', ([name, start, end]) => {
taskData.push([name, start, end])
console.log(`Finished '${name}'`)
})
taskEvents.on('complete', () => {
displayChart(taskData)
})
}
function displayChart (data) {
// sort tasks by start time
data.sort((a, b,) => a[1] - b[1])
// get bounds
const first = Math.min(...data.map((entry) => entry[1]))
const last = Math.max(...data.map((entry) => entry[2]))
// get colors
const colors = randomColor({ count: data.length })
// some heading before the bars
console.log(`\nbuild completed. task timeline:`)
// build bars for bounds
data.map((entry, index) => {
const [label, start, end] = entry
const [start2, end2] = [start, end].map((value) => adjust(value, first, last, 40))
const barString = barBuilder(start2, end2)
const color = colors[index]
const coloredBarString = colorize(color, barString)
const duration = ((end - start) / 1e3).toFixed(1)
console.log(coloredBarString, `${label} ${duration}s`)
})
}
function colorize (color, string) {
const colorizer = (typeof chalk[color] === 'function') ? chalk[color] : chalk.hex(color)
return colorizer(string)
}
// scale number within bounds
function adjust (value, first, last, size) {
const length = last - first
const result = (value - first) / length * size
return result
}
// draw bars
function barBuilder (start, end) {
const [spaceInt, spaceRest] = splitNumber(start)
const barBodyLength = end - spaceInt
let [barInt, barRest] = splitNumber(barBodyLength)
// We are handling zero value as a special case
// to print at least something on the screen
if (barInt === 0 && barRest === 0) {
barInt = 0
barRest = 0.001
}
const spaceFull = SYMBOLS.Space.repeat(spaceInt)
const spacePartial = getSymbolNormalRight(spaceRest)
const barFull = SYMBOLS.Full.repeat(barInt)
const barPartial = getSymbolNormal(barRest)
return `${spaceFull}${spacePartial}${barFull}${barPartial}`
}
// get integer and remainder
function splitNumber (value = 0) {
const [int, rest = '0'] = value.toString().split('.')
const int2 = parseInt(int, 10)
const rest2 = parseInt(rest, 10) / Math.pow(10, rest.length)
return [int2, rest2]
}
// get partial block char for value (left-adjusted)
function getSymbolNormal (value) {
// round to closest supported value
const possibleValues = [0, 1 / 8, 1 / 4, 3 / 8, 1 / 2, 5 / 8, 3 / 4, 7 / 8, 1]
const rounded = possibleValues.reduce((prev, curr) => {
return (Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev)
})
if (rounded === 0) {
return SYMBOLS.Empty
} else if (rounded === 1 / 8) {
return SYMBOLS.Eighth
} else if (rounded === 1 / 4) {
return SYMBOLS.Quarter
} else if (rounded === 3 / 8) {
return SYMBOLS.ThreeEighths
} else if (rounded === 1 / 2) {
return SYMBOLS.Half
} else if (rounded === 5 / 8) {
return SYMBOLS.FiveEighths
} else if (rounded === 3 / 4) {
return SYMBOLS.ThreeQuarters
} else if (rounded === 7 / 8) {
return SYMBOLS.SevenEighths
} else {
return SYMBOLS.Full
}
}
// get partial block char for value (right-adjusted)
function getSymbolNormalRight (value) {
// round to closest supported value (not much :/)
const possibleValues = [0, 1 / 2, 7 / 8, 1]
const rounded = possibleValues.reduce((prev, curr) => {
return (Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev)
})
if (rounded === 0) {
return SYMBOLS.Full
} else if (rounded === 1 / 2) {
return SYMBOLS.RightHalf
} else if (rounded === 7 / 8) {
return SYMBOLS.RightEigth
} else if (rounded === 1) {
return SYMBOLS.Space
} else {
throw new Error('getSymbolNormalRight got unexpected result')
}
}