@opscompass/opscompass-cli 1.0.46 → 1.3.1

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/updates.js CHANGED
@@ -1,71 +1,81 @@
1
- const pkg = require("./package.json");
2
- const semver = require("semver");
3
- const { exec } = require("child_process");
4
- const { greenText, yellowText } = require("./colors.js");
5
- const { opscompassHome } = require("./environment/current.js");
6
- const fs = require("fs");
7
- const path = require("path");
1
+ import pkg from "./package.json" with { type: "json" };
2
+ import semver from "semver";
3
+ import { exec } from "child_process";
4
+ import { yellowText, redText } from "./colors.js";
5
+ import { opscompassHome } from "./environment/current.js";
6
+ import fs from "fs";
7
+ import path from "path";
8
8
 
9
9
  /* versionCheck sends a command to the terminal to check the latest package version and compares it to the current version specified in package.json, prompting the user to upgrade if it is out of date. */
10
10
  /* This is turned off by default when using the local environment. To turn it on for dev purposes, comment out isLocal check in index.js */
11
11
 
12
12
  function versionCheck() {
13
13
  if (global.isOffline) {
14
- return;
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
- return;
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
- const environmentSpecificPackageVersion = `npm view ${pkg.name} version`;
30
- /* latestVersion is the standard output(stdout), which is read and compared to current package version */
31
- exec(
32
- environmentSpecificPackageVersion,
33
- { timeout: 5000 },
34
- (error, latestVersion, stderr) => {
35
- if (error) {
36
- if (error.killed) {
37
- console.log("Version check error: timed out.");
38
- } else {
39
- console.log(`Version check error: ${error.message}`);
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
- // Update the timestamp of .versioncheck file after successful check
45
- try {
46
- fs.utimesSync(checkPath, new Date(), new Date());
47
- } catch (err) {
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
- /* Semver used to semantically compare version numbers */
53
- if (semver.gt(latestVersion, pkg.version)) {
54
- console.error(
55
- yellowText,
56
- `Warning! You are using CLI version ${pkg.version}, but the latest version is ${latestVersion}To upgrade to the latest version, please run:`
57
- );
58
- /* Prompt user to upgrade to the correct package based on their current environment */
59
- console.error(
60
- greenText,
61
- `\nnpm install -g ${pkg.name}@${latestVersion}\n`
62
- );
63
- return;
64
- }
65
- }
66
- );
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);
76
+ },
77
+ );
78
+ });
67
79
  }
68
80
 
69
- module.exports = {
70
- versionCheck,
71
- };
81
+ export { versionCheck };