-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (138 loc) · 3.65 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /usr/bin/env node
const fs = require("fs");
const process = require("process");
const child_process = require("child_process");
const program = require("commander");
if (!fs.existsSync("./package-lock.json")) {
process.stderr.write("package-json.lock does not exist!\n");
return -1;
}
program
.version("1.1.1")
.option("-e, --errors", "Output errors")
.option("-p, --prefix <string>", "Commit message default", "chore")
.parse(process.argv);
const options = program.opts();
const writeCommitMessage = (diffs) => {
const prefix = options.prefix + ": ";
process.stdout.write(prefix + "Update packages\n\n");
diffs.forEach(function (diff) {
var str = "* " + diff.pkg;
if (diff.new) {
str += " was added";
process.stdout.write(str + "\n");
} else if (diff.deleted) {
str += " was removed";
process.stdout.write(str + "\n");
} else if (diff.version || diff.ref) {
if (diff.version) {
str += ": " + diff.version.old + " => " + diff.version.new;
} else if (diff.ref) {
str += ": " + diff.ref.old + " => " + diff.ref.new;
}
process.stdout.write(str + "\n");
}
});
};
const comparePackages = (package_key, package_old, package_new) => {
let diff = {
pkg: package_key,
};
if (package_old.version !== package_new.version) {
diff.version = {
old: package_old.version,
new: package_new.version,
};
}
if (diff.version) {
return diff;
}
return false;
};
const packageDiff = (lock_head, lock_current) => {
let diffs = [];
let packages_head = lock_head.packages;
let packages_current = lock_current.packages;
let package_keys_head = Object.keys(packages_head);
let package_keys_current = Object.keys(packages_current);
package_keys_head.forEach((package_key) => {
if (package_key === "") {
return;
}
if (package_keys_current.includes(package_key)) {
let diff = comparePackages(
package_key,
packages_head[package_key],
packages_current[package_key]
);
if (diff !== false) {
diffs.push(diff);
}
} else {
// package was deleted
diffs.push({
pkg: package_key,
deleted: true,
});
}
});
// Check for new packages
package_keys_current.forEach((package_key) => {
if (package_key === "") {
return;
}
if (!package_keys_head.includes(package_key)) {
diffs.push({
pkg: package_key,
new: true,
});
}
});
return diffs;
};
const loadCurrentPackageLock = () => {
return new Promise(function (resolve, reject) {
fs.readFile("./package-lock.json", "utf8", function (error, data) {
if (error) {
reject(error);
return;
}
resolve(JSON.parse(data));
});
});
};
const loadHeadPackageLock = () => {
return new Promise(function (resolve) {
var p = child_process.spawn("git", ["show", "HEAD:./package-lock.json"]);
var json = "";
p.stdout.on("data", (data) => {
json += data;
});
p.stderr.on("data", (data) => {
if (options.errors) {
process.stderr.write(`stderr: ${data}`);
}
});
p.on("close", (code) => {
if (code !== 0) {
resolve({ packages: [] });
} else {
resolve(JSON.parse(json));
}
});
});
};
const lock_head = loadHeadPackageLock();
const lock_updated = loadCurrentPackageLock();
Promise.all([lock_head, lock_updated]).then(
function (values) {
const [head, current] = values;
const diffs = packageDiff(head, current);
writeCommitMessage(diffs);
},
function (error) {
if (options.errors) {
process.stderr.write(`Err: ${error}\n`);
}
}
);