@kubiklabs/wasmkit
Version:
Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.
139 lines (138 loc) • 6.04 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readPackageJson = exports.loadPluginFile = exports.usePlugin = void 0;
const debug_1 = __importDefault(require("debug"));
const path = __importStar(require("path"));
const semver = __importStar(require("semver"));
const errors_1 = require("../core/errors");
const errors_list_1 = require("../core/errors-list");
const execution_mode_1 = require("./execution-mode");
const log = (0, debug_1.default)("wasmkit:core:plugins");
/**
* Validates a plugin dependencies and loads it.
* @param pluginName - The plugin name
* @param WasmkitContext - The WasmkitContext
* @param from - Where to resolve plugins and dependencies from. Only for
* testing purposes.
*/
function usePlugin(WasmkitContext, pluginName, from) {
log("Loading plugin %s", pluginName);
// We have a special case for `ExecutionMode.EXECUTION_MODE_LINKED`
//
// If WasmKit is linked, a require without `from` would be executed in the
// context of WasmKit, and not find any plugin (linked or not). We workaround
// this by using the CWD here.
//
// This is not ideal, but the only reason to link WasmKit is testing.
if (from === undefined &&
(0, execution_mode_1.getExecutionMode)() === execution_mode_1.ExecutionMode.EXECUTION_MODE_LINKED) {
from = process.cwd();
log("WasmKit is linked, searching for plugin starting from CWD", from);
}
let globalFlag = "";
let globalWarning = "";
if ((0, execution_mode_1.getExecutionMode)() === execution_mode_1.ExecutionMode.EXECUTION_MODE_GLOBAL_INSTALLATION) {
globalFlag = " --global";
globalWarning =
"You are using a global installation of WasmKit. Plugins and their dependencies must also be global.\n";
}
const pluginPackageJson = readPackageJson(pluginName, from);
if (pluginPackageJson === undefined) {
const installExtraFlags = globalFlag;
throw new errors_1.WasmkitError(errors_list_1.ERRORS.PLUGINS.NOT_INSTALLED, {
plugin: pluginName,
extraMessage: globalWarning,
extraFlags: installExtraFlags
});
}
// We use the package.json's version of the name, as it is normalized.
pluginName = pluginPackageJson.name;
if (WasmkitContext.loadedPlugins.includes(pluginName)) {
return;
}
if (pluginPackageJson.peerDependencies !== undefined) {
checkPeerDependencies(pluginPackageJson.peerDependencies, pluginName, from, globalFlag, globalWarning);
}
const options = from !== undefined ? { paths: [from] } : undefined;
const pluginPath = require.resolve(pluginName, options);
loadPluginFile(pluginPath);
WasmkitContext.setPluginAsLoaded(pluginName);
}
exports.usePlugin = usePlugin;
function checkPeerDependencies(deps, pluginName, from, flag, warning) {
for (const [dependencyName, versionSpec] of Object.entries(deps)) {
const dependencyPackageJson = readPackageJson(dependencyName, from);
let installExtraFlags = flag;
if (versionSpec.match(/^[0-9]/) !== null) {
installExtraFlags += " --save-exact";
}
if (dependencyPackageJson === undefined) {
throw new errors_1.WasmkitError(errors_list_1.ERRORS.PLUGINS.MISSING_DEPENDENCIES, {
plugin: pluginName,
dependency: dependencyName,
extraMessage: warning,
extraFlags: installExtraFlags,
versionSpec
});
}
const installedVersion = dependencyPackageJson.version;
if (!semver.satisfies(installedVersion, versionSpec, {
includePrerelease: true
})) {
throw new errors_1.WasmkitError(errors_list_1.ERRORS.PLUGINS.DEPENDENCY_VERSION_MISMATCH, {
plugin: pluginName,
dependency: dependencyName,
extraMessage: warning,
extraFlags: installExtraFlags,
versionSpec,
installedVersion
});
}
}
}
function loadPluginFile(absolutePluginFilePath) {
log("Loading plugin file %s", absolutePluginFilePath);
const imported = require(absolutePluginFilePath); // eslint-disable-line @typescript-eslint/no-var-requires
const plugin = imported.default !== undefined ? imported.default : imported;
if (typeof plugin === "function") {
plugin();
}
}
exports.loadPluginFile = loadPluginFile;
function readPackageJson(packageName, from) {
try {
const options = from !== undefined ? { paths: [from] } : undefined;
const packageJsonPath = require.resolve(path.join(packageName, "package.json"), options);
return require(packageJsonPath);
}
catch (error) {
return undefined;
}
}
exports.readPackageJson = readPackageJson;