-
Notifications
You must be signed in to change notification settings - Fork 1
/
concat_json.js
executable file
·94 lines (80 loc) · 2.22 KB
/
concat_json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
const fs = require("fs").promises;
const crypto = require("crypto");
const resumeSchema = require("resume-schema");
const dateFns = require("date-fns");
function displaySuccess(output) {
console.log(output);
}
function displayError(error) {
if (error instanceof Error) {
console.error(error.stack);
} else {
console.error(error);
}
process.exit(1);
}
function computeHashes(payload) {
payload.work.forEach((work) => {
work.id = `w${crypto
.createHash("sha256")
.update(work.company)
.digest("hex")
.substring(0, 10)}`;
});
}
function formatDates(payload) {
payload.work.forEach((work) => {
if (work.startDate) {
try {
const startDate = new Date(work.startDate);
const endDate = work.endDate ? new Date(work.endDate) : new Date();
work.startDate = dateFns.format(startDate, "LLL yyyy");
work.endDate = work.endDate
? dateFns.format(endDate, "LLL yyyy")
: work.endDate;
work.duration = dateFns.formatDistance(startDate, endDate);
} catch (e) {
console.error(e);
}
}
});
payload.education.forEach((education) => {
if (education.startDate) {
try {
const startDate = new Date(education.startDate);
const endDate = education.endDate
? new Date(education.endDate)
: new Date();
education.startDate = dateFns.format(startDate, "LLL yyyy");
education.endDate = education.endDate
? dateFns.format(endDate, "LLL yyyy")
: education.endDate;
} catch (e) {
console.error(e);
}
}
});
}
function validateSchema(payload) {
resumeSchema.validate(payload, (err) => {
if (err) {
console.error("The resume is invalid:", err);
}
});
}
(async () => {
try {
const content = await Promise.all(
process.argv.slice(2).map((file) => fs.readFile(file, "utf-8")),
);
const jsonContent = content.map((c) => JSON.parse(c));
validateSchema(jsonContent[0]);
const data = jsonContent.reduce((p, c) => Object.assign(p, c), {});
computeHashes(data);
formatDates(data);
displaySuccess(JSON.stringify(data, null, 2));
} catch (e) {
displayError(e);
}
})();