123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- '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();
- };
|