Last active
April 6, 2026 11:31
-
-
Save weskerty/ac2556b2e1c8798639dde52c19ee1a34 to your computer and use it in GitHub Desktop.
NPM Install Helper Module 100% no broken bot trust me bro - npm pakage@version pakage@version etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ENV Change manager... Ej; HELPERCMD = "yarn add " | |
| const { bot, setVar, getVars } = require('../lib'); | |
| const { exec } = require('child_process'); | |
| const { promisify } = require('util'); | |
| const eP = promisify(exec); | |
| class HelperNPM { | |
| constructor() { | |
| this.installed = false; | |
| this.installing = false; | |
| } | |
| pName(pkg) { return pkg.split('@')[0]; } | |
| pPkg(env) { return (!env || !env.trim()) ? [] : env.split(',').map(p => p.trim()).filter(p => p); } | |
| async cI(pkg) { | |
| try { require.resolve(this.pName(pkg)); return true; } catch { return false; } | |
| } | |
| async gV(pkg) { | |
| try { | |
| const p = require.resolve(this.pName(pkg)); | |
| return require(p.replace(/\/[^\/]+$/, '/package.json')).version; | |
| } catch { return null; } | |
| } | |
| async iAll(packages, cmd) { | |
| if (!packages.length || this.installed || this.installing) return; | |
| this.installing = true; | |
| const missing = []; | |
| for (const pkg of packages) { | |
| if (!(await this.cI(pkg))) missing.push(pkg); | |
| } | |
| if (!missing.length) { this.installed = true; this.installing = false; return; } | |
| try { | |
| const baseCmd = cmd || `npm install --force --no-save --no-package-lock`; | |
| await eP(`${baseCmd} ${missing.join(' ')}`, { cwd: process.cwd() }); | |
| this.installed = true; | |
| for (const pkg of missing) { | |
| const v = await this.gV(pkg); | |
| console.log(`[NPM] OK ${this.pName(pkg)}${v ? ` ${v}` : ''}`); | |
| } | |
| } catch (e) { | |
| console.error(`[NPM] Error: ${e.message}`); | |
| } finally { this.installing = false; } | |
| } | |
| async gS(packages) { | |
| return Promise.all(packages.map(async pkg => ({ | |
| name: this.pName(pkg), | |
| version: await this.gV(pkg), | |
| installed: await this.cI(pkg) | |
| }))); | |
| } | |
| } | |
| const helper = new HelperNPM(); | |
| const pkgs = helper.pPkg(process.env.HELPERNPM || ''); | |
| if (pkgs.length) helper.iAll(pkgs, process.env.HELPERCMD || null).catch(() => {}); | |
| bot({ | |
| pattern: 'npm ?(.*)', | |
| fromMe: true, | |
| desc: 'NodeModules Installer', | |
| type: 'misc' | |
| }, async (message, match, ctx) => { | |
| const reply = t => message.send(t, { quoted: message.data }); | |
| const arg = (match || '').trim(); | |
| const vars = await getVars(message.id); | |
| const current = helper.pPkg(vars.HELPERNPM || ctx.HELPERNPM); | |
| if (!arg) { | |
| if (!current.length) return reply('npm <package@version> to install node modules'); | |
| const st = await helper.gS(current); | |
| let msg = 'Dependencias:\n\n'; | |
| for (const p of st) msg += `${p.installed ? 'OK' : 'NO'} *${p.name}*${p.version ? ` ${p.version}` : ''}\n`; | |
| if (helper.installing) msg += '\nInstalando...'; | |
| return reply(msg.trim()); | |
| } | |
| if (arg.startsWith('del ')) { | |
| const name = arg.slice(4).trim(); | |
| const before = current.length; | |
| const updated = current.filter(p => helper.pName(p) !== name); | |
| if (updated.length === before) return reply(`No encontrado: ${name}`); | |
| await setVar({ HELPERNPM: updated.join(',') }, message.id); | |
| return reply(`Removido: ${name}`); | |
| } | |
| const incoming = arg.trim().split(/\s+/); | |
| const merged = [...current]; | |
| const added = [], updated = []; | |
| for (const pkg of incoming) { | |
| const name = helper.pName(pkg); | |
| const idx = merged.findIndex(p => helper.pName(p) === name); | |
| if (idx === -1) { merged.push(pkg); added.push(pkg); } | |
| else if (merged[idx] !== pkg) { merged[idx] = pkg; updated.push(pkg); } | |
| } | |
| await setVar({ HELPERNPM: merged.join(',') }, message.id); | |
| let msg = ''; | |
| if (added.length) msg += `Agregado: ${added.join(', ')}\n`; | |
| if (updated.length) msg += `Actualizado: ${updated.join(', ')}\n`; | |
| if (!added.length && !updated.length) msg = 'Sin cambios.'; | |
| await reply(msg.trim()); | |
| if (added.length || updated.length) helper.iAll(added.concat(updated), ctx.HELPERCMD || null).catch(() => {}); | |
| }); | |
| module.exports = {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment