Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [16.x, 18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Main features are:
- Update/uninstall packages
- Any Node.js packages can be installed
- No special configuration is required
- Installed packages can have dependencies
- Installed packages can have dependencies
- dependencies are automatically installed
- optionalDependencies are also installed (errors are ignored)
- when updating a dependencies all dependents are reloaded
- Support for concurrency operation on filesystem (cloud/web farm scenario where file system is shared)
- A filesystem lock is used to prevent multiple instances to work on the same filesystem in the same moment
Expand Down
3 changes: 3 additions & 0 deletions dist/src/PackageInfo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export interface PackageJsonInfo extends PackageInfo {
dependencies?: {
[name: string]: string;
};
optionalDependencies?: {
[name: string]: string;
};
}
export interface PackageInfo {
name: string;
Expand Down
3 changes: 3 additions & 0 deletions dist/src/PluginInfo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export interface IPluginInfo {
readonly dependencies: {
[name: string]: string;
};
readonly optionalDependencies?: {
[name: string]: string;
};
readonly dependencyDetails?: {
[name: string]: PackageJsonInfo | undefined;
};
Expand Down
2 changes: 2 additions & 0 deletions dist/src/PluginManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export declare class PluginManager {
private installFromGithubLockFree;
private installFromBitbucketLockFree;
private installFromCodeLockFree;
private installDependency;
private listDependencies;
private installDependencies;
private linkDependencyToPlugin;
private unloadDependents;
Expand Down
76 changes: 50 additions & 26 deletions dist/src/PluginManager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/PluginManager.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/src/VersionManager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/VersionManager.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "live-plugin-manager",
"version": "1.0.0",
"version": "1.1.0",
"description": "Install and uninstall any node package at runtime from npm registry",
"keywords": [
"plugin",
Expand Down
1 change: 1 addition & 0 deletions src/PackageInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface PackageJsonInfo extends PackageInfo {
main?: string;
dependencies?: { [name: string]: string };
optionalDependencies?: { [name: string]: string };
}

export interface PackageInfo {
Expand Down
1 change: 1 addition & 0 deletions src/PluginInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IPluginInfo {
readonly name: string;
readonly version: string;
readonly dependencies: { [name: string]: string };
readonly optionalDependencies?: { [name: string]: string };
readonly dependencyDetails?: {
[name: string]: PackageJsonInfo | undefined;
}
Expand Down
97 changes: 60 additions & 37 deletions src/PluginManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as fs from "./fileSystem";
import * as path from "path";
import {NpmRegistryClient, NpmRegistryConfig} from "./NpmRegistryClient";
import {PluginVm} from "./PluginVm";
import {IPluginInfo} from "./PluginInfo";
import { NpmRegistryClient, NpmRegistryConfig } from "./NpmRegistryClient";
import { PluginVm } from "./PluginVm";
import { IPluginInfo } from "./PluginInfo";
import * as lockFile from "lockfile";
import * as semver from "semver";
import Debug from "debug";
Expand Down Expand Up @@ -80,7 +80,7 @@ export class PluginManager {
options.pluginsPath = path.join(options.cwd, "plugin_packages");
}

this.options = {...createDefaultOptions(), ...(options || {})};
this.options = { ...createDefaultOptions(), ...(options || {}) };
this.versionManager = new VersionManager({
cwd: this.options.cwd,
rootPath: this.options.versionsPath || path.join(this.options.pluginsPath, ".versions")
Expand Down Expand Up @@ -210,7 +210,7 @@ export class PluginManager {
}

require(fullName: string): any {
const {pluginName, requiredPath} = this.vm.splitRequire(fullName);
const { pluginName, requiredPath } = this.vm.splitRequire(fullName);

const info = this.getInfo(pluginName);
if (!info) {
Expand Down Expand Up @@ -519,7 +519,7 @@ export class PluginManager {
debug(`Create plugin ${name} to ${this.options.pluginsPath} from code`);
}

const location = this.versionManager.getPath({name, version});
const location = this.versionManager.getPath({ name, version });
if (!location) {
throw new Error(`Cannot resolve path for ${name}@${version}`);
}
Expand All @@ -533,53 +533,76 @@ export class PluginManager {
return this.addPlugin(pluginInfo);
}

private async installDependencies(plugin: IPluginInfo): Promise<{ [name: string]: string }> {
if (!plugin.dependencies) {
return {};
}

const dependencies: { [name: string]: string } = {};

for (const key in plugin.dependencies) {
if (!plugin.dependencies.hasOwnProperty(key)) {
continue;
}
if (this.shouldIgnore(key)) {
continue;
private async installDependency(plugin: IPluginInfo, name: string, version: string): Promise<{ installed: boolean, error?: Error }> {
try {
if (this.shouldIgnore(name)) {
return { installed: false };
}

const version = plugin.dependencies[key];

if (this.isModuleAvailableFromHost(key, version)) {
if (this.isModuleAvailableFromHost(name, version)) {
Comment thread
davideicardi marked this conversation as resolved.
if (debug.enabled) {
debug(`Installing dependencies of ${plugin.name}: ${key} is already available on host`);
debug(`Installing dependencies of ${plugin.name}: ${name} is already available on host`);
}
} else if (this.alreadyInstalled(key, version)) {
return { installed: true };
} else if (this.alreadyInstalled(name, version)) {
if (debug.enabled) {
debug(`Installing dependencies of ${plugin.name}: ${key} is already installed`);
debug(`Installing dependencies of ${plugin.name}: ${name} is already installed`);
}
const installed = await this.versionManager.resolvePath(key, version);
const installed = await this.versionManager.resolvePath(name, version);
if (!installed) {
throw new Error(`Cannot resolve path for ${key}@${version}`);
const error = new Error(`Cannot resolve path for ${name}@${version}`);
return { installed: false, error };
}
await this.linkDependencyToPlugin(plugin, key, installed);
await this.linkDependencyToPlugin(plugin, name, installed);
return { installed: true };
} else {
if (debug.enabled) {
debug(`Installing dependencies of ${plugin.name}: ${key} ...`);
debug(`Installing dependencies of ${plugin.name}: ${name} ...`);
}
const installedPlugin = await this.installLockFree(key, version);
const installedPlugin = await this.installLockFree(name, version);
const installed = await this.versionManager.resolvePath(installedPlugin.name, installedPlugin.version);
if (!installed) {
throw new Error(`Cannot resolve path for ${installedPlugin.name}@${installedPlugin.version}`);
const error = new Error(`Cannot resolve path for ${installedPlugin.name}@${installedPlugin.version}`);
return { installed: false, error };
}
await this.linkDependencyToPlugin(plugin, name, installed);
return { installed: true };
}
} catch (error) {
if (debug.enabled) {
debug(`Error installing dependency ${name} for ${plugin.name}:`, error);
}
const err = error instanceof Error ? error : new Error(String(error));
return { installed: false, error: err };
}
}

private listDependencies(plugin: IPluginInfo): { name: string, version: string, isOptional: boolean }[] {
const allDependenciesToInstall = Object.keys(plugin.dependencies).map((key) => ({ isOptional: false, name: key, version: plugin.dependencies[key] }));
if (plugin.optionalDependencies) {
const optDeps = plugin.optionalDependencies;
allDependenciesToInstall.push(...Object.keys(optDeps).map((key) => ({ isOptional: true, name: key, version: optDeps[key] })));
}

return allDependenciesToInstall;
}

private async installDependencies(plugin: IPluginInfo): Promise<{ [name: string]: string }> {
const installedDependencies: { [name: string]: string } = {};
for (const dep of this.listDependencies(plugin)) {
const installResult = await this.installDependency(plugin, dep.name, dep.version);
if (!installResult.installed && installResult.error) {
if (dep.isOptional) {
continue;
}
await this.linkDependencyToPlugin(plugin, key, installed);
throw installResult.error;
}

// NOTE: maybe here I should put the actual version?
dependencies[key] = version;
// NOTE: maybe here I should put the actual installed version?
installedDependencies[dep.name] = dep.version;
Comment thread
davideicardi marked this conversation as resolved.
}

return dependencies;
return installedDependencies;
}

private async linkDependencyToPlugin(plugin: IPluginInfo, packageName: string, versionPath: string) {
Expand Down Expand Up @@ -652,7 +675,7 @@ export class PluginManager {

// '/' is permitted to support scoped packages
if (name.startsWith(".")
|| name.indexOf("\\") >= 0) {
|| name.indexOf("\\") >= 0) {
return false;
}

Expand Down Expand Up @@ -906,7 +929,7 @@ export class PluginManager {
}

private async createPluginInfo(packageInfo: PackageInfo): Promise<IPluginInfo> {
const {name, version} = packageInfo;
const { name, version } = packageInfo;
const location = await this.versionManager.resolvePath(name, version);
if (!location) {
throw new Error(`Cannot resolve path for ${name}@${version}`);
Expand Down
Loading