Skip to content

Commit 6af5dfa

Browse files
authored
Update hacker-library.js
1 parent 57c9931 commit 6af5dfa

File tree

1 file changed

+80
-35
lines changed

1 file changed

+80
-35
lines changed

hacker-library/hacker-library.js

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,107 @@
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-
71
const fs = require('fs');
82
const path = require('path');
93
const fetch = require('node-fetch');
4+
const { execSync } = require('child_process');
105

116
const args = process.argv.slice(2);
127
const action = args[0];
13-
148
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';
1510

1611
if (!fs.existsSync(libDir)) {
1712
fs.mkdirSync(libDir, { recursive: true });
1813
}
1914

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+
}
2146

2247
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+
});
2856
} else if (action === 'install') {
2957
const libname = args[1];
3058
if (!libname) {
3159
console.error('Usage: hacker-library install <libname>');
3260
process.exit(1);
3361
}
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) {
5283
console.error(`Error installing ${libname}: ${err.message}`);
5384
process.exit(1);
54-
});
85+
}
86+
});
5587
} else if (action === 'update') {
5688
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+
});
60105
});
61106
} else {
62107
console.error('Usage: hacker-library [list|install|update] [libname]');

0 commit comments

Comments
 (0)