-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
149 lines (132 loc) · 3.29 KB
/
app.ts
File metadata and controls
149 lines (132 loc) · 3.29 KB
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
#!/usr/bin/env node
import * as crypto from "crypto";
import * as fs from "fs";
import { exec } from "child_process";
import * as program from "commander";
const boxen = require("boxen");
const colours = require("colors");
function handleError(err: any) {
if (err) {
console.error(err);
process.exit(1);
}
}
function md5(str: string) {
return crypto
.createHash("md5")
.update(str, "utf8")
.digest("hex");
}
function calculateHash(): Promise<string> {
return new Promise(resolve =>
fs.readFile("package-lock.json", (err, data) => {
handleError(err);
resolve(md5(data.toString("utf8")));
})
);
}
function readHash() {
return new Promise(resolve => {
fs.readFile("package.md5", (err, data) => {
if (err && err.code === "ENOENT") {
resolve("");
return;
}
handleError(err);
resolve(data.toString("utf8"));
});
});
}
function saveHash(hash: string) {
return new Promise(resolve => {
fs.writeFile("package.md5", hash, err => {
handleError(err);
resolve();
});
});
}
async function npmInstall() {
return new Promise(resolve => {
console.log("pkgmon: Restoring packages...");
exec("npm ci", (err, stdout, stderr) => {
handleError(err);
console.warn(stderr);
console.log(stdout);
console.log(colours.green("pkgmon: Package restore complete."));
resolve();
});
});
}
async function check() {
const currentHash = await calculateHash();
const oldHash = await readHash();
if (currentHash !== oldHash) {
console.log(
boxen(
"pkgmon: Change in package-lock.json detected. " +
colours.bold("Please run ") +
colours.underline.bold("pkgmon install") +
colours.bold("!"),
{
padding: 1,
borderStyle: "double"
}
)
);
process.exit(-1);
} else {
console.log(
colours.green("pkgmon: No change in package-lock.json detected.")
);
}
}
async function run() {
const currentHash = await calculateHash();
const oldHash = await readHash();
if (currentHash !== oldHash) {
console.log(
"pkgmon: Change in package-lock.json detected. Running npm ci..."
);
await npmInstall();
await saveHash(currentHash);
} else {
console.log(
colours.green("pkgmon: No change in package-lock.json detected.")
);
}
}
async function install() {
await npmInstall();
await reset();
}
async function reset() {
const currentHash = await calculateHash();
await saveHash(currentHash);
console.log(colours.green("pkgmon: Reset complete."));
}
program.version("0.0.5");
program
.command("check")
.description("Checks if an npm install is required but will not execute it.")
.action(() => check());
program
.command("run")
.description(
"Checks if an npm install is required. If so, it will run the 'npm ci' command."
)
.action(() => run());
program
.command("install")
.description("Runs an 'npm ci' command and resets pkgmon.")
.action(() => install());
program
.command("reset")
.description(
"Resets pkgmon to the current installed packages. Use this if you know that an npm install is not required."
)
.action(() => reset());
if (process.argv.length < 3) {
program.help();
} else {
program.parse(process.argv);
}