UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

101 lines (100 loc) 4.32 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizePath = exports.removeReactTmpDir = exports.createEmptyTmpReleaseFolder = exports.fileDoesNotExistOrIsDirectory = exports.generateRandomFilename = exports.getLastFolderInPath = exports.moveReleaseFilesInTmpFolder = exports.copyFileToTmpDir = exports.isDirectory = exports.isBinaryOrZip = void 0; const fs = require("fs"); const path = require("path"); const os = require("os"); const rimraf = require("rimraf"); const temp = require("temp"); const pfs = require("../../../util/misc/promisfied-fs"); function isBinaryOrZip(path) { return path.search(/\.zip$/i) !== -1 || path.search(/\.apk$/i) !== -1 || path.search(/\.ipa$/i) !== -1; } exports.isBinaryOrZip = isBinaryOrZip; function isDirectory(path) { return fs.statSync(path).isDirectory(); } exports.isDirectory = isDirectory; function copyFileToTmpDir(filePath) { if (!isDirectory(filePath)) { const outputFolderPath = temp.mkdirSync("code-push"); rimraf.sync(outputFolderPath); fs.mkdirSync(outputFolderPath); const outputFilePath = path.join(outputFolderPath, path.basename(filePath)); fs.writeFileSync(outputFilePath, fs.readFileSync(filePath)); return outputFolderPath; } } exports.copyFileToTmpDir = copyFileToTmpDir; function moveReleaseFilesInTmpFolder(updateContentsPath) { return __awaiter(this, void 0, void 0, function* () { let tmpUpdateContentsPath = temp.mkdirSync("code-push"); tmpUpdateContentsPath = path.join(tmpUpdateContentsPath, "CodePush"); fs.mkdirSync(tmpUpdateContentsPath); if (isDirectory(updateContentsPath)) { yield pfs.cp(normalizePath(updateContentsPath), normalizePath(tmpUpdateContentsPath)); } else { const targetFileName = path.parse(updateContentsPath).base; yield pfs.cpFile(normalizePath(updateContentsPath), path.join(tmpUpdateContentsPath, targetFileName)); } return tmpUpdateContentsPath; }); } exports.moveReleaseFilesInTmpFolder = moveReleaseFilesInTmpFolder; function getLastFolderInPath(path) { const splittedPath = normalizePath(path) .split("/") .filter((el) => { return el !== ""; }); if (isDirectory(path)) { return splittedPath[splittedPath.length - 1]; } else { return splittedPath[splittedPath.length - 2]; } } exports.getLastFolderInPath = getLastFolderInPath; function generateRandomFilename(length) { let filename = ""; const validChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (let i = 0; i < length; i++) { // eslint-disable-next-line no-restricted-properties filename += validChar.charAt(Math.floor(Math.random() * validChar.length)); } return filename; } exports.generateRandomFilename = generateRandomFilename; function fileDoesNotExistOrIsDirectory(path) { try { return isDirectory(path); } catch (error) { return true; } } exports.fileDoesNotExistOrIsDirectory = fileDoesNotExistOrIsDirectory; function createEmptyTmpReleaseFolder(folderPath) { rimraf.sync(folderPath); fs.mkdirSync(folderPath); } exports.createEmptyTmpReleaseFolder = createEmptyTmpReleaseFolder; function removeReactTmpDir() { rimraf.sync(`${os.tmpdir()}/react-*`); } exports.removeReactTmpDir = removeReactTmpDir; function normalizePath(filePath) { //replace all backslashes coming from cli running on windows machines by slashes return filePath.replace(/\\/g, "/"); } exports.normalizePath = normalizePath;