Skip to content
This repository was archived by the owner on Jan 6, 2022. It is now read-only.

Commit 54c213b

Browse files
committed
format-diff
- refactored lib/format-diff.js - added tests
1 parent 03a5404 commit 54c213b

File tree

4 files changed

+369
-77
lines changed

4 files changed

+369
-77
lines changed

index.js

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,14 @@
11
const fs = require('fs')
2-
const { EOL } = require('os')
32
const { promisify } = require('util')
43

5-
const colorizeDiff = require('@npmcli/disparity-colors')
64
const Arborist = require('@npmcli/arborist')
7-
const jsDiff = require('diff')
85
const pacote = require('pacote')
96
const packlist = require('npm-packlist')
107
const rpj = require('read-package-json-fast')
118

12-
const shouldPrintPatch = require('./lib/should-print-patch.js')
9+
const formatDiff = require('./lib/format-diff.js')
1310
const untar = require('./lib/untar.js')
1411

15-
const printDiff = ({ files, opts, refs, versions }) => {
16-
for (const filename of files.values()) {
17-
const names = {
18-
a: `a/${filename}`,
19-
b: `b/${filename}`
20-
}
21-
22-
let fileMode = ''
23-
const filenames = {
24-
a: refs.get(names.a),
25-
b: refs.get(names.b)
26-
}
27-
const contents = {
28-
a: filenames.a && filenames.a.content,
29-
b: filenames.b && filenames.b.content
30-
}
31-
const modes = {
32-
a: filenames.a && filenames.a.mode,
33-
b: filenames.b && filenames.b.mode
34-
}
35-
36-
if (contents.a === contents.b) continue
37-
38-
let res = ''
39-
let headerLength = 0
40-
const header = str => {
41-
headerLength++
42-
res += `${str}${EOL}`
43-
}
44-
45-
// manually build a git diff-compatible header
46-
header(`diff --git ${names.a} ${names.b}`)
47-
if (modes.a === modes.b) {
48-
fileMode = filenames.a.mode
49-
} else {
50-
if (modes.a && modes.b) {
51-
header(`old mode ${modes.a}`)
52-
header(`new mode ${modes.b}`)
53-
} else if (modes.a && !modes.b) {
54-
header(`deleted file mode ${modes.a}`)
55-
} else if (!modes.a && modes.b) {
56-
header(`new file mode ${modes.b}`)
57-
}
58-
}
59-
header(`index ${versions.a}..${versions.b} ${fileMode}`)
60-
61-
if (shouldPrintPatch(filename)) {
62-
res += jsDiff.createTwoFilesPatch(
63-
names.a,
64-
names.b,
65-
contents.a || '',
66-
contents.b || '',
67-
'',
68-
'',
69-
{ context: 3 }
70-
).replace(
71-
'===================================================================\n',
72-
''
73-
)
74-
headerLength += 2
75-
} else {
76-
header(`--- ${names.a}`)
77-
header(`+++ ${names.b}`)
78-
}
79-
80-
return opts.color
81-
? colorizeDiff(res, { headerLength })
82-
: res
83-
}
84-
}
85-
8612
const readPackageFiles = async ({ files, path, prefix, refs }) => {
8713
const readFile = promisify(fs.readFile)
8814
const stat = promisify(fs.stat)
@@ -131,7 +57,7 @@ const diffSelf = async (opts = {}) => {
13157
prefix: 'b/'
13258
})
13359

