git-userhub 3.0.7 → 3.0.9

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.
Files changed (2) hide show
  1. package/bin/git-user.js +13 -123
  2. package/package.json +4 -2
package/bin/git-user.js CHANGED
@@ -4,137 +4,27 @@ const { spawn } = require('child_process');
4
4
  const fs = require('fs');
5
5
  const path = require('path');
6
6
  const os = require('os');
7
- const https = require('https');
8
- const crypto = require('crypto');
9
- const tar = require('tar');
10
- const PKG_JSON = require('../package.json');
11
-
12
- const REPO = 'divyo-argha/git-user';
13
- const VERSION = `v${PKG_JSON.version}`;
14
7
 
15
8
  const platform = os.platform();
16
9
  const arch = os.arch();
17
-
18
- const platformMap = { 'darwin': 'darwin', 'linux': 'linux', 'win32': 'windows' };
19
- const archMap = { 'x64': 'x86_64', 'arm64': 'arm64' };
20
-
21
- const osName = platformMap[platform];
22
- const archName = archMap[arch];
23
10
  const ext = platform === 'win32' ? '.exe' : '';
24
11
 
25
- if (!osName || !archName) {
26
- console.error(`❌ Unsupported platform: ${platform} ${arch}`);
27
- process.exit(1);
28
- }
29
-
30
12
  const finalBinaryName = `git-user-${platform}-${arch}${ext}`;
31
13
  const finalBinaryPath = path.join(__dirname, finalBinaryName);
32
- const assetName = `git-user_${osName}_${archName}.tar.gz`;
33
-
34
- function runBinary() {
35
- const child = spawn(finalBinaryPath, process.argv.slice(2), {
36
- stdio: 'inherit',
37
- shell: false
38
- });
39
-
40
- child.on('exit', (code) => {
41
- process.exit(code || 0);
42
- });
43
-
44
- child.on('error', (err) => {
45
- console.error('❌ Failed to start git-user:', err.message);
46
- process.exit(1);
47
- });
48
- }
49
-
50
- if (fs.existsSync(finalBinaryPath)) {
51
- runBinary();
52
- return;
53
- }
54
-
55
- // FIRST RUN: Download binary
56
- console.log(`[git-user] First run detected. Downloading native binary for ${platform}-${arch} (~8MB)...`);
57
-
58
- function fetchJson(url) {
59
- return new Promise((resolve, reject) => {
60
- https.get(url, { headers: { 'User-Agent': 'node' } }, (res) => {
61
- if (res.statusCode !== 200) return reject(new Error(`API Error ${res.statusCode}`));
62
- let data = '';
63
- res.on('data', c => data += c);
64
- res.on('end', () => resolve(JSON.parse(data)));
65
- }).on('error', reject);
66
- });
67
- }
68
14
 
69
- function fetchFile(url, dest) {
70
- return new Promise((resolve, reject) => {
71
- https.get(url, { headers: { 'User-Agent': 'node' } }, (res) => {
72
- if (res.statusCode === 301 || res.statusCode === 302) {
73
- return fetchFile(res.headers.location, dest).then(resolve).catch(reject);
74
- }
75
- if (res.statusCode !== 200) return reject(new Error(`Download Error ${res.statusCode}`));
76
- const file = fs.createWriteStream(dest);
77
- res.pipe(file);
78
- file.on('finish', () => { file.close(); resolve(); });
79
- }).on('error', reject);
80
- });
81
- }
82
-
83
- function verifyHash(file, expected) {
84
- return new Promise((resolve, reject) => {
85
- const hash = crypto.createHash('sha256');
86
- const stream = fs.createReadStream(file);
87
- stream.on('data', d => hash.update(d));
88
- stream.on('end', () => {
89
- if (hash.digest('hex') !== expected) reject(new Error("Checksum mismatch!"));
90
- else resolve();
91
- });
92
- });
15
+ if (!fs.existsSync(finalBinaryPath)) {
16
+ console.error(`❌ git-user binary not found at ${finalBinaryPath}`);
17
+ console.error('Please reinstall the package: npm install -g git-userhub');
18
+ process.exit(1);
93
19
  }
94
20
 
95
- async function install() {
96
- try {
97
- const release = await fetchJson(`https://api.github.com/repos/${REPO}/releases/tags/${VERSION}`);
98
- const checksumAsset = release.assets.find(a => a.name.endsWith('checksums.txt'));
99
- const binAsset = release.assets.find(a => a.name === assetName);
100
-
101
- if (!checksumAsset || !binAsset) {
102
- throw new Error("Could not find required assets on GitHub Release");
103
- }
104
-
105
- const checksumPath = path.join(__dirname, 'checksums.txt');
106
- const archivePath = path.join(__dirname, assetName);
107
-
108
- await fetchFile(checksumAsset.browser_download_url, checksumPath);
109
- const checksums = fs.readFileSync(checksumPath, 'utf8');
110
- const hashLine = checksums.split('\n').find(l => l.endsWith(assetName));
111
- if (!hashLine) throw new Error("Checksum missing in txt file");
112
- const expectedHash = hashLine.split(/\s+/)[0];
113
-
114
- await fetchFile(binAsset.browser_download_url, archivePath);
115
- await verifyHash(archivePath, expectedHash);
21
+ const child = spawn(finalBinaryPath, process.argv.slice(2), {
22
+ stdio: 'inherit',
23
+ shell: false
24
+ });
116
25
 
117
- const binaryNameInArchive = platform === 'win32' ? 'git-user.exe' : 'git-user';
118
- await tar.extract({
119
- file: archivePath,
120
- cwd: __dirname,
121
- filter: (p) => p.replace(/^\.\//, '') === binaryNameInArchive
122
- });
123
-
124
- const extractedPath = path.join(__dirname, binaryNameInArchive);
125
- fs.renameSync(extractedPath, finalBinaryPath);
126
-
127
- if (platform !== 'win32') fs.chmodSync(finalBinaryPath, 0o755);
128
-
129
- fs.unlinkSync(archivePath);
130
- fs.unlinkSync(checksumPath);
131
-
132
- console.log(`[git-user] Installation complete.\n`);
133
- runBinary();
134
- } catch (err) {
135
- console.error(`\n❌ git-user installation failed: ${err.message}`);
136
- process.exit(1);
137
- }
138
- }
139
-
140
- install();
26
+ child.on('exit', (code) => process.exit(code || 0));
27
+ child.on('error', (err) => {
28
+ console.error('❌ Failed to start git-user:', err.message);
29
+ process.exit(1);
30
+ });
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "git-userhub",
3
- "version": "3.0.7",
3
+ "version": "3.0.9",
4
4
  "description": "Switch Git accounts in one command. No config editing. No SSH key chaos.",
5
5
  "bin": {
6
6
  "git-user": "bin/git-user.js"
7
7
  },
8
8
  "scripts": {
9
- "test": "echo \"No tests yet\" && exit 0"
9
+ "test": "echo \"No tests yet\" && exit 0",
10
+ "prepublishOnly": "node scripts/inject-hashes.js",
11
+ "postinstall": "node scripts/install.js"
10
12
  },
11
13
  "keywords": [
12
14
  "git",