UNPKG

@pact-toolbox/unplugin

Version:
50 lines (48 loc) 2.02 kB
import { prettyPrintError } from "./utils-caVmxe1_.js"; import { createPactToJSTransformer } from "./pactToJS-p3Xxm_Ew.js"; import "./transformer-_Vj4ar5p.js"; import "./api-DbnSyTTh.js"; import { getNetworkConfig, isLocalNetwork, resolveConfig } from "@pact-toolbox/config"; import { PactToolboxClient } from "@pact-toolbox/runtime"; import { logger, writeFile } from "@pact-toolbox/utils"; //#region src/loader.ts const cache = { resolvedConfig: void 0, client: void 0 }; const transformPactToJS = createPactToJSTransformer({ debug: process.env.DEBUG === "true" || process.env.DEBUG === "1" || process.env.NODE_ENV === "development" }); async function transformAndDeploy(id, src) { if (!cache.resolvedConfig) cache.resolvedConfig = await resolveConfig(); if (!cache.client) cache.client = new PactToolboxClient(cache.resolvedConfig); const { code, types, modules } = transformPactToJS(src); try { const client = cache.client; const isDeployed = modules.length > 0 ? (await Promise.all(modules.map((m) => client?.isContractDeployed(m.path)))).every(Boolean) : false; await writeFile(`${id}.d.ts`, types); const networkConfig = getNetworkConfig(cache.resolvedConfig); if (isLocalNetwork(networkConfig)) { logger.start(`[pactLoader] Deploying contract ${id} to ${networkConfig.name}`); await client.deployCode(src, { build: { upgrade: isDeployed, init: !isDeployed } }); logger.success(`[pactLoader] Successfully deployed contract ${id} to ${networkConfig.name}`); } return code; } catch (error) { prettyPrintError(`[pactLoader] Failed to deploy contract ${id}`, error); return code; } } function pactLoader(contents) { this.cacheable && this.cacheable(); const callback = this.async(); const id = this.resourcePath.replace(this.rootContext, ""); transformAndDeploy(id, contents).then((code) => { callback(null, code); }).catch(callback); } var loader_default = pactLoader; //#endregion export { loader_default as default, pactLoader }; //# sourceMappingURL=loader.js.map