UNPKG

@taqueria/protocol

Version:

A TypeScript package which contains types that are to be shared between @taqueria/node-sdk and @taqueria/taqueria.

382 lines (381 loc) • 14.4 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // types-config-files.ts var types_config_files_exports = {}; __export(types_config_files_exports, { readConfigFiles: () => readConfigFiles, readJsonFileInterceptConfig: () => readJsonFileInterceptConfig, transformConfigFileV1ToConfigFileSetV2: () => transformConfigFileV1ToConfigFileSetV2, transformConfigFileV2ToConfig: () => transformConfigFileV2ToConfig, transformConfigToConfigFileV2: () => transformConfigToConfigFileV2, writeConfigFiles: () => writeConfigFiles, writeJsonFileInterceptConfig: () => writeJsonFileInterceptConfig }); module.exports = __toCommonJS(types_config_files_exports); var readJsonFileInterceptConfig = (readJsonFile) => async (filePath) => { if (filePath.endsWith(`.taq/config.json`)) { return transformConfigFileV2ToConfig(await readConfigFiles(readJsonFile)(filePath)); } return readJsonFile(filePath); }; var readConfigFiles = (readJsonFile) => async (configFilePath) => { var _a; const configFileObj = await readJsonFile(configFilePath); if (configFileObj.version !== `v2`) { const configFileSetV2 = transformConfigFileV1ToConfigFileSetV2(configFileObj); return configFileSetV2; } const configFileV2 = configFileObj; const envFiles = await Promise.all( Object.keys((_a = configFileV2.environments) != null ? _a : {}).map(async (envName) => { try { return { key: envName, value: await readJsonFile( configFilePath.replace(`config.json`, `config.local.${envName}.json`) ) }; } catch { return {}; } }) ); return { config: configFileV2, environments: Object.fromEntries(envFiles.filter((x) => x.value).map((x) => [x.key, x.value])) }; }; var writeJsonFileInterceptConfig = (writeJsonFile) => (filePath) => { if (filePath.endsWith(`.taq/config.json`)) { return async (config) => { return await writeConfigFiles(writeJsonFile)(filePath)(transformConfigToConfigFileV2(config)); }; } return writeJsonFile(filePath); }; var writeConfigFiles = (writeJsonFile) => (configFilePath) => async (configFileSetV2) => { const configFileResult = await writeJsonFile(configFilePath)(configFileSetV2.config); await Promise.all( Object.entries(configFileSetV2.environments).map(async ([envName, value]) => { await writeJsonFile(configFilePath.replace(`config.json`, `config.local.${envName}.json`))(value); }) ); return configFileResult; }; var removeUndefinedFields = (x) => { return JSON.parse(JSON.stringify(x)); }; var transformConfigFileV1ToConfigFileSetV2 = (configFileV1) => { var _a, _b; const config = configFileV1; const configFileV2 = { version: `v2`, language: config.language, metadata: config.metadata, artifactsDir: config.artifactsDir, contractsDir: config.contractsDir, accounts: !config.accounts ? void 0 : Object.fromEntries( Object.entries(config.accounts).map(([k, v]) => [k, { balance: { amount: v, units: `mutez` } }]) ), contracts: config.contracts, environmentDefault: (_a = config.environment) == null ? void 0 : _a.default, environments: Object.fromEntries( Object.entries((_b = config.environment) != null ? _b : {}).filter(([k, v]) => k !== `default`).map(([k, v]) => [k, v]).map(([k, v]) => [k, { // Known fields type: v.sandboxes.length ? `flextesa` : `simple`, // Unknown fields ...(() => { var _a2, _b2, _c, _d, _e, _f, _g, _h; const vClone = { ...v }; delete vClone.networks; delete vClone.sandboxes; delete vClone.aliases; if (v.aliases) vClone.contracts = v.aliases; if ((_a2 = v.sandboxes) == null ? void 0 : _a2[0]) { const sandboxName = v.sandboxes[0]; if ((_b2 = config.sandbox) == null ? void 0 : _b2[sandboxName].accounts) { const accountsClone = { ...config.sandbox[sandboxName].accounts }; delete accountsClone["default"]; vClone.accounts = accountsClone; if ((_e = (_d = (_c = config.sandbox) == null ? void 0 : _c[sandboxName]) == null ? void 0 : _d.accounts) == null ? void 0 : _e["default"]) { vClone.accountDefault = (_h = (_g = (_f = config.sandbox) == null ? void 0 : _f[sandboxName]) == null ? void 0 : _g.accounts) == null ? void 0 : _h["default"]; } } } return vClone; })(), // Preserve sandbox or network name networkName: v.networks[0], sandboxName: v.sandboxes[0], // Fields from the first sandbox or network (there should be only 1) // These overwrite fields in environment ...[ ...v.networks.map((k2) => { var _a2; return (_a2 = config.network) == null ? void 0 : _a2[k2]; }), ...v.sandboxes.map((k2) => { var _a2; const retval = { ...(_a2 = config.sandbox) == null ? void 0 : _a2[k2] }; delete retval["accounts"]; return retval; }) ][0] }]) ), plugins: config.plugins }; const environmentsV2 = {}; return removeUndefinedFields({ config: configFileV2, environments: environmentsV2 }); }; var transformConfigToConfigFileV2 = (config) => { var _a, _b; const environmentsV2Raw = Object.fromEntries( Object.entries(config.environment).filter(([k]) => k !== `default`).map(([k, v]) => [k, v]).map(([k, v]) => [k, { // Known fields type: v.sandboxes.length ? `flextesa` : `simple`, // Unknown fields ...(() => { var _a2, _b2, _c, _d, _e; const vClone = { ...v }; delete vClone.networks; delete vClone.sandboxes; delete vClone.aliases; if (v.aliases) vClone.contracts = v.aliases; if ((_a2 = v.sandboxes) == null ? void 0 : _a2[0]) { const sandboxName = v.sandboxes[0]; if ((_c = (_b2 = config.sandbox) == null ? void 0 : _b2[sandboxName]) == null ? void 0 : _c.accounts) { const accountsClone = { ...config.sandbox[sandboxName].accounts }; delete accountsClone["default"]; vClone.accounts = accountsClone; if ((_d = config.sandbox[sandboxName].accounts) == null ? void 0 : _d["default"]) { vClone.accountDefault = (_e = config.sandbox[sandboxName].accounts) == null ? void 0 : _e["default"]; } } } return vClone; })(), // Preserve sandbox or network name networkName: v.networks[0], sandboxName: v.sandboxes[0], // Fields from the first sandbox or network (there should be only 1) // These overwrite fields in environment ...[ ...v.networks.map((k2) => { var _a2; return (_a2 = config.network) == null ? void 0 : _a2[k2]; }), ...v.sandboxes.map((k2) => { var _a2; const retval = { ...(_a2 = config.sandbox) == null ? void 0 : _a2[k2] }; delete retval["accounts"]; return retval; }) ][0] }]) ); const configFileV2 = { version: `v2`, language: config.language, metadata: config.metadata, artifactsDir: config.artifactsDir, contractsDir: config.contractsDir, accounts: !config.accounts ? void 0 : Object.fromEntries( Object.entries(config.accounts).map(([k, v]) => [k, { balance: { amount: v, units: `mutez` } }]) ), contracts: config.contracts, environmentDefault: config.environment.default, environments: environmentsV2Raw, plugins: config.plugins }; const environmentsV2 = {}; for (const [envName, eMain] of Object.entries((_a = configFileV2.environments) != null ? _a : {})) { const eLocal = environmentsV2[envName] = { ...eMain }; for (const k of Object.keys(eMain)) { const key = k; if (key === `type`) { delete eLocal[key]; continue; } if (eMain.type === "flextesa" || eMain.type === "tezbox") { if (k === "label" || k === "protocol") { delete eLocal[key]; continue; } if (k === "rpcUrl") { continue; } } if (eMain.type === "simple") { if (k === "label" || k === "protocol" || k === "rpcUrl") { delete eLocal[key]; continue; } } if (k == "aliases") { eLocal["contracts"] = Object.entries((_b = eLocal[key]) != null ? _b : {}).reduce( (retval, [key2, value]) => ({ ...retval, [key2]: value }), {} ); delete eLocal[key]; } delete eMain[key]; } continue; } return removeUndefinedFields({ config: configFileV2, environments: environmentsV2 }); }; var transformConfigFileV2ToConfig = (configFileSetV2) => { var _a, _b, _c; const { config: configFileV2, environments: environmentFilesV2 } = configFileSetV2; const environments = Object.entries((_a = configFileV2.environments) != null ? _a : {}).map(([k, v]) => { var _a2; return { key: k, value: { ...v, // merge in the fields from the envFile ...(_a2 = environmentFilesV2[k]) != null ? _a2 : {} } }; }); const getUnknownFields = (x, structure) => { if (structure === "environment") { return {}; } const unknownFields = (() => { const vClone = { ...x.value }; delete vClone.type; delete vClone.networkName; delete vClone.sandboxName; delete vClone.label; delete vClone.rpcUrl; delete vClone.storage; delete vClone.aliases; delete vClone.contracts; delete vClone.accounts; return vClone; })(); return unknownFields; }; const simpleEnvironments = environments.filter((x) => x.value.type === `simple`); const sandboxEnvironments = environments.filter((x) => x.value.type !== "simple"); const config = { // Common fields ...(() => { const vClone = { ...configFileV2 }; delete vClone.version; delete vClone.environmentDefault; delete vClone.environments; return vClone; })(), // Transformed fields accounts: !configFileV2.accounts ? void 0 : Object.fromEntries( Object.entries(configFileV2.accounts).map(([k, v]) => [k, `${v.balance.amount}`]) ), environment: { default: (_c = configFileV2.environmentDefault) != null ? _c : (_b = environments[0]) == null ? void 0 : _b.key, ...Object.fromEntries(environments.map((x) => { var _a2, _b2; return [x.key, { // Network and sandbox networks: x.value.type !== `simple` ? [] : [ // use same name as enviroment by default (_a2 = x.value.networkName) != null ? _a2 : `${x.key}` ], sandboxes: x.value.type !== `flextesa` ? [] : [ // use same name as enviroment by default (_b2 = x.value.sandboxName) != null ? _b2 : `${x.key}` ], // Known environment fields storage: x.value.storage, aliases: x.value.contracts, // Unknown fields might need to be in the environment ...getUnknownFields(x, "environment") }]; })) }, network: !simpleEnvironments.length ? void 0 : Object.fromEntries(simpleEnvironments.map((x) => { var _a2, _b2, _c2; return [(_a2 = x.value.networkName) != null ? _a2 : `${x.key}`, { label: (_b2 = x.value.label) != null ? _b2 : ``, rpcUrl: (_c2 = x.value.rpcUrl) != null ? _c2 : ``, // Unknown fields might need to be in the network or sandbox ...getUnknownFields(x, "network"), ...["accounts", "contracts"].reduce( (retval, fieldName) => { if (fieldName === "accounts") { return x.value.accounts ? { ...retval, accounts: x.value.accounts } : retval; } else if (fieldName === "contracts") { return x.value.aliases ? { ...retval, contracts: x.value.aliases } : retval; } return retval; }, {} ) }]; })), sandbox: !sandboxEnvironments.length ? void 0 : Object.fromEntries(sandboxEnvironments.map((x) => { var _a2, _b2, _c2; return [(_a2 = x.value.sandboxName) != null ? _a2 : `${x.key}`, { label: (_b2 = x.value.label) != null ? _b2 : ``, rpcUrl: (_c2 = x.value.rpcUrl) != null ? _c2 : ``, // Add the following lines to preserve blockTime and baking // @ts-ignore ...x.value.blockTime ? { blockTime: x.value.blockTime } : {}, // @ts-ignore ...x.value.baking ? { baking: x.value.baking } : {}, // ... rest of the existing code ... // Unknown fields might need to be in the network or sandbox ...getUnknownFields(x, "sandbox"), ...(() => { const environment = x.value; if (environment && environment.accounts) { return environment.accountDefault ? { accounts: { ...environment.accounts, default: environment.accountDefault } } : { accounts: { ...environment.accounts, default: Object.keys(environment.accounts)[0] } }; } return {}; })() }]; })) }; return removeUndefinedFields(config); }; //# sourceMappingURL=types-config-files.js.map