134-
printDiff({
60+
formatDiff({
13561
files,
13662
opts,
13763
refs,
@@ -196,7 +122,7 @@ const diffComparison = async (specs, opts = {}) => {
196122
}
197123
], opts)
198124

199-
printDiff({
125+
formatDiff({
200126
files,
201127
opts,
202128
refs,

lib/format-diff.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const { EOL } = require('os')
2+
3+
const colorizeDiff = require('@npmcli/disparity-colors')
4+
const jsDiff = require('diff')
5+
6+
const shouldPrintPatch = require('./should-print-patch.js')
7+
8+
const formatDiff = ({ files, opts = {}, refs, versions }) => {
9+
for (const filename of files.values()) {
10+
const names = {
11+
a: `a/${filename}`,
12+
b: `b/${filename}`
13+
}
14+
15+
let fileMode = ''
16+
const filenames = {
17+
a: refs.get(names.a),
18+
b: refs.get(names.b)
19+
}
20+
const contents = {
21+
a: filenames.a && filenames.a.content,
22+
b: filenames.b && filenames.b.content
23+
}
24+
const modes = {
25+
a: filenames.a && filenames.a.mode,
26+
b: filenames.b && filenames.b.mode
27+
}
28+
29+
if (contents.a === contents.b && modes.a === modes.b) continue
30+
31+
let res = ''
32+
let headerLength = 0
33+
const header = str => {
34+
headerLength++
35+
res += `${str}${EOL}`
36+
}
37+
38+
// manually build a git diff-compatible header
39+
header(`diff --git ${names.a} ${names.b}`)
40+
if (modes.a === modes.b) {
41+
fileMode = filenames.a.mode
42+
} else {
43+
if (modes.a && !modes.b) {
44+
header(`deleted file mode ${modes.a}`)
45+
} else if (!modes.a && modes.b) {
46+
header(`new file mode ${modes.b}`)
47+
} else {
48+
header(`old mode ${modes.a}`)
49+
header(`new mode ${modes.b}`)
50+
}
51+
}
52+
header(`index v${versions.a}..v${versions.b} ${fileMode}`)
53+
54+
if (shouldPrintPatch(filename)) {
55+
res += jsDiff.createTwoFilesPatch(
56+
names.a,
57+
names.b,
58+
contents.a || '',
59+
contents.b || '',
60+
'',
61+
'',
62+
{ context: 3 }
63+
).replace(
64+
'===================================================================\n',
65+
''
66+
)
67+
headerLength += 2
68+
} else {
69+
header(`--- ${names.a}`)
70+
header(`+++ ${names.b}`)
71+
}
72+
73+
return opts.color
74+
? colorizeDiff(res, { headerLength })
75+
: res
76+
}
77+
78+
return ''
79+
}
80+
81+
module.exports = formatDiff
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/format-diff.js TAP added file > should output expected added file diff result 1`] = `
9+
diff --git a/foo.js b/foo.js
10+
new file mode 100755
11+
index v1.0.0..v2.0.0
12+
--- a/foo.js
13+
+++ b/foo.js
14+
@@ -0,0 +1,2 @@
15+
+"use strict"
16+
+module.exports = "foo"
17+
18+
`
19+
20+
exports[`test/format-diff.js TAP binary file > should output expected bin file diff result 1`] = `
21+
diff --git a/foo.jpg b/foo.jpg
22+
index v1.0.0..v2.0.0 100644
23+
--- a/foo.jpg
24+
+++ b/foo.jpg
25+
26+
`
27+
28+
exports[`test/format-diff.js TAP changed file mode > should output expected changed file mode diff result 1`] = `
29+
diff --git a/foo.js b/foo.js
30+
old mode 100644
31+
new mode 100755
32+
index v1.0.0..v2.0.0
33+
--- a/foo.js
34+
+++ b/foo.js
35+
36+
`
37+
38+
exports[`test/format-diff.js TAP colored output > should output expected colored diff result 1`] = `
39+
diff --git a/foo.js b/foo.js
40+
index v1.0.0..v2.0.0 100644
41+
--- a/foo.js 
42+
+++ b/foo.js 
43+
@@ -1,2 +1,2 @@
44+
"use strict"
45+
-module.exports = "foo"
46+
+module.exports = "foobar"
47+
48+
`
49+
50+
exports[`test/format-diff.js TAP format removed file > should output expected removed file diff result 1`] = `
51+
diff --git a/foo.js b/foo.js
52+
deleted file mode 100644
53+
index v1.0.0..v2.0.0
54+
--- a/foo.js
55+
+++ b/foo.js
56+
@@ -1,2 +0,0 @@
57+
-"use strict"
58+
-module.exports = "foo"
59+
/ No newline at end of file
60+
61+
`
62+
63+
exports[`test/format-diff.js TAP format simple diff > should output expected diff result 1`] = `
64+
diff --git a/foo.js b/foo.js
65+
index v1.0.0..v2.0.0 100644
66+
--- a/foo.js
67+
+++ b/foo.js
68+
@@ -1,2 +1,2 @@
69+
"use strict"
70+
-module.exports = "foo"
71+
+module.exports = "foobar"
72+
73+
`

0 commit comments

Comments
 (0)