cicy-code 0.1.0 → 0.1.2
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/cicy-code.js +86 -14
- package/package.json +1 -1
- package/scripts/install.js +12 -16
package/bin/cicy-code.js
CHANGED
|
@@ -1,23 +1,95 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const { spawn } = require('child_process');
|
|
2
|
+
const { spawn, execSync } = require('child_process');
|
|
3
3
|
const path = require('path');
|
|
4
4
|
const fs = require('fs');
|
|
5
|
+
const https = require('https');
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
const
|
|
7
|
+
const pkg = require('../package.json');
|
|
8
|
+
const binPath = path.join(__dirname, 'cicy-code');
|
|
8
9
|
|
|
9
|
-
const
|
|
10
|
-
const binPath = path.join(binDir, binName);
|
|
10
|
+
const cn = process.argv.includes('--cn') || process.env.CN_MIRROR === '1';
|
|
11
11
|
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
console.error('Run: npm install cicy-code');
|
|
15
|
-
process.exit(1);
|
|
12
|
+
if (process.argv.includes('--cn')) {
|
|
13
|
+
process.env.CN_MIRROR = '1';
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
if (cn) {
|
|
17
|
+
console.log(' [mirror] Using Chinese mirrors (npm + GitHub proxy)');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Check for updates
|
|
21
|
+
function checkUpdate() {
|
|
22
|
+
const registry = cn
|
|
23
|
+
? 'https://registry.npmmirror.com/cicy-code/latest'
|
|
24
|
+
: 'https://registry.npmjs.org/cicy-code/latest';
|
|
25
|
+
if (cn) console.log(` [mirror] Registry: registry.npmmirror.com`);
|
|
26
|
+
return new Promise((resolve) => {
|
|
27
|
+
https.get(registry, (res) => {
|
|
28
|
+
let data = '';
|
|
29
|
+
res.on('data', (c) => data += c);
|
|
30
|
+
res.on('end', () => {
|
|
31
|
+
try {
|
|
32
|
+
const latest = JSON.parse(data).version;
|
|
33
|
+
if (latest && latest !== pkg.version) {
|
|
34
|
+
resolve(latest);
|
|
35
|
+
} else {
|
|
36
|
+
resolve(null);
|
|
37
|
+
}
|
|
38
|
+
} catch { resolve(null); }
|
|
39
|
+
});
|
|
40
|
+
}).on('error', () => resolve(null));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function main() {
|
|
45
|
+
// Check update (non-blocking, timeout 3s)
|
|
46
|
+
const latest = await Promise.race([
|
|
47
|
+
checkUpdate(),
|
|
48
|
+
new Promise(r => setTimeout(() => r(null), 3000))
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
if (latest) {
|
|
52
|
+
console.log(`\n Update available: ${pkg.version} → ${latest}`);
|
|
53
|
+
console.log(` Updating...\n`);
|
|
54
|
+
try {
|
|
55
|
+
const npmCmd = cn
|
|
56
|
+
? `npm install -g cicy-code@${latest} --registry=https://registry.npmmirror.com`
|
|
57
|
+
: `npm install -g cicy-code@${latest}`;
|
|
58
|
+
execSync(npmCmd, { stdio: 'inherit' });
|
|
59
|
+
console.log(`\n Updated to ${latest}! Restarting...\n`);
|
|
60
|
+
// Re-exec with new version
|
|
61
|
+
const child = spawn('cicy-code', process.argv.slice(2), { stdio: 'inherit', env: process.env });
|
|
62
|
+
child.on('exit', (code) => process.exit(code || 0));
|
|
63
|
+
return;
|
|
64
|
+
} catch (e) {
|
|
65
|
+
console.log(` Update failed, running current version.\n`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Install globally if not already
|
|
70
|
+
try {
|
|
71
|
+
execSync('which cicy-code', { stdio: 'ignore' });
|
|
72
|
+
} catch {
|
|
73
|
+
console.log(' Installing cicy-code globally...');
|
|
74
|
+
try {
|
|
75
|
+
const npmCmd = cn
|
|
76
|
+
? 'npm install -g cicy-code --registry=https://registry.npmmirror.com'
|
|
77
|
+
: 'npm install -g cicy-code';
|
|
78
|
+
execSync(npmCmd, { stdio: 'inherit' });
|
|
79
|
+
console.log(' Installed! You can now run: cicy-code\n');
|
|
80
|
+
} catch {}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!fs.existsSync(binPath)) {
|
|
84
|
+
console.error('Binary not found. Reinstall: npm install -g cicy-code');
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const child = spawn(binPath, process.argv.slice(2), {
|
|
89
|
+
stdio: 'inherit',
|
|
90
|
+
env: process.env
|
|
91
|
+
});
|
|
92
|
+
child.on('exit', (code) => process.exit(code || 0));
|
|
93
|
+
}
|
|
22
94
|
|
|
23
|
-
|
|
95
|
+
main();
|
package/package.json
CHANGED
package/scripts/install.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
const https = require('https');
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const path = require('path');
|
|
4
|
-
const { execSync } = require('child_process');
|
|
5
4
|
|
|
6
|
-
const VERSION = '0.1.
|
|
7
|
-
const
|
|
5
|
+
const VERSION = '0.1.2';
|
|
6
|
+
const GH_URL = `https://github.com/cicy-ai/cicy-code/releases/download/v${VERSION}`;
|
|
7
|
+
const CN_URL = `https://gh-proxy.com/https://github.com/cicy-ai/cicy-code/releases/download/v${VERSION}`;
|
|
8
|
+
|
|
9
|
+
const cn = process.env.CN_MIRROR === '1' || process.argv.includes('--cn');
|
|
10
|
+
const BASE_URL = cn ? CN_URL : GH_URL;
|
|
8
11
|
|
|
9
12
|
const PLATFORMS = {
|
|
10
13
|
'darwin-arm64': 'cicy-code-darwin-arm64',
|
|
@@ -18,35 +21,28 @@ const binName = PLATFORMS[key];
|
|
|
18
21
|
|
|
19
22
|
if (!binName) {
|
|
20
23
|
console.error(`Unsupported platform: ${key}`);
|
|
21
|
-
console.error('Supported: darwin-arm64, darwin-x64, linux-x64, linux-arm64');
|
|
22
24
|
process.exit(1);
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
const binDir = path.join(__dirname, '..', 'bin');
|
|
26
|
-
const binPath = path.join(binDir,
|
|
28
|
+
const binPath = path.join(binDir, 'cicy-code');
|
|
27
29
|
const url = `${BASE_URL}/${binName}`;
|
|
28
30
|
|
|
29
|
-
console.log(`Downloading ${binName}...`);
|
|
31
|
+
console.log(`Downloading ${binName}${cn ? ' (CN mirror: gh-proxy.com)' : ''}...`);
|
|
32
|
+
console.log(` URL: ${url}`);
|
|
30
33
|
|
|
31
34
|
function download(url, dest, redirects = 5) {
|
|
32
35
|
return new Promise((resolve, reject) => {
|
|
33
36
|
if (redirects === 0) return reject(new Error('Too many redirects'));
|
|
34
|
-
|
|
35
37
|
const proto = url.startsWith('https') ? https : require('http');
|
|
36
38
|
proto.get(url, (res) => {
|
|
37
39
|
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
38
40
|
return download(res.headers.location, dest, redirects - 1).then(resolve).catch(reject);
|
|
39
41
|
}
|
|
40
|
-
if (res.statusCode !== 200) {
|
|
41
|
-
return reject(new Error(`HTTP ${res.statusCode}`));
|
|
42
|
-
}
|
|
42
|
+
if (res.statusCode !== 200) return reject(new Error(`HTTP ${res.statusCode}`));
|
|
43
43
|
const file = fs.createWriteStream(dest);
|
|
44
44
|
res.pipe(file);
|
|
45
|
-
file.on('finish', () => {
|
|
46
|
-
file.close();
|
|
47
|
-
fs.chmodSync(dest, 0o755);
|
|
48
|
-
resolve();
|
|
49
|
-
});
|
|
45
|
+
file.on('finish', () => { file.close(); fs.chmodSync(dest, 0o755); resolve(); });
|
|
50
46
|
}).on('error', reject);
|
|
51
47
|
});
|
|
52
48
|
}
|
|
@@ -55,6 +51,6 @@ download(url, binPath)
|
|
|
55
51
|
.then(() => console.log('Done!'))
|
|
56
52
|
.catch((err) => {
|
|
57
53
|
console.error('Download failed:', err.message);
|
|
58
|
-
console.error('
|
|
54
|
+
if (!cn) console.error('Try: CN_MIRROR=1 npx cicy-code');
|
|
59
55
|
process.exit(1);
|
|
60
56
|
});
|