UNPKG

firebase-tools

Version:
80 lines (79 loc) • 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileExistsSync = fileExistsSync; exports.dirExistsSync = dirExistsSync; exports.readFile = readFile; exports.listFiles = listFiles; exports.moveAll = moveAll; exports.removeDirectoryIfEmpty = removeDirectoryIfEmpty; const fs_1 = require("fs"); const path = require("path"); const fs = require("fs-extra"); const error_1 = require("./error"); const logger_1 = require("./logger"); function fileExistsSync(path) { try { return (0, fs_1.statSync)(path).isFile(); } catch (e) { return false; } } function dirExistsSync(path) { try { return (0, fs_1.statSync)(path).isDirectory(); } catch (e) { return false; } } function readFile(path) { try { return (0, fs_1.readFileSync)(path).toString(); } catch (e) { if (e.code === "ENOENT") { throw new error_1.FirebaseError(`File not found: ${path}`); } throw e; } } function listFiles(path) { try { return (0, fs_1.readdirSync)(path); } catch (e) { if (e.code === "ENOENT") { throw new error_1.FirebaseError(`Directory not found: ${path}`); } throw e; } } function moveAll(srcDir, destDir) { if (!(0, fs_1.existsSync)(destDir)) { (0, fs_1.mkdirSync)(destDir, { recursive: true }); } const files = listFiles(srcDir); for (const f of files) { const srcPath = path.join(srcDir, f); if (srcPath === destDir) continue; fs.moveSync(srcPath, path.join(destDir, f)); } } async function removeDirectoryIfEmpty(absDirPath) { try { if (await fs.pathExists(absDirPath)) { const stat = await fs.stat(absDirPath); if (stat.isDirectory()) { const entries = await fs.readdir(absDirPath); if (entries.length === 0) { await fs.remove(absDirPath); } } } } catch (err) { logger_1.logger.debug(`Failed to clean up directory '${absDirPath}' if empty: ${(0, error_1.getErrMsg)(err)}`); } }