Skip to content

Commit db788ed

Browse files
committed
update
1 parent f3b0d9e commit db788ed

File tree

5 files changed

+97
-47
lines changed

5 files changed

+97
-47
lines changed

index.js

100755100644
File mode changed.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-diff-cli",
3-
"version": "0.4.9",
3+
"version": "0.5.0",
44
"description": "json-diff-cli",
55
"main": "index.js",
66
"scripts": {
@@ -35,7 +35,7 @@
3535
"chalk": "^1.1.3",
3636
"csvtojson": "^1.1.5",
3737
"easy-table": "^1.1.0",
38-
"json-diff-core": "^0.1.6",
38+
"json-diff-core": "^0.1.8",
3939
"yargs": "^8.0.1"
4040
},
4141
"release": {

scripts/csv.js

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,44 @@ const parseCSV = (csvPath) => {
99
const result = [];
1010
return new Promise((resolve, reject) => {
1111
csv({ noheader: false })
12-
.fromFile(csvPath)
13-
.on('json', (obj) => {
14-
result.push(obj);
15-
})
16-
.on('done', (err) => {
17-
if (err) {
18-
reject(err);
19-
} else {
20-
resolve(result);
21-
}
22-
});
12+
.fromFile(csvPath)
13+
.on('json', (obj) => {
14+
result.push(obj);
15+
})
16+
.on('done', (err) => {
17+
if (err) {
18+
reject(err);
19+
} else {
20+
resolve(result);
21+
}
22+
});
2323
});
2424
};
2525

26-
const readFirstLine = csvPath => new Promise((resolve, reject) => {
27-
const rs = fs.createReadStream(csvPath, { encoding: 'utf8' });
28-
let acc = '';
29-
let pos = 0;
30-
let index;
31-
rs.on('data', (chunk) => {
32-
index = chunk.indexOf('\n');
33-
acc += chunk;
34-
if (index !== -1) {
35-
rs.close();
36-
} else {
37-
pos += chunk.length;
38-
}
39-
})
40-
.on('close', () => {
41-
resolve(acc.slice(0, pos + index));
42-
})
43-
.on('error', (err) => {
44-
reject(err);
26+
const readFirstLine = (csvPath) =>
27+
new Promise((resolve, reject) => {
28+
const rs = fs.createReadStream(csvPath, { encoding: 'utf8' });
29+
let acc = '';
30+
let pos = 0;
31+
let index;
32+
rs.on('data', (chunk) => {
33+
index = chunk.indexOf('\n');
34+
acc += chunk;
35+
if (index !== -1) {
36+
rs.close();
37+
} else {
38+
pos += chunk.length;
39+
}
40+
})
41+
.on('close', () => {
42+
resolve(acc.slice(0, pos + index));
43+
})
44+
.on('error', (err) => {
45+
reject(err);
46+
});
4547
});
46-
});
4748

48-
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
49+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
4950

5051
const csvScript = async (args) => {
5152
const csvPath = args.path;
@@ -69,10 +70,35 @@ const csvScript = async (args) => {
6970
const url2 = row.url2;
7071
const method = row.method;
7172
const sortKey = row.sortKey;
72-
const body = row.body;
73+
let body = row.body;
7374
const timeout = args.timeout;
7475
const skipcertificate = row.skipCertificate || false;
7576
const expectedStatusCode = row.expectedStatusCode ? parseInt(row.expectedStatusCode, 10) : null;
77+
let customDiff = row.customDiff || '{}';
78+
let customCompare = row.customCompare || '{}';
79+
80+
try {
81+
const parsedJSON = JSON.parse(body);
82+
body = [];
83+
Object.keys(parsedJSON).forEach((key) => {
84+
const value = parsedJSON[key];
85+
body.push(`${key}:${value}`);
86+
});
87+
} catch (err) {
88+
// do nothing
89+
}
90+
91+
try {
92+
customDiff = JSON.parse(customDiff);
93+
} catch (err) {
94+
throw new Error('invalid json provided to customDiff');
95+
}
96+
97+
try {
98+
customCompare = JSON.parse(customCompare);
99+
} catch (err) {
100+
throw new Error('invalid json provided to customCompare');
101+
}
76102

77103
if (url1.charAt(0) === '#') {
78104
continue; // eslint-disable-line
@@ -129,6 +155,8 @@ const csvScript = async (args) => {
129155
expectedStatusCode,
130156
skipHeaders,
131157
ignore: ignores,
158+
customDiff,
159+
customCompare,
132160
};
133161

134162
try {
@@ -209,6 +237,7 @@ const csvScript = async (args) => {
209237
}
210238
};
211239

212-
module.exports = args => csvScript(args).catch((err) => {
213-
console.log(chalk.red(err.toString()));
214-
});
240+
module.exports = (args) =>
241+
csvScript(args).catch((err) => {
242+
console.log(chalk.red(err.toString()));
243+
});

scripts/diff.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ const diffScript = async (args) => {
77
const leftUrl = args.leftURL;
88
const rightURL = args.rightURL;
99

10-
const diff = await core.diffURLs(leftUrl, rightURL, args);
10+
const expectedStatusCode = args.expectedStatusCode ? parseInt(args.expectedStatusCode, 10) : null;
11+
let customCompare = args.customCompare || '{}';
12+
13+
try {
14+
customCompare = JSON.parse(customCompare);
15+
} catch (err) {
16+
throw new Error('invalid json provided to customCompare');
17+
}
18+
19+
const diff = await core.diffURLs(leftUrl, rightURL, {
20+
...args,
21+
customCompare,
22+
expectedStatusCode,
23+
});
1124

1225
if (args.diffheaders) {
1326
const headersDiff = await core.diffJSON(diff.leftHeaders, diff.rightHeaders);
@@ -60,9 +73,17 @@ const diffScript = async (args) => {
6073
}
6174
const output = t.toString();
6275

63-
console.log(output);
76+
if (!args.quiet) {
77+
console.log(output);
78+
}
79+
80+
if (args.failOnDiff && (diff.differences.length > 1 || diff.differences[0].diff !== 'none')) {
81+
process.exit(1);
82+
}
6483
};
6584

66-
module.exports = args => diffScript(args).catch((err) => {
67-
console.log(chalk.red(err.toString()));
68-
});
85+
module.exports = (args) =>
86+
diffScript(args).catch((err) => {
87+
console.log(chalk.red(err.toString()));
88+
process.exit(1);
89+
});

0 commit comments

Comments
 (0)