@assert-equals/dappdriver
Version:
DappDriver is an e2e testing framework designed for testing decentralized applications (dApps) using MetaMask, Rainbow or Zerion
139 lines (138 loc) • 6.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveFiles = exports.findGithubAsset = exports.fileExists = exports.fetchGithubWorkflow = exports.fetchGithubTags = exports.fetchGithubRun = exports.fetchGithubRelease = exports.fetchGithubArtifact = exports.extractZipContents = exports.downloadArtifactZipFile = exports.downloadAssetZipFile = exports.createDirectory = exports.compareVersion = exports.checkEnvVariable = void 0;
const adm_zip_1 = __importDefault(require("adm-zip"));
const axios_1 = __importDefault(require("axios"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const semver_1 = __importDefault(require("semver"));
const constants_1 = require("../constants");
function checkEnvVariable(variable) {
const envVariable = process.env[variable];
if (!envVariable) {
throw new Error(`The environment variable ${variable} is not set. Please set it to your personal access token.\r\nYou can set it in your profile (e.g., ~/.bash_profile, ~/.zshrc): export ${variable}=your_personal_access_token`);
}
}
exports.checkEnvVariable = checkEnvVariable;
function compareVersion(wallet, version, recommendedVersions) {
const satisfy = semver_1.default.satisfies(version, recommendedVersions);
if (!satisfy) {
throw new Error(`This version (${version}) of ${wallet} is not supported. Please try version(s): ${recommendedVersions}.`);
}
}
exports.compareVersion = compareVersion;
function createDirectory(directory) {
if (fs_1.default.existsSync(directory) === false) {
fs_1.default.mkdirSync(directory, { recursive: true });
}
}
exports.createDirectory = createDirectory;
async function downloadAssetZipFile(asset, directory) {
const file = asset.name.endsWith('.zip') ? asset.name : `${asset.name}.zip`;
const zipFileName = path_1.default.join(directory, file);
const zipFile = await axios_1.default.get(asset.browser_download_url, {
responseType: 'arraybuffer'
});
fs_1.default.writeFileSync(zipFileName, zipFile.data);
return zipFileName;
}
exports.downloadAssetZipFile = downloadAssetZipFile;
async function downloadArtifactZipFile(artifact, directory) {
const file = artifact.name.endsWith('.zip') ? artifact.name : `${artifact.name}.zip`;
const zipFileName = path_1.default.join(directory, file);
const zipFile = await axios_1.default.get(artifact.archive_download_url, {
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
Accept: 'application/vnd.github.v3+json'
},
responseType: 'arraybuffer'
});
fs_1.default.writeFileSync(zipFileName, zipFile.data);
return zipFileName;
}
exports.downloadArtifactZipFile = downloadArtifactZipFile;
function extractZipContents(from) {
const to = from.substring(0, from.length - 4);
const zip = new adm_zip_1.default(from);
zip.extractAllTo(to, true);
return to;
}
exports.extractZipContents = extractZipContents;
async function fetchGithubArtifact(artifactName, run, githubApiUrl) {
const artifactsResponse = await axios_1.default.get(`${githubApiUrl}/actions/runs/${run.id}/artifacts`);
const artifacts = artifactsResponse.data.artifacts;
const artifact = artifacts.find((item) => item.name === artifactName);
if (!artifact) {
throw new Error(`Artifact with name ${artifactName} not found.`);
}
return artifact;
}
exports.fetchGithubArtifact = fetchGithubArtifact;
async function fetchGithubRelease(wallet, version, githubApiUrl) {
const releasesUrl = `${githubApiUrl}/releases`;
const response = await axios_1.default.get(releasesUrl);
const releases = response.data;
const release = releases.find((item) => item.tag_name === `v${version}`);
if (!release) {
throw new Error(`Could not find version (${version}) of ${wallet}, try updating the '${constants_1.PACKAGE_NAME}' package.`);
}
return release;
}
exports.fetchGithubRelease = fetchGithubRelease;
async function fetchGithubRun(version, workflowName, workflow, githubApiUrl) {
const runBranch = `rc-v${version}`;
const runsResponse = await axios_1.default.get(`${githubApiUrl}/actions/workflows/${workflow.id}/runs`);
const runs = runsResponse.data.workflow_runs;
const run = runs.find((item) => item.head_branch === runBranch);
if (!run) {
throw new Error(`No runs found for workflow ${workflowName} with branch ${runBranch}.`);
}
return run;
}
exports.fetchGithubRun = fetchGithubRun;
async function fetchGithubTags(wallet, version, githubApiUrl) {
const tagsUrl = `${githubApiUrl}/tags`;
const response = await axios_1.default.get(tagsUrl);
const tags = response.data;
const tag = tags.find((item) => item.name === `v${version}`);
if (!tag) {
throw new Error(`Could not find version (${version}) of ${wallet}, try updating the '${constants_1.PACKAGE_NAME}' package.`);
}
}
exports.fetchGithubTags = fetchGithubTags;
async function fetchGithubWorkflow(workflowName, githubApiUrl) {
const workflowsResponse = await axios_1.default.get(`${githubApiUrl}/actions/workflows`);
const workflows = workflowsResponse.data.workflows;
const workflow = workflows.find((item) => item.name === workflowName);
if (!workflow) {
throw new Error(`Workflow with name ${workflowName} not found.`);
}
return workflow;
}
exports.fetchGithubWorkflow = fetchGithubWorkflow;
function fileExists(path) {
return fs_1.default.existsSync(path);
}
exports.fileExists = fileExists;
function findGithubAsset(assetName, release) {
const file = assetName.endsWith('.zip') ? assetName : `${assetName}.zip`;
const asset = release.assets.find((item) => item.name === file);
return asset;
}
exports.findGithubAsset = findGithubAsset;
function moveFiles(destDir) {
const destDirs = destDir.split(path_1.default.sep);
const lastDirName = destDirs.pop();
const srcDir = path_1.default.join(destDir, lastDirName);
const files = fs_1.default.readdirSync(srcDir);
for (const file of files) {
const srcFilePath = path_1.default.join(srcDir, file);
const destFilePath = path_1.default.join(destDir, file);
fs_1.default.renameSync(srcFilePath, destFilePath);
}
fs_1.default.rmdirSync(srcDir);
}
exports.moveFiles = moveFiles;