简体   繁体   中英

Using npm as custom plugin manager?

Think of sublime text, where you can install or uninstall plugins. I want that for my app, and I want to use npm/github to do it.

Maybe I'll require that your package starts with myapp- to be considered a plugin for my app. How can I search npm based on that, and also install/update packages into the folder I want (not node_modules) and ideally it should work even if the person doesn't have npm installed (using an http api?).

Plugins for my app go into plugins/plugin-name folder, all I need to do is download their git source into that folder

I have created a project to solve similar problems. See live-plugin-manager .

You can install, uninstall and load plugins from npm at runtime.

import {PluginManager} from "live-plugin-manager";
import * as path from "path";

const manager = new PluginManager({
  pluginsPath: path.join(__dirname, "plugins")
});

async function run() {
  await manager.installFromNpm("moment");

  const moment = manager.require("moment");
  console.log(moment().format());

  await manager.uninstall("moment");
}

run();

In the above code I install moment package at runtime, load and execute it. Here I have used typescript, but the same can be written with plain javascript.

Plugins are installed inside the directory specified in the PluginManager constructor or in the plugins directory if not specified.

I just created a great module, for enhancements like your proposal: https://www.npmjs.com/package/@kawix/core

You can read the README.md, for try understand the usage, i will add a basic example:

> npm install -g @kawix/core
> kwcore  "https://raw.githubusercontent.com/voxsoftware/kawix-core/master/example/npmrequire/express.js"

And this is the content of file https://raw.githubusercontent.com/voxsoftware/kawix-core/master/example/npmrequire/express.js

// this will download the npm module and make a local cache
import express from 'npm://express@^4.16.4'


var app = express() 
app.get('/', function (req, res) {
  res.send('Hello World')
}) 
app.listen(3000)
console.log("Listening on 3000")

https://api-docs.npms.io/ has an http API for searching npm packages.

Which can be used like this: https://api.npms.io/v2/search?q=keywords:myapp to get all plugins for myapp

To actually download/install an npm package into any folder, I found an npm package called download-npm-package that lets me do it in code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM