mirror of
https://github.com/kennethreitz/bake.git
synced 2026-06-05 23:00:17 +00:00
cleanup
This commit is contained in:
+48
@@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const readChunk = require('read-chunk');
|
||||
const istextorbinary = require('istextorbinary');
|
||||
const dateFormat = require('dateformat');
|
||||
const prettyBytes = require('pretty-bytes');
|
||||
const Table = require('cli-table');
|
||||
|
||||
exports.isBinary = (existingFilePath, newFileContents) => {
|
||||
const existingHeader = readChunk.sync(existingFilePath, 0, 512);
|
||||
return (
|
||||
istextorbinary.isBinarySync(undefined, existingHeader) ||
|
||||
(newFileContents && istextorbinary.isBinarySync(undefined, newFileContents))
|
||||
);
|
||||
};
|
||||
|
||||
exports.diff = (existingFilePath, newFileContents) => {
|
||||
const existingStat = fs.statSync(existingFilePath);
|
||||
const table = new Table({
|
||||
head: ['', 'Existing', 'Replacement', 'Diff']
|
||||
});
|
||||
|
||||
let sizeDiff;
|
||||
|
||||
if (!newFileContents) {
|
||||
newFileContents = Buffer.from([]);
|
||||
}
|
||||
|
||||
if (existingStat.size > newFileContents.length) {
|
||||
sizeDiff = '-';
|
||||
} else {
|
||||
sizeDiff = '+';
|
||||
}
|
||||
|
||||
sizeDiff += prettyBytes(Math.abs(existingStat.size - newFileContents.length));
|
||||
|
||||
table.push(
|
||||
[
|
||||
'Size',
|
||||
prettyBytes(existingStat.size),
|
||||
prettyBytes(newFileContents.length),
|
||||
sizeDiff
|
||||
],
|
||||
['Last modified', dateFormat(existingStat.mtime), '', '']
|
||||
);
|
||||
|
||||
return table.toString();
|
||||
};
|
||||
Reference in New Issue
Block a user