-
-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom diff view for binary files
- Loading branch information
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var readChunk = require('read-chunk'); | ||
var istextorbinary = require('istextorbinary'); | ||
var dateFormat = require('dateformat'); | ||
var prettyBytes = require('pretty-bytes'); | ||
var Table = require('cli-table'); | ||
|
||
module.exports = { | ||
isBinary: function (existingFilePath, newFileContents) { | ||
var existingHeader = readChunk.sync(existingFilePath, 0, 512); | ||
return istextorbinary.isBinarySync(undefined, existingHeader) || istextorbinary.isBinarySync(undefined, newFileContents); | ||
}, | ||
|
||
diff: function (existingFilePath, newFileContents) { | ||
var existingStat = fs.statSync(existingFilePath); | ||
|
||
var table = new Table({ | ||
head: ['', 'Existing', 'Replacement', 'Diff'] | ||
}); | ||
|
||
var sizeDiff; | ||
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters