microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/lib/codepush-node-sdk/dist/utils/file-utils.js
169lines · modecode
| 1 | "use strict"; |
| 2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | return new (P || (P = Promise))(function (resolve, reject) { |
| 4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 7 | step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 8 | }); |
| 9 | }; |
| 10 | Object.defineProperty(exports, "__esModule", { value: true }); |
| 11 | const fs = require("fs"); |
| 12 | const path = require("path"); |
| 13 | const os = require("os"); |
| 14 | const rimraf = require("rimraf"); |
| 15 | const temp = require("temp"); |
| 16 | const _ = require("lodash"); |
| 17 | const noop = require('node-noop').noop; |
| 18 | function fileExists(file) { |
| 19 | try { |
| 20 | return fs.statSync(file).isFile(); |
| 21 | } |
| 22 | catch (e) { |
| 23 | return false; |
| 24 | } |
| 25 | } |
| 26 | exports.fileExists = fileExists; |
| 27 | function isBinaryOrZip(path) { |
| 28 | return path.search(/\.zip$/i) !== -1 |
| 29 | || path.search(/\.apk$/i) !== -1 |
| 30 | || path.search(/\.ipa$/i) !== -1; |
| 31 | } |
| 32 | exports.isBinaryOrZip = isBinaryOrZip; |
| 33 | function isDirectory(path) { |
| 34 | return fs.statSync(path).isDirectory(); |
| 35 | } |
| 36 | exports.isDirectory = isDirectory; |
| 37 | function copyFileToTmpDir(filePath) { |
| 38 | if (!isDirectory(filePath)) { |
| 39 | const outputFolderPath = temp.mkdirSync('code-push'); |
| 40 | rimraf.sync(outputFolderPath); |
| 41 | fs.mkdirSync(outputFolderPath); |
| 42 | const outputFilePath = path.join(outputFolderPath, path.basename(filePath)); |
| 43 | fs.writeFileSync(outputFilePath, fs.readFileSync(filePath)); |
| 44 | return outputFolderPath; |
| 45 | } |
| 46 | } |
| 47 | exports.copyFileToTmpDir = copyFileToTmpDir; |
| 48 | function generateRandomFilename(length) { |
| 49 | let filename = ''; |
| 50 | const validChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 51 | for (let i = 0; i < length; i++) { |
| 52 | filename += validChar.charAt(Math.floor(Math.random() * validChar.length)); |
| 53 | } |
| 54 | return filename; |
| 55 | } |
| 56 | exports.generateRandomFilename = generateRandomFilename; |
| 57 | function fileDoesNotExistOrIsDirectory(path) { |
| 58 | try { |
| 59 | return isDirectory(path); |
| 60 | } |
| 61 | catch (error) { |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | exports.fileDoesNotExistOrIsDirectory = fileDoesNotExistOrIsDirectory; |
| 66 | function createEmptyTmpReleaseFolder(folderPath) { |
| 67 | rimraf.sync(folderPath); |
| 68 | fs.mkdirSync(folderPath); |
| 69 | } |
| 70 | exports.createEmptyTmpReleaseFolder = createEmptyTmpReleaseFolder; |
| 71 | function removeReactTmpDir() { |
| 72 | rimraf.sync(`${os.tmpdir()}/react-*`); |
| 73 | } |
| 74 | exports.removeReactTmpDir = removeReactTmpDir; |
| 75 | function normalizePath(filePath) { |
| 76 | // replace all backslashes coming from cli running on windows machines by slashes |
| 77 | return filePath.replace(/\\/g, '/'); |
| 78 | } |
| 79 | exports.normalizePath = normalizePath; |
| 80 | function walk(dir) { |
| 81 | return __awaiter(this, void 0, void 0, function* () { |
| 82 | const stats = yield stat(dir); |
| 83 | if (stats.isDirectory()) { |
| 84 | var files = []; |
| 85 | for (const file of yield readdir(dir)) { |
| 86 | files = files.concat(yield walk(path.join(dir, file))); |
| 87 | } |
| 88 | return files; |
| 89 | } |
| 90 | else { |
| 91 | return [dir]; |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | exports.walk = walk; |
| 96 | function stat(path) { |
| 97 | return __awaiter(this, void 0, void 0, function* () { |
| 98 | return (yield callFs(fs.stat, path))[0]; |
| 99 | }); |
| 100 | } |
| 101 | exports.stat = stat; |
| 102 | function readdir(path) { |
| 103 | return __awaiter(this, void 0, void 0, function* () { |
| 104 | return (yield callFs(fs.readdir, path))[0]; |
| 105 | }); |
| 106 | } |
| 107 | exports.readdir = readdir; |
| 108 | function readFile(...args) { |
| 109 | return __awaiter(this, void 0, void 0, function* () { |
| 110 | return (yield callFs(fs.readFile, ...args))[0]; |
| 111 | }); |
| 112 | } |
| 113 | exports.readFile = readFile; |
| 114 | function access(path, mode) { |
| 115 | return __awaiter(this, void 0, void 0, function* () { |
| 116 | return callFs(fs.access, path, mode).then(() => { noop(); }); |
| 117 | }); |
| 118 | } |
| 119 | exports.access = access; |
| 120 | function rmDir(source, recursive = true) { |
| 121 | if (recursive) { |
| 122 | return new Promise((resolve, reject) => { |
| 123 | rimraf(source, err => { |
| 124 | if (err) { |
| 125 | reject(err); |
| 126 | } |
| 127 | else { |
| 128 | resolve(); |
| 129 | } |
| 130 | }); |
| 131 | }); |
| 132 | } |
| 133 | else { |
| 134 | return callFs(fs.rmdir, source).then(() => { noop(); }); |
| 135 | } |
| 136 | } |
| 137 | exports.rmDir = rmDir; |
| 138 | function mkTempDir(affixes) { |
| 139 | return callTemp(temp.mkdir, affixes); |
| 140 | } |
| 141 | exports.mkTempDir = mkTempDir; |
| 142 | function callTemp(func, ...args) { |
| 143 | return new Promise((resolve, reject) => { |
| 144 | func.apply(temp, _.concat(args, [ |
| 145 | (err, result) => { |
| 146 | if (err) { |
| 147 | reject(err); |
| 148 | } |
| 149 | else { |
| 150 | resolve(result); |
| 151 | } |
| 152 | } |
| 153 | ])); |
| 154 | }); |
| 155 | } |
| 156 | function callFs(func, ...args) { |
| 157 | return new Promise((resolve, reject) => { |
| 158 | func.apply(fs, _.concat(args, [ |
| 159 | (err, ...args) => { |
| 160 | if (err) { |
| 161 | reject(err); |
| 162 | } |
| 163 | else { |
| 164 | resolve(args); |
| 165 | } |
| 166 | } |
| 167 | ])); |
| 168 | }); |
| 169 | } |
| 170 | |