Skip to content

Commit 7662c65

Browse files
author
Nick Balestra
committed
cycle-dev-utils package
1 parent bae8c7e commit 7662c65

File tree

9 files changed

+1214
-38
lines changed

9 files changed

+1214
-38
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* BSD License
3+
* For create-react-app software
4+
*
5+
* Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
6+
* Redistribution and use in source and binary forms, with or without modification,
7+
* are permitted provided that the following conditions are met:
8+
*
9+
* Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* Neither the name Facebook nor the names of its contributors may be used to
17+
* endorse or promote products derived from this software without specific
18+
* prior written permission.
19+
*
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
'use strict'
34+
35+
var fs = require('fs')
36+
var path = require('path')
37+
var chalk = require('chalk')
38+
var filesize = require('filesize')
39+
var recursive = require('recursive-readdir')
40+
var stripAnsi = require('strip-ansi')
41+
var gzipSize = require('gzip-size').sync
42+
43+
// Prints a detailed summary of build files.
44+
function printFileSizesAfterBuild (webpackStats, previousSizeMap) {
45+
var root = previousSizeMap.root
46+
var sizes = previousSizeMap.sizes
47+
var assets = webpackStats
48+
.toJson()
49+
.assets.filter(asset => /\.(js|css)$/.test(asset.name))
50+
.map(asset => {
51+
var fileContents = fs.readFileSync(path.join(root, asset.name))
52+
var size = gzipSize(fileContents)
53+
var previousSize = sizes[removeFileNameHash(root, asset.name)]
54+
var difference = getDifferenceLabel(size, previousSize)
55+
return {
56+
folder: path.join('build', path.dirname(asset.name)),
57+
name: path.basename(asset.name),
58+
size: size,
59+
sizeLabel: filesize(size) + (difference ? ' (' + difference + ')' : '')
60+
}
61+
})
62+
assets.sort((a, b) => b.size - a.size)
63+
var longestSizeLabelLength = Math.max.apply(
64+
null,
65+
assets.map(a => stripAnsi(a.sizeLabel).length)
66+
)
67+
assets.forEach(asset => {
68+
var sizeLabel = asset.sizeLabel
69+
var sizeLength = stripAnsi(sizeLabel).length
70+
if (sizeLength < longestSizeLabelLength) {
71+
var rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength)
72+
sizeLabel += rightPadding
73+
}
74+
console.log(
75+
' ' +
76+
sizeLabel +
77+
' ' +
78+
chalk.dim(asset.folder + path.sep) +
79+
chalk.cyan(asset.name)
80+
)
81+
})
82+
}
83+
84+
function removeFileNameHash (buildFolder, fileName) {
85+
return fileName
86+
.replace(buildFolder, '')
87+
.replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3)
88+
}
89+
90+
// Input: 1024, 2048
91+
// Output: "(+1 KB)"
92+
function getDifferenceLabel (currentSize, previousSize) {
93+
var FIFTY_KILOBYTES = 1024 * 50
94+
var difference = currentSize - previousSize
95+
var fileSize = !Number.isNaN(difference) ? filesize(difference) : 0
96+
if (difference >= FIFTY_KILOBYTES) {
97+
return chalk.red('+' + fileSize)
98+
} else if (difference < FIFTY_KILOBYTES && difference > 0) {
99+
return chalk.yellow('+' + fileSize)
100+
} else if (difference < 0) {
101+
return chalk.green(fileSize)
102+
} else {
103+
return ''
104+
}
105+
}
106+
107+
function measureFileSizesBeforeBuild (buildFolder) {
108+
return new Promise(resolve => {
109+
recursive(buildFolder, (err, fileNames) => {
110+
var sizes
111+
if (!err && fileNames) {
112+
sizes = fileNames
113+
.filter(fileName => /\.(js|css)$/.test(fileName))
114+
.reduce(
115+
(memo, fileName) => {
116+
var contents = fs.readFileSync(fileName)
117+
var key = removeFileNameHash(buildFolder, fileName)
118+
memo[key] = gzipSize(contents)
119+
return memo
120+
},
121+
{}
122+
)
123+
}
124+
resolve({
125+
root: buildFolder,
126+
sizes: sizes || {}
127+
})
128+
})
129+
})
130+
}
131+
132+
module.exports = {
133+
measureFileSizesBeforeBuild: measureFileSizesBeforeBuild,
134+
printFileSizesAfterBuild: printFileSizesAfterBuild
135+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* BSD License
3+
* For create-react-app software
4+
*
5+
* Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
6+
* Redistribution and use in source and binary forms, with or without modification,
7+
* are permitted provided that the following conditions are met:
8+
*
9+
* Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* Neither the name Facebook nor the names of its contributors may be used to
17+
* endorse or promote products derived from this software without specific
18+
* prior written permission.
19+
*
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
'use strict'
34+
35+
var Anser = require('anser')
36+
37+
// Color scheme inspired by https://chriskempson.github.io/base16/css/base16-github.css
38+
// var base00 = 'ffffff'; // Default Background
39+
var base01 = 'f5f5f5' // Lighter Background (Used for status bars)
40+
// var base02 = 'c8c8fa'; // Selection Background
41+
var base03 = '969896' // Comments, Invisibles, Line Highlighting
42+
// var base04 = 'e8e8e8'; // Dark Foreground (Used for status bars)
43+
var base05 = '333333' // Default Foreground, Caret, Delimiters, Operators
44+
// var base06 = 'ffffff'; // Light Foreground (Not often used)
45+
// var base07 = 'ffffff'; // Light Background (Not often used)
46+
var base08 = 'ed6a43' // Variables, XML Tags, Markup Link Text, Markup Lists, Diff Deleted
47+
// var base09 = '0086b3'; // Integers, Boolean, Constants, XML Attributes, Markup Link Url
48+
// var base0A = '795da3'; // Classes, Markup Bold, Search Text Background
49+
var base0B = '183691' // Strings, Inherited Class, Markup Code, Diff Inserted
50+
var base0C = '183691' // Support, Regular Expressions, Escape Characters, Markup Quotes
51+
// var base0D = '795da3'; // Functions, Methods, Attribute IDs, Headings
52+
var base0E = 'a71d5d' // Keywords, Storage, Selector, Markup Italic, Diff Changed
53+
// var base0F = '333333'; // Deprecated, Opening/Closing Embedded Language Tags e.g. <?php ?>
54+
55+
// Map ANSI colors from what babel-code-frame uses to base16-github
56+
// See: https://github.com/babel/babel/blob/e86f62b304d280d0bab52c38d61842b853848ba6/packages/babel-code-frame/src/index.js#L9-L22
57+
var colors = {
58+
reset: [base05, 'transparent'],
59+
black: base05,
60+
red: base08 /* marker, bg-invalid */,
61+
green: base0B /* string */,
62+
yellow: base08 /* capitalized, jsx_tag, punctuator */,
63+
blue: base0C,
64+
magenta: base0C /* regex */,
65+
cyan: base0E /* keyword */,
66+
gray: base03 /* comment, gutter */,
67+
lightgrey: base01,
68+
darkgrey: base03
69+
}
70+
71+
var anserMap = {
72+
'ansi-bright-black': 'black',
73+
'ansi-bright-yellow': 'yellow',
74+
'ansi-yellow': 'yellow',
75+
'ansi-bright-green': 'green',
76+
'ansi-green': 'green',
77+
'ansi-bright-cyan': 'cyan',
78+
'ansi-cyan': 'cyan',
79+
'ansi-bright-red': 'red',
80+
'ansi-red': 'red',
81+
'ansi-bright-magenta': 'magenta',
82+
'ansi-magenta': 'magenta'
83+
}
84+
85+
function ansiHTML (txt) {
86+
var arr = new Anser().ansiToJson(txt, {
87+
use_classes: true
88+
})
89+
90+
var result = ''
91+
var open = false
92+
for (var index = 0; index < arr.length; ++index) {
93+
var c = arr[index]
94+
var content = c.content
95+
var fg = c.fg
96+
97+
var contentParts = content.split('\n')
98+
for (var _index = 0; _index < contentParts.length; ++_index) {
99+
if (!open) {
100+
result += '<span data-ansi-line="true">'
101+
open = true
102+
}
103+
var part = contentParts[_index].replace('\r', '')
104+
var color = colors[anserMap[fg]]
105+
if (color != null) {
106+
result += '<span style="color: #' + color + ';">' + part + '</span>'
107+
} else {
108+
if (fg != null) console.log('Missing color mapping: ', fg)
109+
result += '<span>' + part + '</span>'
110+
}
111+
if (_index < contentParts.length - 1) {
112+
result += '</span>'
113+
open = false
114+
result += '<br/>'
115+
}
116+
}
117+
}
118+
if (open) {
119+
result += '</span>'
120+
open = false
121+
}
122+
return result
123+
}
124+
125+
module.exports = ansiHTML
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* BSD License
3+
* For create-react-app software
4+
*
5+
* Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
6+
* Redistribution and use in source and binary forms, with or without modification,
7+
* are permitted provided that the following conditions are met:
8+
*
9+
* Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* Neither the name Facebook nor the names of its contributors may be used to
17+
* endorse or promote products derived from this software without specific
18+
* prior written permission.
19+
*
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
'use strict'
34+
35+
function clearConsole () {
36+
process.stdout.write(
37+
process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H'
38+
)
39+
}
40+
41+
module.exports = clearConsole

0 commit comments

Comments
 (0)