UNPKG

@kubiklabs/wasmkit

Version:

Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.

128 lines (127 loc) 5.13 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchRepository = exports.copyTemplatetoDestination = exports.setUpTempDirectory = void 0; const chalk_1 = __importDefault(require("chalk")); const download_git_repo_1 = __importDefault(require("download-git-repo")); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const tmp_1 = __importDefault(require("tmp")); const util_1 = require("util"); /** * Creates a temporary directory in /tmp to download templates * eg. tmp-86625-iMiAxZVeTy1U/templates */ async function setUpTempDirectory() { const options = { unsafeCleanup: true }; try { const tmpDir = tmp_1.default.dirSync(options); return { path: path_1.default.join(tmpDir.name, "templates"), cleanupCallback: tmpDir.removeCallback }; } catch (error) { console.error('Failed to initialize'); throw error; } } exports.setUpTempDirectory = setUpTempDirectory; /** * Creates a prompt for each overwriting file, and confirms overwrite. * if overwrite is confirmed the file is deleted * @param contentCollisions Colliding files * @param destination location to copy template from tempDir */ async function promptOverwrites(contentCollisions, destination) { const { default: enquirer } = await Promise.resolve().then(() => __importStar(require("enquirer"))); const overwriteContents = []; let response; for (const file of contentCollisions) { console.log(chalk_1.default.yellow(`${file} already exists in this directory..`)); const overwriteToggle = [ { type: "Toggle", name: "overwrite", message: `Overwrite ${file}?`, enabled: 'Yes', disabled: 'No' } ]; response = await enquirer.prompt(overwriteToggle); if (response.overwrite) { fs_extra_1.default.removeSync(`${destination}/${file}`); overwriteContents.push(file); } } return overwriteContents; } /** * Copies template files from tempory directory to destination directory. * If --force is not used, user is asked by a promt to overwrite colliding files or not * @param tmpDir template directory to copy from * @param destination directory to copy template to * @param force if true, the colliding files are overwritten by default */ async function copyTemplatetoDestination(tmpDir, destination, force) { fs_extra_1.default.ensureDirSync(destination); const templateContents = fs_extra_1.default.readdirSync(tmpDir); const destinationContents = fs_extra_1.default.readdirSync(destination); const newContents = templateContents.filter(filename => !destinationContents.includes(filename)); const contentCollisions = templateContents.filter(filename => destinationContents.includes(filename)); let shouldCopy; if (force) { shouldCopy = templateContents; } else { const overwriteContents = await promptOverwrites(contentCollisions, destination); shouldCopy = [...newContents, ...overwriteContents]; } for (const file of shouldCopy) { fs_extra_1.default.copySync(`${tmpDir}/${file}`, `${destination}/${file}`); } } exports.copyTemplatetoDestination = copyTemplatetoDestination; /** * Downloads repo from git url to destination path * @param url git url (<organization/repo>) * @param destination location to download repo */ async function fetchRepository(url, destination) { try { // eslint-disable-next-line @typescript-eslint/no-misused-promises await (0, util_1.promisify)(download_git_repo_1.default)(`direct:${url}`, destination, { clone: true }); } catch (error) { console.error(`Failed to initialize ${url}`); throw error; } } exports.fetchRepository = fetchRepository;