|
1 | | -#!/usr/bin/env node |
2 | | - |
3 | | -// hacker-library.js - Library manager for Hacker Lang. |
4 | | -// Located at ~/.hacker-lang/bin/hacker-library. |
5 | | -// Handles install (downloads libraries), update (checks updates), list (shows available/installed). |
6 | | - |
7 | 1 | const fs = require('fs'); |
8 | 2 | const path = require('path'); |
9 | 3 | const fetch = require('node-fetch'); |
| 4 | +const { execSync } = require('child_process'); |
10 | 5 |
|
11 | 6 | const args = process.argv.slice(2); |
12 | 7 | const action = args[0]; |
13 | | - |
14 | 8 | const libDir = path.join(process.env.HOME, '.hacker-lang', 'libs'); |
| 9 | +const packageListUrl = 'https://raw.githubusercontent.com/HackerOS-Linux-System/Hacker-Lang/main/hacker-library/index-packages/package-list.hacker'; |
15 | 10 |
|
16 | 11 | if (!fs.existsSync(libDir)) { |
17 | 12 | fs.mkdirSync(libDir, { recursive: true }); |
18 | 13 | } |
19 | 14 |
|
20 | | -const availableLibs = ['util', 'net', 'crypto']; |
| 15 | +async function fetchPackageList() { |
| 16 | + try { |
| 17 | + const response = await fetch(packageListUrl); |
| 18 | + if (!response.ok) throw new Error(`HTTP ${response.status}`); |
| 19 | + const text = await response.text(); |
| 20 | + const lines = text.split('\n'); |
| 21 | + const packages = {}; |
| 22 | + let in_config = false; |
| 23 | + for (const line of lines) { |
| 24 | + const trimmed = line.trim(); |
| 25 | + if (trimmed === '[') { |
| 26 | + in_config = true; |
| 27 | + continue; |
| 28 | + } |
| 29 | + if (trimmed === ']') { |
| 30 | + in_config = false; |
| 31 | + continue; |
| 32 | + } |
| 33 | + if (in_config && trimmed.includes('=>')) { |
| 34 | + const [name, url] = trimmed.split('=>').map(s => s.trim()); |
| 35 | + if (name && url) { |
| 36 | + packages[name] = url; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + return packages; |
| 41 | + } catch (err) { |
| 42 | + console.error(`Error fetching package list: ${err.message}`); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | +} |
21 | 46 |
|
22 | 47 | if (action === 'list') { |
23 | | - console.log('Available libraries:'); |
24 | | - availableLibs.forEach(lib => console.log(`- ${lib}`)); |
25 | | - const installed = fs.existsSync(libDir) ? fs.readdirSync(libDir).filter(f => f.endsWith('.hacker')) : []; |
26 | | - console.log('\nInstalled libraries:'); |
27 | | - installed.forEach(lib => console.log(`- ${lib.replace('.hacker', '')}`)); |
| 48 | + console.log('Fetching available libraries...'); |
| 49 | + fetchPackageList().then(packages => { |
| 50 | + console.log('Available libraries:'); |
| 51 | + Object.keys(packages).forEach(lib => console.log(`- ${lib}`)); |
| 52 | + const installed = fs.existsSync(libDir) ? fs.readdirSync(libDir).filter(f => fs.lstatSync(path.join(libDir, f)).isDirectory()) : []; |
| 53 | + console.log('\nInstalled libraries:'); |
| 54 | + installed.forEach(lib => console.log(`- ${lib}`)); |
| 55 | + }); |
28 | 56 | } else if (action === 'install') { |
29 | 57 | const libname = args[1]; |
30 | 58 | if (!libname) { |
31 | 59 | console.error('Usage: hacker-library install <libname>'); |
32 | 60 | process.exit(1); |
33 | 61 | } |
34 | | - if (!availableLibs.includes(libname)) { |
35 | | - console.error(`Library ${libname} not found. Available: ${availableLibs.join(', ')}`); |
36 | | - process.exit(1); |
37 | | - } |
38 | | - const url = `https://example.com/hacker-lang/${libname}.hacker`; // Placeholder |
39 | | - const filePath = path.join(libDir, `${libname}.hacker`); |
40 | | - console.log(`Installing ${libname} to ${filePath}...`); |
41 | | - fetch(url) |
42 | | - .then(res => { |
43 | | - if (!res.ok) throw new Error(`HTTP ${res.status}`); |
44 | | - const file = fs.createWriteStream(filePath); |
45 | | - res.body.pipe(file); |
46 | | - file.on('finish', () => { |
47 | | - file.close(); |
48 | | - console.log(`Installed ${libname}`); |
49 | | - }); |
50 | | - }) |
51 | | - .catch(err => { |
| 62 | + fetchPackageList().then(packages => { |
| 63 | + if (!packages[libname]) { |
| 64 | + console.error(`Library ${libname} not found in package list.`); |
| 65 | + process.exit(1); |
| 66 | + } |
| 67 | + const repoUrl = packages[libname]; |
| 68 | + const libPath = path.join(libDir, libname); |
| 69 | + console.log(`Installing ${libname} from ${repoUrl}...`); |
| 70 | + try { |
| 71 | + if (fs.existsSync(libPath)) { |
| 72 | + console.log(`Removing existing ${libname}...`); |
| 73 | + execSync(`rm -rf ${libPath}`); |
| 74 | + } |
| 75 | + execSync(`git clone ${repoUrl} ${libPath}`, { stdio: 'inherit' }); |
| 76 | + if (!fs.existsSync(path.join(libPath, 'main.hacker'))) { |
| 77 | + console.error(`Library ${libname} missing main.hacker`); |
| 78 | + execSync(`rm -rf ${libPath}`); |
| 79 | + process.exit(1); |
| 80 | + } |
| 81 | + console.log(`Installed ${libname} to ${libPath}`); |
| 82 | + } catch (err) { |
52 | 83 | console.error(`Error installing ${libname}: ${err.message}`); |
53 | 84 | process.exit(1); |
54 | | - }); |
| 85 | + } |
| 86 | + }); |
55 | 87 | } else if (action === 'update') { |
56 | 88 | console.log('Checking for library updates...'); |
57 | | - availableLibs.forEach(lib => { |
58 | | - const filePath = path.join(libDir, `${lib}.hacker`); |
59 | | - console.log(`Update check for ${lib}: ${fs.existsSync(filePath) ? 'Up to date (placeholder)' : 'Not installed'}`); |
| 89 | + const installed = fs.existsSync(libDir) ? fs.readdirSync(libDir).filter(f => fs.lstatSync(path.join(libDir, f)).isDirectory()) : []; |
| 90 | + fetchPackageList().then(packages => { |
| 91 | + installed.forEach(lib => { |
| 92 | + if (packages[lib]) { |
| 93 | + const libPath = path.join(libDir, lib); |
| 94 | + console.log(`Updating ${lib}...`); |
| 95 | + try { |
| 96 | + execSync(`cd ${libPath} && git pull`, { stdio: 'inherit' }); |
| 97 | + console.log(`${lib} updated`); |
| 98 | + } catch (err) { |
| 99 | + console.error(`Error updating ${lib}: ${err.message}`); |
| 100 | + } |
| 101 | + } else { |
| 102 | + console.log(`${lib}: No update info available`); |
| 103 | + } |
| 104 | + }); |
60 | 105 | }); |
61 | 106 | } else { |
62 | 107 | console.error('Usage: hacker-library [list|install|update] [libname]'); |
|
0 commit comments