@moonsong-labs/moonwall-cli
Version:
Testing framework for the Moon family of projects
168 lines (166 loc) • 7.46 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/lib/binaries.ts
var binaries_exports = {};
__export(binaries_exports, {
BINARY_DIRECTORY: () => BINARY_DIRECTORY,
RUNTIME_DIRECTORY: () => RUNTIME_DIRECTORY,
SPECS_DIRECTORY: () => SPECS_DIRECTORY,
generatePlainSpecs: () => generatePlainSpecs,
generateRawSpecs: () => generateRawSpecs,
getGithubReleaseBinary: () => getGithubReleaseBinary,
getMoonbeamDockerBinary: () => getMoonbeamDockerBinary,
getMoonbeamReleaseBinary: () => getMoonbeamReleaseBinary,
getPlainSpecsFromTag: () => getPlainSpecsFromTag,
getPolkadotReleaseBinary: () => getPolkadotReleaseBinary,
getRawSpecsFromTag: () => getRawSpecsFromTag,
getRuntimeWasm: () => getRuntimeWasm,
getTagSha8: () => getTagSha8
});
module.exports = __toCommonJS(binaries_exports);
var import_api_augment = require("@moonbeam-network/api-augment");
var import_path = __toESM(require("path"), 1);
var import_fs = __toESM(require("fs"), 1);
var import_child_process = __toESM(require("child_process"), 1);
var import_moonwall_util = require("@moonsong-labs/moonwall-util");
var BINARY_DIRECTORY = process.env.BINARY_DIRECTORY || "binaries";
var RUNTIME_DIRECTORY = process.env.RUNTIME_DIRECTORY || "runtimes";
var SPECS_DIRECTORY = process.env.SPECS_DIRECTORY || "specs";
async function getGithubReleaseBinary(url, binaryPath) {
if (!import_fs.default.existsSync(binaryPath)) {
console.log(` Missing ${binaryPath} locally, downloading it...`);
import_child_process.default.execSync(
`mkdir -p ${import_path.default.dirname(binaryPath)} && wget -q ${url} -O ${binaryPath} && chmod u+x ${binaryPath}`
);
console.log(`${binaryPath} downloaded !`);
}
return binaryPath;
}
async function getMoonbeamReleaseBinary(binaryTag) {
const binaryPath = import_path.default.join(BINARY_DIRECTORY, `moonbeam-${binaryTag}`);
return getGithubReleaseBinary(
`https://github.com/PureStake/moonbeam/releases/download/${binaryTag}/moonbeam`,
binaryPath
);
}
async function getPolkadotReleaseBinary(binaryTag) {
const binaryPath = import_path.default.join(BINARY_DIRECTORY, `polkadot-${binaryTag}`);
return getGithubReleaseBinary(
`https://github.com/paritytech/polkadot/releases/download/${binaryTag}/polkadot`,
binaryPath
);
}
async function getTagSha8(binaryTag) {
const sha = import_child_process.default.execSync(`git rev-list -1 ${binaryTag}`).toString();
if (!sha) {
throw new Error(`Invalid runtime tag ${binaryTag}`);
return;
}
return sha.slice(0, 8);
}
async function getMoonbeamDockerBinary(binaryTag) {
const sha8 = await getTagSha8(binaryTag);
const binaryPath = import_path.default.join(BINARY_DIRECTORY, `moonbeam-${sha8}`);
if (!import_fs.default.existsSync(binaryPath)) {
if (process.platform != "linux") {
console.error(`docker binaries are only supported on linux.`);
process.exit(1);
}
const dockerImage = `purestake/moonbeam:sha-${sha8}`;
console.log(` Missing ${binaryPath} locally, downloading it...`);
import_child_process.default.execSync(`mkdir -p ${import_path.default.dirname(binaryPath)} && docker create --pull always --name moonbeam-tmp ${dockerImage} && docker cp moonbeam-tmp:/moonbeam/moonbeam ${binaryPath} && docker rm moonbeam-tmp`);
console.log(`${binaryPath} downloaded !`);
}
return binaryPath;
}
async function getRuntimeWasm(runtimeName, runtimeTag, localPath) {
const runtimePath = import_path.default.join(
RUNTIME_DIRECTORY,
`${runtimeName}-${runtimeTag}.wasm`
);
if (!import_fs.default.existsSync(RUNTIME_DIRECTORY)) {
import_fs.default.mkdirSync(RUNTIME_DIRECTORY, { recursive: true });
}
if (runtimeTag == "local") {
const builtRuntimePath = localPath ? localPath : import_path.default.join(
import_moonwall_util.OVERRIDE_RUNTIME_PATH || `../target/release/wbuild/${runtimeName}-runtime/`,
`${runtimeName}_runtime.compact.compressed.wasm`
);
const code = import_fs.default.readFileSync(builtRuntimePath);
import_fs.default.writeFileSync(runtimePath, `0x${code.toString("hex")}`);
} else if (!import_fs.default.existsSync(runtimePath)) {
console.log(` Missing ${runtimePath} locally, downloading it...`);
import_child_process.default.execSync(
`mkdir -p ${import_path.default.dirname(runtimePath)} && wget -q https://github.com/PureStake/moonbeam/releases/download/${runtimeTag}/${runtimeName}-${runtimeTag}.wasm -O ${runtimePath}.bin`
);
const code = import_fs.default.readFileSync(`${runtimePath}.bin`);
import_fs.default.writeFileSync(runtimePath, `0x${code.toString("hex")}`);
console.log(`${runtimePath} downloaded !`);
}
return runtimePath;
}
async function getPlainSpecsFromTag(runtimeName, tag) {
const binaryPath = await getMoonbeamDockerBinary(tag);
return generateSpecs(binaryPath, runtimeName, false);
}
async function getRawSpecsFromTag(runtimeName, tag) {
const binaryPath = await getMoonbeamDockerBinary(tag);
return generateSpecs(binaryPath, runtimeName, true);
}
async function generateSpecs(binaryPath, runtimeName, raw) {
const specPath = import_path.default.join(
SPECS_DIRECTORY,
`${runtimeName}-${raw ? "raw" : "plain"}-specs.json`
);
import_child_process.default.execSync(
`mkdir -p ${import_path.default.dirname(specPath)} && ${binaryPath} build-spec --chain ${runtimeName} ${raw ? "--raw" : ""} --disable-default-bootnode > ${specPath}`
);
return specPath;
}
async function generatePlainSpecs(binaryPath, runtimeName) {
return generateSpecs(binaryPath, runtimeName, false);
}
async function generateRawSpecs(binaryPath, runtimeName) {
return generateSpecs(binaryPath, runtimeName, true);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BINARY_DIRECTORY,
RUNTIME_DIRECTORY,
SPECS_DIRECTORY,
generatePlainSpecs,
generateRawSpecs,
getGithubReleaseBinary,
getMoonbeamDockerBinary,
getMoonbeamReleaseBinary,
getPlainSpecsFromTag,
getPolkadotReleaseBinary,
getRawSpecsFromTag,
getRuntimeWasm,
getTagSha8
});