aboutsummaryrefslogtreecommitdiff
path: root/modules/ext/wpm.js
blob: 4dc9c299865adf38b1300d542916b5d0a72c497b (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class WpmModule {
    constructor() {
        this.commands = {};
        this.terminal = null;
        this.indexUrl = 'https://webx11.duckdns.org/spaces/newos4/packages.json';
        this.index = null;
        this.installed = this.loadInstalled();
    }
    getHelpInfo() {
        return { 'wpm': 'wpm <install|list|search|update|remove|info> [...] — Web Package Manager for installing and managing modules' };
    }
    loadInstalled() {
        try {
            const data = localStorage.getItem('webx11_wpm_installed');
            return data ? JSON.parse(data) : {};
        } catch (e) { return {}; }
    }
    saveInstalled() {
        localStorage.setItem('webx11_wpm_installed', JSON.stringify(this.installed));
    }
    registerCommands() {
        this.commands['wpm'] = (args) => this.handle(args);
    }
    handle(args) {
        if (!args[0]) return this.terminal.print('Usage: wpm install <pkg>, wpm list, wpm search <kw>, wpm update, wpm remove <pkg>, wpm info <pkg>', 'warning');
        const sub = args[0].toLowerCase();
        if (sub === 'install') this.install(args[1]);
        else if (sub === 'list') this.list();
        else if (sub === 'search') this.search(args.slice(1).join(' '));
        else if (sub === 'update') this.update();
        else if (sub === 'remove') this.remove(args[1]);
        else if (sub === 'info') this.info(args[1]);
        else this.terminal.print(`Unknown: ${sub}`, 'error');
    }
    async loadIndex() {
        if (this.index) return this.index;
        try {
            this.terminal.print('Fetching package index...', 'info');
            const res = await fetch(this.indexUrl);
            if (!res.ok) throw new Error('HTTP ' + res.status);
            this.index = await res.json();
            return this.index;
        } catch (e) {
            this.terminal.print(`Failed to load index: ${e.message}`, 'error');
            return null;
        }
    }
    async install(name) {
        if (!name) return this.terminal.print('Usage: wpm install <package>', 'warning');
        const idx = await this.loadIndex();
        if (!idx) return;
        const pkg = idx.packages[name];
        if (!pkg) return this.terminal.print(`Package "${name}" not found. Try: wpm search <keyword>`, 'error');
        if (this.installed[name]) return this.terminal.print(`Package "${name}" already installed. Use wpm update to reinstall.`, 'warning');
        this.terminal.print(`Installing ${pkg.name} v${pkg.version}...`, 'info');
        this.terminal.print(`  ${pkg.desc}`, 'info');
        const success = await this.terminal.moduleManager.loadModuleFromURL(pkg.url);
        if (success) {
            this.installed[name] = { version: pkg.version, installed: new Date().toISOString() };
            this.saveInstalled();
            this.terminal.print(`"${name}" installed successfully!`, 'success');
        }
    }
    async list() {
        const idx = await this.loadIndex();
        if (!idx) return;
        const names = Object.keys(this.installed);
        this.terminal.print('=== Installed Packages ===', 'info');
        if (names.length === 0) {
            this.terminal.print('  None. Use: wpm install <pkg>', 'info');
        } else {
            names.forEach(name => {
                const inst = this.installed[name];
                this.terminal.print(`  ${name} v${inst.version} (${inst.installed.split('T')[0]})`);
            });
        }
        this.terminal.print('\n=== Available Packages ===', 'info');
        Object.keys(idx.packages).sort().forEach(name => {
            const pkg = idx.packages[name];
            const installed = this.installed[name] ? ' ✅' : '';
            this.terminal.print(`  ${name} - ${pkg.desc}${installed}`);
        });
    }
    async search(keyword) {
        if (!keyword) return this.terminal.print('Usage: wpm search <keyword>', 'warning');
        const idx = await this.loadIndex();
        if (!idx) return;
        const kw = keyword.toLowerCase();
        const results = [];
        Object.entries(idx.packages).forEach(([name, pkg]) => {
            if (name.includes(kw) || pkg.desc.toLowerCase().includes(kw) || pkg.category.toLowerCase().includes(kw)) {
                results.push({ name, ...pkg });
            }
        });
        if (results.length === 0) return this.terminal.print(`No packages matching "${keyword}"`, 'info');
        this.terminal.print(`=== Search: "${keyword}" (${results.length} results) ===`, 'info');
        results.forEach(r => {
            const installed = this.installed[r.name] ? ' [installed]' : '';
            this.terminal.print(`  ${r.name} [${r.category}] - ${r.desc}${installed}`);
        });
        this.terminal.print('Use: wpm install <name> to install', 'info');
    }
    async update() {
        this.index = null;
        await this.loadIndex();
        if (!this.index) return;
        this.terminal.print('Index refreshed.', 'success');
        const outdated = [];
        Object.keys(this.installed).forEach(name => {
            const pkg = this.index.packages[name];
            if (pkg && pkg.version !== this.installed[name].version) {
                outdated.push(name);
            }
        });
        if (outdated.length > 0) {
            this.terminal.print(`Outdated packages: ${outdated.join(', ')}. Reinstall with "wpm install <name>"`, 'warning');
        }
    }
    async remove(name) {
        if (!name) return this.terminal.print('Usage: wpm remove <package>', 'warning');
        if (!this.installed[name]) return this.terminal.print(`Package "${name}" is not installed via wpm`, 'warning');
        const idx = await this.loadIndex();
        const className = idx && idx.packages[name] ? idx.packages[name].name.replace(/\s+/g, '') + 'Module' : null;
        if (className && this.terminal.moduleManager.unregisterModule(className)) {
            this.terminal.print(`Module ${className} unregistered`, 'info');
        }
        delete this.installed[name];
        this.saveInstalled();
        this.terminal.print(`Package "${name}" removed. If module persists, use: removemod <ClassName>`, 'success');
    }
    async info(name) {
        if (!name) return this.terminal.print('Usage: wpm info <package>', 'warning');
        const idx = await this.loadIndex();
        if (!idx) return;
        const pkg = idx.packages[name];
        if (!pkg) return this.terminal.print(`Package "${name}" not found`, 'error');
        this.terminal.print(`=== ${pkg.name} v${pkg.version} ===`, 'info');
        this.terminal.print(`Description: ${pkg.desc}`);
        this.terminal.print(`Category: ${pkg.category}`);
        this.terminal.print(`Commands: ${pkg.commands.join(', ')}`);
        this.terminal.print(`URL: ${pkg.url}`);
        const installed = this.installed[name];
        if (installed) this.terminal.print(`Installed: ${installed.installed}`, 'success');
        else this.terminal.print('Not installed. Use: wpm install ' + name, 'info');
    }
}
if (typeof window.webx11 !== 'undefined' && window.webx11) {
    webx11.moduleManager.registerModule(new WpmModule(), 'https://webx11.duckdns.org/spaces/newos4/modules/ext/wpm.js');
}