UNPKG

@kubiklabs/wasmkit

Version:

Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.

104 lines (103 loc) 4.13 kB
"use strict"; 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.resolveBuilderRegisterPath = exports.runScript = void 0; const debug_1 = __importDefault(require("debug")); const path = __importStar(require("path")); const errors_1 = require("../core/errors"); const errors_list_1 = require("../core/errors-list"); const log = (0, debug_1.default)("wasmkit:core:scripts-runner"); // eslint-disable-next-line async function loadScript(relativeScriptPath) { const absoluteScriptPath = path.join(process.cwd(), relativeScriptPath); try { return require(absoluteScriptPath); } catch (err) { throw new errors_1.WasmkitError(errors_list_1.ERRORS.GENERAL.SCRIPT_LOAD_ERROR, { script: absoluteScriptPath, error: err.message }); } } /** Returns error line number and position at line attached with path. * @param error Error * @param scriptPath relative path to script where error occured */ function attachLineNumbertoScriptPath( // eslint-disable-next-line error, scriptPath) { const stackTraces = error.stack.split('\n'); for (const trace of stackTraces) { const line = trace?.split(scriptPath.concat(':'))[1]?.slice(0, -1); // extract line number if (line) { const [lineNo, position] = line.split(':'); return scriptPath.concat(`:Line:${lineNo},Position:${position}`); } } return scriptPath; } // eslint-disable-next-line function displayErr(error, relativeScriptPath) { if (error instanceof errors_1.WasmkitError) { throw error; } const relativeScriptPathWithLine = attachLineNumbertoScriptPath(error, relativeScriptPath); throw new errors_1.WasmkitError(errors_list_1.ERRORS.BUILTIN_TASKS.RUN_SCRIPT_ERROR, { script: relativeScriptPathWithLine, error: error.message }, error); } async function runScript(relativeScriptPath, runtimeEnv) { if (relativeScriptPath.endsWith('.ts')) { relativeScriptPath = path.join('build', relativeScriptPath.split('.ts')[0] + '.js'); } log(`Running ${relativeScriptPath}.default()`); const requiredScript = await loadScript(relativeScriptPath); if (!requiredScript.default) { throw new errors_1.WasmkitError(errors_list_1.ERRORS.GENERAL.NO_DEFAULT_EXPORT_IN_SCRIPT, { script: relativeScriptPath }); } try { await requiredScript.default(runtimeEnv); } catch (error) { displayErr(error, relativeScriptPath); } } exports.runScript = runScript; /** * Ensure wasmkit/register source file path is resolved to compiled JS file * instead of TS source file, so we don't need to run ts-node unnecessarily. */ function resolveBuilderRegisterPath() { const wasmKitCoreBaseDir = path.join(__dirname, "..", "..", ".."); return path.join(wasmKitCoreBaseDir, "dist/register.js"); } exports.resolveBuilderRegisterPath = resolveBuilderRegisterPath;