Skip to content

Commit

Permalink
simplify assets
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Dec 11, 2018
1 parent 21ba715 commit f435e2a
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions build/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const Table = require('cli-table');
const path = require('path');
const sh = require('shelljs');

// find all js/css files in the dist dir
// but ignore any files in lang, example, or font directories
const filepaths = sh.find(path.join(__dirname, '..', 'dist', '**', '*.{js,css}')).filter(function(filepath) {
if ((/\/(lang|example|font)\//).test(filepath)) {
return false;
Expand All @@ -14,8 +16,9 @@ const filepaths = sh.find(path.join(__dirname, '..', 'dist', '**', '*.{js,css}')
return true;
});

// map all files that we found into an array of
// table entries the filepath, file size, and gzip size.
Promise.all(filepaths.map(function(filepath) {
// gzip and stat
return new Promise(function(resolve, reject) {
const readStream = fs.createReadStream(filepath);
const writeStream = fs.createWriteStream(filepath + '.gz');
Expand All @@ -25,36 +28,25 @@ Promise.all(filepaths.map(function(filepath) {
const gzStat = fs.statSync(filepath + '.gz');
const fileStat = fs.statSync(filepath);

resolve({filepath, gzStat, fileStat});
fs.unlinkSync(filepath + '.gz');

resolve([filepath.split('dist/')[1], filesize(fileStat.size), filesize(gzStat.size)]);
})
.on('error', reject);
});
})).then(function(results) {
return Promise.all(results.map(function(result) {
return new Promise(function(resolve, reject) {
const {fileStat, gzStat, filepath} = result;
})).then(function(lines) {
// log all the files and there sizes using a cli table
const table = new Table({
head: ['filename', 'size', 'gzipped'],
colAligns: ['left', 'right', 'right'],
style: {
border: ['white']
}
});

resolve([filepath.split('dist/')[1], filesize(fileStat.size), filesize(gzStat.size)]);
});
}));
})
.then(function(lines) {
const table = new Table({
head: ['filename', 'size', 'gzipped'],
colAligns: ['left', 'right', 'right'],
style: {
border: ['white']
}
});

table.push.apply(table, lines);
console.log(table.toString());

filepaths.forEach(function(filepath) {
fs.unlinkSync(filepath + '.gz');
});
table.push.apply(table, lines);
console.log(table.toString());

})
.catch(function(err) {
console.error(err.stack);
});
}).catch(function(err) {
console.error(err.stack);
});

0 comments on commit f435e2a

Please sign in to comment.