-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.cjs
More file actions
107 lines (88 loc) · 3.04 KB
/
install.cjs
File metadata and controls
107 lines (88 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const os = require('os');
const { spawnSync } = require('child_process');
// Detect platform and architecture
const platform = os.platform();
const arch = os.arch();
// Map Node.js platform/arch to our naming convention
const platformMap = {
'linux-x64': 'linux-x64',
'linux-arm64': 'linux-arm64',
'darwin-x64': 'darwin-x64',
'darwin-arm64': 'darwin-arm64',
'win32-x64': 'win32-x64',
};
const key = platform + '-' + arch;
const platformKey = platformMap[key];
const npmjsRegistry = 'https://registry.npmjs.org/';
if (!platformKey) {
console.error('Unsupported platform: ' + platform + ' ' + arch);
process.exit(1);
}
// Try to find the binary from the platform-specific package
const packageName = '@dongowu/git-ai-cli-' + platformKey;
const nodeModulesPath = path.join(__dirname, 'node_modules');
const binaryName = platform === 'win32' ? 'git-ai.exe' : 'git-ai';
const binaryPath = path.join(nodeModulesPath, packageName, 'bin', binaryName);
function getRootPackageVersion() {
try {
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
return pkg.version;
} catch (err) {
console.warn('Warning: Failed to read package version: ' + err.message);
return 'latest';
}
}
function installPlatformPackageFromNpmjs(packageSpec) {
const npmExecPath = process.env.npm_execpath;
let command = 'npm';
const args = [];
if (npmExecPath && npmExecPath.endsWith('.js')) {
command = process.execPath;
args.push(npmExecPath);
} else if (npmExecPath && fs.existsSync(npmExecPath)) {
command = npmExecPath;
}
args.push(
'install',
'--no-save',
'--no-package-lock',
'--ignore-scripts',
'--registry',
npmjsRegistry,
packageSpec
);
const result = spawnSync(command, args, {
cwd: __dirname,
stdio: 'inherit',
env: process.env,
});
return result.status === 0;
}
// Check if binary exists
if (!fs.existsSync(binaryPath)) {
console.warn('Warning: Binary not found at ' + binaryPath);
const packageVersion = getRootPackageVersion();
const packageSpec = packageName + '@' + packageVersion;
console.warn('Trying to install ' + packageSpec + ' from ' + npmjsRegistry + '...');
const installed = installPlatformPackageFromNpmjs(packageSpec);
if (!installed || !fs.existsSync(binaryPath)) {
const detectedRegistry = process.env.npm_config_registry || 'unknown';
console.error('Failed to install ' + packageSpec + '.');
console.error('Detected npm registry: ' + detectedRegistry);
console.error('Please retry with: npm install -g @dongowu/git-ai-cli --registry=' + npmjsRegistry);
process.exit(1);
}
console.log('✅ Installed missing platform package: ' + packageSpec);
}
// Make binary executable on Unix-like systems
if (process.platform !== 'win32') {
try {
fs.chmodSync(binaryPath, 0o755);
} catch (err) {
console.warn('Warning: Could not make binary executable: ' + err.message);
}
}
console.log('✅ git-ai binary installed for ' + platformKey);