@arifbiswas/git-pre-push 1.0.17 → 1.0.18

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/README.md CHANGED
@@ -30,6 +30,14 @@ Whenever `git push` is invoked—from terminal, VS Code UI, or any Git GUI—it
30
30
 
31
31
  ## 🚀 Quick Start
32
32
 
33
+ > **Install globally (`-g`).** `npm i @arifbiswas/git-pre-push` without `-g` only puts GitSafty into that project's `node_modules`: there is no `gitsafty` command and other repos stay unprotected. npm does not let a package install itself globally, so when GitSafty notices it is running from a project (or from `npx`) it asks *"Install GitSafty globally now? (Y/n)"* and does it for you. The recommended command is:
34
+ >
35
+ > ```bash
36
+ > npm install -g @arifbiswas/git-pre-push
37
+ > gitsafty init --global
38
+ > ```
39
+
40
+
33
41
  ### Option 1: Automatic Setup via npm (Recommended)
34
42
 
35
43
  Add `git-pre-push` to your project:
package/bin/cli.js CHANGED
@@ -63,6 +63,17 @@ async function main() {
63
63
  } catch (_) {}
64
64
  }
65
65
 
66
+ // Installed as a project dependency (`npm i` without -g)? Offer the global
67
+ // install once per command run (remembered for 30 days when declined).
68
+ if (!hookCommands.includes(command) && command !== 'init' && command !== 'install') {
69
+ try {
70
+ const un = require('../src/updateNotifier');
71
+ if (un.looksLikeProjectCopy() && un.installKind() === 'local-dependency') {
72
+ await un.offerPermanentInstall(['init', '--global'], { kind: 'local-dependency', remember: true });
73
+ }
74
+ } catch (_) {}
75
+ }
76
+
66
77
  // `gitsafty update` does its own check below.
