git-userhub 3.0.4 → 3.0.7

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/git-user.js CHANGED
@@ -4,21 +4,19 @@ 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}`;
7
14
 
8
- // Detect platform and architecture
9
15
  const platform = os.platform();
10
16
  const arch = os.arch();
11
17
 
12
- const platformMap = {
13
- 'darwin': 'darwin',
14
- 'linux': 'linux',
15
- 'win32': 'windows'
16
- };
17
-
18
- const archMap = {
19
- 'x64': 'amd64',
20
- 'arm64': 'arm64'
21
- };
18
+ const platformMap = { 'darwin': 'darwin', 'linux': 'linux', 'win32': 'windows' };
19
+ const archMap = { 'x64': 'x86_64', 'arm64': 'arm64' };
22
20
 
23
21
  const osName = platformMap[platform];
24
22
  const archName = archMap[arch];
@@ -29,26 +27,114 @@ if (!osName || !archName) {
29
27
  process.exit(1);
30
28
  }
31
29
 
32
- const binaryName = `git-user-${osName}-${archName}${ext}`;
33
- const binaryPath = path.join(__dirname, binaryName);
30
+ const finalBinaryName = `git-user-${platform}-${arch}${ext}`;
31
+ const finalBinaryPath = path.join(__dirname, finalBinaryName);
32
+ const assetName = `git-user_${osName}_${archName}.tar.gz`;
34
33
 
35
- if (!fs.existsSync(binaryPath)) {
36
- console.error(`❌ git-user binary not found at ${binaryPath}`);
37
- console.error(` Please ensure the package was correctly installed.`);
38
- process.exit(1);
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;
39
53
  }
40
54
 
41
- // Forward all arguments to the bundled binary
42
- const child = spawn(binaryPath, process.argv.slice(2), {
43
- stdio: 'inherit',
44
- shell: false
45
- });
55
+ // FIRST RUN: Download binary
56
+ console.log(`[git-user] First run detected. Downloading native binary for ${platform}-${arch} (~8MB)...`);
46
57
 
47
- child.on('exit', (code) => {
48
- process.exit(code || 0);
49
- });
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
+ }
50
68
 
51
- child.on('error', (err) => {
52
- console.error('❌ Failed to start git-user:', err.message);
53
- process.exit(1);
54
- });
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
+ });
93
+ }
94
+
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);
116
+
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();
package/package.json CHANGED
@@ -1,16 +1,12 @@
1
1
  {
2
2
  "name": "git-userhub",
3
- "version": "3.0.4",
3
+ "version": "3.0.7",
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",
10
- "prepublishOnly": "node scripts/download-binaries.js"
11
- },
12
- "devDependencies": {
13
- "tar": "^7.4.3"
9
+ "test": "echo \"No tests yet\" && exit 0"
14
10
  },
15
11
  "keywords": [
16
12
  "git",
@@ -47,5 +43,8 @@
47
43
  ],
48
44
  "config": {
49
45
  "repo": "divyo-argha/git-user"
46
+ },
47
+ "dependencies": {
48
+ "tar": "^7.5.16"
50
49
  }
51
- }
50
+ }
Binary file
Binary file
Binary file
Binary file
Binary file