@kubiklabs/wasmkit
Version:
Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.
55 lines (54 loc) • 2.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.persistCheckpoint = exports.loadFromYamlFileSilent = exports.toMap = exports.loadCheckpoint = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const yaml_1 = __importDefault(require("yaml"));
function loadCheckpoint(checkpointName) {
const checkpoints = loadFromYamlFileSilent(checkpointName, { mapAsMap: false });
for (const k of Object.keys(checkpoints)) {
if (checkpoints[k]?.metadata) {
checkpoints[k].metadata = toMap(checkpoints[k].metadata);
}
}
return checkpoints;
}
exports.loadCheckpoint = loadCheckpoint;
// http://xahlee.info/js/js_object_to_map_datatype.html
function toMap(obj) {
const mp = new Map();
Object.keys(obj).forEach(k => { mp.set(k, obj[k]); });
return mp;
}
exports.toMap = toMap;
function loadFromYamlFileSilent(filePath, options) {
// Try-catch is the way:
// https://nodejs.org/docs/latest/api/fs.html#fs_fs_stat_path_options_callback
// Instead, user code should open/read/write the file directly and
// handle the error raised if the file is not available
try {
return readYAML(filePath, options);
}
catch (e) {
return defaultYamlValue(options);
}
}
exports.loadFromYamlFileSilent = loadFromYamlFileSilent;
function readYAML(filePath, options) {
return yaml_1.default.parse(fs_1.default.readFileSync(filePath).toString(), options);
}
function defaultYamlValue(options) {
if (options?.mapAsMap) {
return new Map(); // eslint-disable-line @typescript-eslint/no-explicit-any
}
return {};
}
function persistCheckpoint(contractPath, checkpoint) {
const scriptDir = path_1.default.dirname(contractPath);
fs_1.default.mkdirSync(scriptDir, { recursive: true });
fs_1.default.writeFileSync(contractPath, yaml_1.default.stringify(checkpoint));
}
exports.persistCheckpoint = persistCheckpoint;