67
78
  if (!hookCommands.includes(command) && command !== 'update' && command !== 'upgrade') {
68
79
  // Opening the menu always asks the registry, so a fresh release is never missed.
@@ -317,7 +328,8 @@ async function main() {
317
328
  console.log(`${colors.cyan}Your git environment is now protected!${colors.reset}`);
318
329
  console.log(`${colors.dim}Every git push, clone and pull${isGlobalFlag ? ' on this PC' : ' in this repository'} is scanned for malware & secret leaks.${colors.reset}\n`);
319
330
  const { offerPermanentInstall } = require('../src/updateNotifier');
320
- await offerPermanentInstall(['init', ...(isGlobalFlag ? ['--global'] : [])]);
331
+ // npx copy: re-run the same init globally. Project copy: protect the whole PC.
332
+ await offerPermanentInstall(['init', '--global']);
321
333
  } else {
322
334
  console.error(`\n${colors.red}${colors.bold}✖ ${result.message}${colors.reset}\n`);
323
335
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arifbiswas/git-pre-push",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
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": {
@@ -20,6 +20,12 @@ function main() {
20
20
  console.log(`[git-safty] 🛡️ Global hooks updated (push + clone + pull malware scan) in ${refreshed}`);
21
21
  }
22
22
 
23
+ if (!isGlobalInstall) {
24
+ // npm can't make a package install globally on its own; say it clearly.
25
+ console.log('[git-safty] Installed in this project only. To protect every repo on this PC and get the `gitsafty` command:');
26
+ console.log('[git-safty] npm install -g @arifbiswas/git-pre-push && gitsafty init --global');
27
+ }
28
+
23
29
  // When installed as a dependency, process.env.INIT_CWD points to the parent project
24
30
  const targetDir = process.env.INIT_CWD || process.cwd();
25
31
 
package/src/installer.js CHANGED
@@ -348,7 +348,9 @@ function isPermanentGlobalInstall(cli = CLI_PATH) {
348
348
  return /\/node_modules\/@arifbiswas\/git-pre-push\/bin\/cli\.js$/.test(cli) &&
349
349
  !/\/_npx\//.test(cli) &&
350
350
  !/\/node_modules\/.+\/node_modules\//.test(cli) &&
351
- /\/lib\/node_modules\/|\/npm\/node_modules\/|\/nodejs\/node_modules\/|^[A-Za-z]:\/.*\/node_modules\//.test(cli);
351
+ // unix prefix (nvm, Homebrew, /usr/local), Windows npm prefix (AppData/Roaming/npm),
352
+ // Program Files/nodejs, nvm-windows (AppData/Roaming/nvm/v20.x/node_modules).
353
+ /\/lib\/node_modules\/|\/npm\/node_modules\/|\/nodejs\/node_modules\/|\/nvm\/v[0-9][^/]*\/node_modules\//i.test(cli);
352
354
  }
353
355
 
354
356
  /**
@@ -275,17 +275,44 @@ function globalCliPath() {
275
275
  * re-runs init with the permanent copy so the hooks point there.
276
276
  * @param {string[]} initArgs e.g. ['init', '--global']
277
277
  */
278
- async function offerPermanentInstall(initArgs) {
279
- if (installKind() !== 'npx') return;
278
+ /**
279
+ * Offers to install GitSafty globally when this copy is not a global install:
280
+ * - npx: runs from a temporary cache, no `gitsafty` command, hooks point
281
+ * into that cache (which npm may delete)
282
+ * - project dependency (`npm i @arifbiswas/git-pre-push` without -g):
283
+ * only this project has it, no `gitsafty` command, other repos unprotected
284
+ * npm can't make a package install globally by itself, so GitSafty asks.
285
+ * Yes runs `npm install -g` and then `init` with the global copy.
286
+ * @param {string[]} initArgs init arguments to re-run with the global copy
287
+ * @param {{ kind?: string, remember?: boolean }} [opts]
288
+ */
289
+ async function offerPermanentInstall(initArgs, opts = {}) {
290
+ const kind = opts.kind || installKind();
291
+ if (kind !== 'npx' && kind !== 'local-dependency') return;
280
292
  const isWin = process.platform === 'win32';
281
- console.log(`${colors.yellow}${colors.bold}⚠ You ran GitSafty with npx.${colors.reset}`);
282
- console.log(` npx only runs it from a temporary cache: the ${colors.bold}gitsafty${colors.reset} command is not installed,`);
283
- console.log(` and the hooks point into that cache, which npm may delete later.`);
284
- console.log(` ${colors.dim}Install it permanently:${colors.reset} ${colors.cyan}npm install -g ${PACKAGE_NAME}${colors.reset} ${colors.dim}then${colors.reset} ${colors.cyan}gitsafty ${initArgs.join(' ')}${colors.reset}\n`);
293
+ const cache = readCache() || {};
294
+ const declinedRecently = opts.remember && cache.declinedGlobalAt && Date.now() - cache.declinedGlobalAt < 30 * 24 * 60 * 60 * 1000;
295
+ if (declinedRecently) return;
296
+
297
+ if (kind === 'npx') {
298
+ console.log(`${colors.yellow}${colors.bold}⚠ You ran GitSafty with npx.${colors.reset}`);
299
+ console.log(` npx only runs it from a temporary cache: the ${colors.bold}gitsafty${colors.reset} command is not installed,`);
300
+ console.log(` and the hooks point into that cache, which npm may delete later.`);
301
+ } else {
302
+ console.log(`${colors.yellow}${colors.bold}⚠ GitSafty is installed in this project only${colors.reset} ${colors.dim}(${path.resolve(__dirname, '..')})${colors.reset}`);
303
+ console.log(` \`npm i\` without -g puts it in node_modules: no ${colors.bold}gitsafty${colors.reset} command, and other repos on this PC are not protected.`);
304
+ }
305
+ console.log(` ${colors.dim}Install it for the whole PC:${colors.reset} ${colors.cyan}npm install -g ${PACKAGE_NAME}${colors.reset} ${colors.dim}then${colors.reset} ${colors.cyan}gitsafty ${initArgs.join(' ')}${colors.reset}\n`);
285
306
 
286
307
  if (!isInteractive() || process.env.GITSAFTY_NO_UPDATE_PROMPT === '1') return;
287
- const yes = await askYesNo(`${colors.bold}${colors.cyan}? Install GitSafty permanently now (adds the gitsafty command)? (Y/n): ${colors.reset}`);
288
- if (!yes) return;
308
+ const yes = await askYesNo(`${colors.bold}${colors.cyan}? Install GitSafty globally now (gitsafty command + protect every repo)? (Y/n): ${colors.reset}`);
309
+ if (!yes) {
310
+ if (opts.remember) {
311
+ writeCache(cache.latestVersion || null, { declinedGlobalAt: Date.now() });
312
+ console.log(`${colors.dim}OK, I won't ask again for 30 days.${colors.reset}\n`);
313
+ }
314
+ return;
315
+ }
289
316
  if (!installVersion('latest')) return;
290
317
 
291
318
  const cli = globalCliPath();
@@ -303,9 +330,24 @@ async function offerPermanentInstall(initArgs) {
303
330
  console.log('');
304
331
  }
305
332
 
333
+ /**
334
+ * Fast check (no npm call) whether this copy might be a project dependency.
335
+ * Confirmed with installKind() before asking anything.
336
+ */
337
+ function looksLikeProjectCopy() {
338
+ const cli = path.resolve(__dirname, '..', 'bin', 'cli.js').replace(/\\/g, '/');
339
+ if (!/\/node_modules\/@arifbiswas\/git-pre-push\/bin\/cli\.js$/.test(cli) || /\/_npx\//.test(cli)) return false;
340
+ try {
341
+ return !require('./installer').isPermanentGlobalInstall(cli);
342
+ } catch (_) {
343
+ return true;
344
+ }
345
+ }
346
+
306
347
  module.exports = {
307
348
  isNewerVersion,
308
349
  offerPermanentInstall,
350
+ looksLikeProjectCopy,
309
351
  globalCliPath,
310
352
  installKind,
311
353
  offerUpdate,