-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmatcher-updater.js
85 lines (78 loc) · 2.23 KB
/
matcher-updater.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
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This module was incorporated due to the fact that
* `npm update` ONLY updates the dependencies based
* on the `package.json` file, which may be outdated.
* Also, this will ensure that "extraneous" packages,
* i.e., the ones natively included in node, actually
* are installed on the user's local since npm will
* frequently overlook those packages and thereby,
* causing the build to fail.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
const fs = require('fs');
const exec = require('child_process').exec;
const fetch = require('node-fetch');
const envVars = require('../.env_vars.json');
const pjson = require('../package.json');
const {ping, error} = require('../utils/matcher-pings').pings;
function isLoading(bool) {
if (bool) {
ping('⏳ ');
}
}
async function isOutdated() {
isLoading(true);
const payload = envVars.PAYLOAD;
const packet = {};
packet.response = await fetch(payload)
.then((res) => res.json())
.catch((e) => {
error(`\nNo internet connection available!
Check for details below 👇\n`);
error(`\n${JSON.stringify(e)}\n`);
process.exit();
});
packet.current = await packet.response.version;
packet.user = pjson.version;
// eslint-disable-next-line no-invalid-this
if (packet.user !== packet.current) {
packet.bool = true;
return packet;
} else {
packet.bool = false;
return packet;
}
}
async function takeAction() {
const packet = await isOutdated();
if (!packet.bool) {
ping(`\nYou are all set!
Current version: ${packet.current}\n`);
process.exit();
} else {
error(`\nFound: ${packet.user}
▶ Current: ${packet.current}`);
upgradeLocal(packet.response);
}
}
async function upgradeLocal(res) {
await fs.writeFileSync('./package.json', JSON.stringify(res, null, '\t'));
await npmi();
await ping('Done!');
}
function npmi() {
exec(envVars.QUERY, function(error, stdout, stderr) {
ping('\nstdout: ' + stdout);
ping('\nstderr: ' + stderr);
if (error) {
error('exec error: ' + error);
process.exit();
}
});
ping('\n Updating...\n');
}
exports.updateModules = {
isOutdated: isOutdated,
takeAction: takeAction,
};