@opscompass/opscompass-cli 1.0.46 → 1.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/colors.js +4 -3
- package/index.js +27 -8
- package/package.json +1 -1
- package/updates.js +50 -38
package/colors.js
CHANGED
package/index.js
CHANGED
|
@@ -4,10 +4,14 @@ const modules = require("./modules.js");
|
|
|
4
4
|
const handleError = require("./error.js");
|
|
5
5
|
const { canReadStoredCredentials } = require("./credentials.js");
|
|
6
6
|
const { isLocal, opscompassHome } = require("./environment/current.js");
|
|
7
|
+
const pkg = require("./package.json");
|
|
7
8
|
|
|
8
9
|
const fs = require("fs");
|
|
9
10
|
const path = require("path");
|
|
10
11
|
|
|
12
|
+
const semver = require("semver");
|
|
13
|
+
const { yellowText, redText } = require("./colors.js");
|
|
14
|
+
|
|
11
15
|
let defaultParams = JSON.parse("{}");
|
|
12
16
|
|
|
13
17
|
// Make sure opscompassHome exists
|
|
@@ -27,7 +31,6 @@ let isOffline = false;
|
|
|
27
31
|
const offlinePath = path.join(opscompassHome, ".offline");
|
|
28
32
|
if (fs.existsSync(offlinePath)) {
|
|
29
33
|
isOffline = true;
|
|
30
|
-
console.error(" NOTE: Using CLI in offline mode");
|
|
31
34
|
}
|
|
32
35
|
global.isOffline = isOffline;
|
|
33
36
|
const { versionCheck } = require("./updates.js");
|
|
@@ -84,18 +87,17 @@ function help() {
|
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
(async function () {
|
|
90
|
+
let versionCheckPromise;
|
|
87
91
|
if (!isLocal) {
|
|
88
|
-
versionCheck();
|
|
92
|
+
versionCheckPromise = versionCheck();
|
|
89
93
|
}
|
|
90
|
-
|
|
91
94
|
checkDefaults();
|
|
92
95
|
|
|
93
96
|
const [proc, command, commandModule, ...args] = process.argv;
|
|
94
97
|
const moduleName = commandModule?.toLowerCase() ?? "";
|
|
95
98
|
if (moduleName === "" || moduleName === "help") {
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
if (modules[moduleName]) {
|
|
99
|
+
help();
|
|
100
|
+
} else if (modules[moduleName]) {
|
|
99
101
|
const module = modules[moduleName];
|
|
100
102
|
if (!module.requiresLogin || loggedIn()) {
|
|
101
103
|
try {
|
|
@@ -111,10 +113,27 @@ function help() {
|
|
|
111
113
|
requiresLogin();
|
|
112
114
|
}
|
|
113
115
|
} else if (moduleName === "version") {
|
|
114
|
-
const pkg = require("./package.json");
|
|
115
116
|
console.log(`${pkg.name} ${pkg.version}`);
|
|
116
117
|
} else {
|
|
117
118
|
console.error(`Unknown module, "${moduleName}"`);
|
|
118
|
-
|
|
119
|
+
help();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Wait for version check to complete and show output at the end
|
|
123
|
+
if (versionCheckPromise) {
|
|
124
|
+
const versions = await versionCheckPromise;
|
|
125
|
+
/* Semver used to semantically compare version numbers */
|
|
126
|
+
if (versions && semver.gt(versions.latest, pkg.version)) {
|
|
127
|
+
console.error(
|
|
128
|
+
yellowText,
|
|
129
|
+
`Warning! You are using CLI version ${pkg.version}, but the latest version is ${versions.latest}. To upgrade to the latest version, please run:`
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
/* Prompt user to upgrade to the correct package based on their current environment */
|
|
133
|
+
console.error(
|
|
134
|
+
greenText,
|
|
135
|
+
`\nnpm install -g ${versions.pkg}@${versions.latest}\n`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
119
138
|
}
|
|
120
139
|
})();
|
package/package.json
CHANGED
package/updates.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const pkg = require("./package.json");
|
|
2
2
|
const semver = require("semver");
|
|
3
3
|
const { exec } = require("child_process");
|
|
4
|
-
const {
|
|
4
|
+
const { yellowText, redText } = require("./colors.js");
|
|
5
5
|
const { opscompassHome } = require("./environment/current.js");
|
|
6
6
|
const fs = require("fs");
|
|
7
7
|
const path = require("path");
|
|
@@ -11,59 +11,71 @@ const path = require("path");
|
|
|
11
11
|
|
|
12
12
|
function versionCheck() {
|
|
13
13
|
if (global.isOffline) {
|
|
14
|
-
|
|
14
|
+
console.error(yellowText, "Using CLI in offline mode");
|
|
15
|
+
return Promise.resolve();
|
|
15
16
|
}
|
|
16
17
|
const checkPath = path.join(opscompassHome, ".versioncheck");
|
|
17
18
|
|
|
18
|
-
// If .versioncheck exists and is less than 24 hours old, return
|
|
19
|
+
// If .versioncheck exists and is less than 24 hours old, return the cached version
|
|
19
20
|
if (fs.existsSync(checkPath)) {
|
|
20
21
|
const stats = fs.statSync(checkPath);
|
|
21
22
|
const now = Date.now();
|
|
22
23
|
const mtime = new Date(stats.mtime).getTime();
|
|
23
24
|
const hours24 = 24 * 60 * 60 * 1000;
|
|
24
25
|
if (now - mtime < hours24) {
|
|
25
|
-
|
|
26
|
+
try {
|
|
27
|
+
const cachedVersionData = fs.readFileSync(checkPath, "utf8").trim();
|
|
28
|
+
const versionObject = JSON.parse(cachedVersionData);
|
|
29
|
+
return Promise.resolve(versionObject);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
// If we can't read or parse the file, fall through to do a fresh check
|
|
32
|
+
}
|
|
26
33
|
}
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
// Remove the existing .versioncheck file since we need to check the version
|
|
37
|
+
try {
|
|
38
|
+
fs.unlinkSync(checkPath);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
// Ignore errors when removing the file
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Return a Promise that resolves when version check completes
|
|
44
|
+
return new Promise((resolve) => {
|
|
45
|
+
const environmentSpecificPackageVersion = `npm view ${pkg.name} version`;
|
|
46
|
+
/* latestVersion is the standard output(stdout)
|
|
47
|
+
This is usually fast, but it can hang, so we set a timeout.
|
|
48
|
+
On error, we just resolve without doing anything further.
|
|
49
|
+
*/
|
|
50
|
+
exec(
|
|
51
|
+
environmentSpecificPackageVersion,
|
|
52
|
+
{ timeout: 6000 },
|
|
53
|
+
(error, latestVersion, stderr) => {
|
|
54
|
+
if (error) {
|
|
55
|
+
if (error.killed) {
|
|
56
|
+
console.error(yellowText, "CLI Version check warning: timed out.");
|
|
57
|
+
} else {
|
|
58
|
+
console.error(redText, `CLI Version check error: ${error.message}`);
|
|
59
|
+
}
|
|
60
|
+
resolve();
|
|
61
|
+
return;
|
|
40
62
|
}
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
63
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// If .versioncheck doesn't exist, create it
|
|
49
|
-
fs.closeSync(fs.openSync(checkPath, "w"));
|
|
50
|
-
}
|
|
64
|
+
const result = {
|
|
65
|
+
pkg: pkg.name,
|
|
66
|
+
latest: latestVersion.trim(),
|
|
67
|
+
};
|
|
51
68
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
console.error(
|
|
60
|
-
greenText,
|
|
61
|
-
`\nnpm install -g ${pkg.name}@${latestVersion}\n`
|
|
62
|
-
);
|
|
63
|
-
return;
|
|
69
|
+
// Update the .versioncheck file with the versions after successful check
|
|
70
|
+
try {
|
|
71
|
+
fs.writeFileSync(checkPath, JSON.stringify(result, null, 2) + "\n");
|
|
72
|
+
} catch (err) {
|
|
73
|
+
// Ignore errors when writing the version file
|
|
74
|
+
}
|
|
75
|
+
resolve(result);
|
|
64
76
|
}
|
|
65
|
-
|
|
66
|
-
);
|
|
77
|
+
);
|
|
78
|
+
});
|
|
67
79
|
}
|
|
68
80
|
|
|
69
81
|
module.exports = {
|