@arifbiswas/git-pre-push 1.0.7 → 1.0.8

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/bin/cli.js CHANGED
@@ -1,9 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ const pkg = require('../package.json');
3
4
  const { runPrePush, runManualScan, installHook, uninstallHook } = require('../src/index');
4
5
  const { cleanInfectedFiles } = require('../src/cleaner');
5
6
  const { setupGitHubAction } = require('../src/action');
6
7
  const { runInteractiveMenu } = require('../src/menu');
8
+ const { notifyIfUpdateAvailable, checkUpdateManual } = require('../src/updateNotifier');
7
9
  const { banner, colors } = require('../src/reporter');
8
10
 
9
11
  const args = process.argv.slice(2);
@@ -11,7 +13,18 @@ const command = args[0] ? args[0].toLowerCase() : 'menu';
11
13
  const isGlobalFlag = args.includes('--global') || args.includes('-g');
12
14
 
13
15
  async function main() {
16
+ // Check for updates non-blocking on interactive commands
17
+ if (command !== 'scan-push') {
18
+ await notifyIfUpdateAvailable(pkg.version);
19
+ }
20
+
14
21
  switch (command) {
22
+ case 'update':
23
+ case 'upgrade':
24
+ banner();
25
+ await checkUpdateManual(pkg.version);
26
+ break;
27
+
15
28
  case 'menu':
16
29
  case 'interactive':
17
30
  await runInteractiveMenu();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arifbiswas/git-pre-push",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Zero-dependency pre-push security guard that stops malware, injected scripts, and leaked secrets from reaching GitHub.",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/menu.js CHANGED
@@ -22,6 +22,7 @@ async function runInteractiveMenu() {
22
22
  console.log(` ${colors.green}[6]${colors.reset} ⚡ ${colors.bold}Run Push Intercept Demo${colors.reset} - Simulate a blocked malware push`);
23
23
  console.log(` ${colors.green}[7]${colors.reset} 🤖 ${colors.bold}Setup GitHub Action CI${colors.reset} - Guard repository on GitHub server-side`);
24
24
  console.log(` ${colors.green}[8]${colors.reset} ❌ ${colors.bold}Uninstall Hook${colors.reset} - Remove pre-push hook`);
25
+ console.log(` ${colors.green}[9]${colors.reset} 🔄 ${colors.bold}Check for Updates${colors.reset} - Check if a new version is available on npm`);
25
26
  console.log(` ${colors.dim}[0] 🚪 Exit${colors.reset}\n`);
26
27
 
27
28
  const rl = readline.createInterface({
@@ -30,7 +31,7 @@ async function runInteractiveMenu() {
30
31
  });
31
32
 
32
33
  const choice = await new Promise((resolve) => {
33
- rl.question(`${colors.bold}${colors.yellow}Enter your choice (1-8, 0 to exit): ${colors.reset}`, (ans) => {
34
+ rl.question(`${colors.bold}${colors.yellow}Enter your choice (1-9, 0 to exit): ${colors.reset}`, (ans) => {
34
35
  rl.close();
35
36
  resolve(ans.trim());
36
37
  });
@@ -143,6 +144,13 @@ async function runInteractiveMenu() {
143
144
  break;
144
145
  }
145
146
 
147
+ case '9': {
148
+ const pkg = require('../package.json');
149
+ const { checkUpdateManual } = require('./updateNotifier');
150
+ await checkUpdateManual(pkg.version);
151
+ break;
152
+ }
153
+
146
154
  case '0':
147
155
  default:
148
156
  console.log(`\n${colors.dim}Goodbye! 🛡️${colors.reset}\n`);
@@ -153,3 +161,4 @@ async function runInteractiveMenu() {
153
161
  module.exports = {
154
162
  runInteractiveMenu
155
163
  };
164
+
@@ -0,0 +1,147 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const os = require('os');
4
+ const { colors } = require('./reporter');
5
+
6
+ const CACHE_FILE = path.join(os.homedir(), '.gitsafty-update.json');
7
+ const CHECK_INTERVAL_MS = 12 * 60 * 60 * 1000; // Check at most once every 12 hours
8
+ const PACKAGE_NAME = '@arifbiswas/git-pre-push';
9
+
10
+ /**
11
+ * Checks if latest semver string is strictly newer than current semver string
12
+ * @param {string} current
13
+ * @param {string} latest
14
+ * @returns {boolean}
15
+ */
16
+ function isNewerVersion(current, latest) {
17
+ if (!current || !latest) return false;
18
+ const c = current.replace(/^v/, '').split('.').map(n => parseInt(n, 10) || 0);
19
+ const l = latest.replace(/^v/, '').split('.').map(n => parseInt(n, 10) || 0);
20
+
21
+ for (let i = 0; i < 3; i++) {
22
+ const cv = c[i] || 0;
23
+ const lv = l[i] || 0;
24
+ if (lv > cv) return true;
25
+ if (lv < cv) return false;
26
+ }
27
+ return false;
28
+ }
29
+
30
+ /**
31
+ * Reads cached update info
32
+ */
33
+ function readCache() {
34
+ try {
35
+ if (fs.existsSync(CACHE_FILE)) {
36
+ return JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8'));
37
+ }
38
+ } catch (_) {}
39
+ return null;
40
+ }
41
+
42
+ /**
43
+ * Writes cached update info
44
+ * @param {string} latestVersion
45
+ */
46
+ function writeCache(latestVersion) {
47
+ try {
48
+ fs.writeFileSync(
49
+ CACHE_FILE,
50
+ JSON.stringify({ lastChecked: Date.now(), latestVersion }, null, 2),
51
+ 'utf8'
52
+ );
53
+ } catch (_) {}
54
+ }
55
+
56
+ /**
57
+ * Fetches latest version from npm registry with timeout
58
+ * @returns {Promise<string | null>}
59
+ */
60
+ async function fetchLatestFromNpm() {
61
+ try {
62
+ const controller = new AbortController();
63
+ const timeout = setTimeout(() => controller.abort(), 1200);
64
+
65
+ const res = await fetch(`https://registry.npmjs.org/${PACKAGE_NAME}/latest`, {
66
+ signal: controller.signal,
67
+ headers: { 'User-Agent': 'git-pre-push-update-check' }
68
+ });
69
+ clearTimeout(timeout);
70
+
71
+ if (res.ok) {
72
+ const data = await res.json();
73
+ return data.version || null;
74
+ }
75
+ } catch (_) {}
76
+ return null;
77
+ }
78
+
79
+ /**
80
+ * Prints a clean update notification box
81
+ * @param {string} currentVersion
82
+ * @param {string} latestVersion
83
+ */
84
+ function printUpdateNotification(currentVersion, latestVersion) {
85
+ const line1 = ` Update available! ${colors.dim}${currentVersion}${colors.reset} → ${colors.green}${colors.bold}${latestVersion}${colors.reset}`;
86
+ const line2 = ` Run ${colors.cyan}${colors.bold}npm install -g ${PACKAGE_NAME}${colors.reset} to update`;
87
+ const line3 = ` Or in project: ${colors.cyan}npm install -D ${PACKAGE_NAME}${colors.reset}`;
88
+
89
+ console.log(`\n${colors.yellow}╭─────────────────────────────────────────────────────────────╮${colors.reset}`);
90
+ console.log(`${colors.yellow}│${colors.reset}${line1}`);
91
+ console.log(`${colors.yellow}│${colors.reset}${line2}`);
92
+ console.log(`${colors.yellow}│${colors.reset}${line3}`);
93
+ console.log(`${colors.yellow}╰─────────────────────────────────────────────────────────────╯${colors.reset}\n`);
94
+ }
95
+
96
+ /**
97
+ * Automatically checks for updates (non-blocking, cached)
98
+ * @param {string} currentVersion
99
+ */
100
+ async function notifyIfUpdateAvailable(currentVersion) {
101
+ const cache = readCache();
102
+ const now = Date.now();
103
+
104
+ let latestVersion = cache ? cache.latestVersion : null;
105
+ const shouldCheckRegistry = !cache || (now - (cache.lastChecked || 0) > CHECK_INTERVAL_MS);
106
+
107
+ if (shouldCheckRegistry) {
108
+ const fetched = await fetchLatestFromNpm();
109
+ if (fetched) {
110
+ latestVersion = fetched;
111
+ writeCache(latestVersion);
112
+ }
113
+ }
114
+
115
+ if (latestVersion && isNewerVersion(currentVersion, latestVersion)) {
116
+ printUpdateNotification(currentVersion, latestVersion);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Manual check command: gitsafty update
122
+ * @param {string} currentVersion
123
+ */
124
+ async function checkUpdateManual(currentVersion) {
125
+ console.log(`\n${colors.cyan}Checking npm registry for updates...${colors.reset}`);
126
+ const latest = await fetchLatestFromNpm();
127
+
128
+ if (!latest) {
129
+ console.log(`${colors.red}Could not fetch latest version from npm registry.${colors.reset}\n`);
130
+ return;
131
+ }
132
+
133
+ writeCache(latest);
134
+
135
+ if (isNewerVersion(currentVersion, latest)) {
136
+ printUpdateNotification(currentVersion, latest);
137
+ } else {
138
+ console.log(`\n${colors.green}${colors.bold}✔ You are on the latest version! (v${currentVersion})${colors.reset}\n`);
139
+ }
140
+ }
141
+
142
+ module.exports = {
143
+ isNewerVersion,
144
+ notifyIfUpdateAvailable,
145
+ checkUpdateManual,
146
+ printUpdateNotification
147
+ };