UNPKG

cli-stash

Version:

CLI application to manage and work with Atlassian Stash. Work with your Stash project and repositories from Command lines.

84 lines (83 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileWriter = void 0; const fs = require("fs"); const path = require("path"); /** * Class to create and write files into file system */ class FileWriter { /** * Method to create files asynchronously * @param {string} path path to save the file * @param {string} content content to write into the file * @param {Function} [callback] callback function to handle the end of write */ static createFile(path, content, callback) { fs.writeFile(path, content, (err) => { if (callback) { callback.call(this, err); } }); } /** * Method to create files synchronously * @param {string} path path to save the file * @param {string} content content to write into the file */ static createFileSync(path, content) { fs.writeFileSync(path, content); } /** * Method to create folders synchronously (create the entire folders path if is needed) * @param {string} folderPath folder to create */ static createFolderSync(folderPath) { fs.mkdirSync(folderPath, { recursive: true }); } /** * Method to copy files asynchronously * @param {string} source source file * @param {string} target target file * @param {Function} callback callback function to handle the end of copy */ static copyFile(source, target, callback) { fs.copyFile(source, target, (err) => { if (callback) { callback.call(this, err); } }); } /** * Method to copy files synchronously * @param {string} source source file * @param {string} target target file */ static copyFileSync(sourcePath, targetPath) { fs.copyFileSync(sourcePath, targetPath); } /** * Method to delete and entire folder (and all subfolders) * @param {string} pathToDelete folder to delete */ static delete(pathToDelete) { if (fs.existsSync(pathToDelete)) { if (fs.lstatSync(pathToDelete).isDirectory()) { fs.readdirSync(pathToDelete).forEach(function (entry) { var entry_path = path.join(pathToDelete, entry); if (fs.lstatSync(entry_path).isDirectory()) { FileWriter.delete(entry_path); } else { fs.unlinkSync(entry_path); } }); fs.rmdirSync(pathToDelete); } else { fs.unlinkSync(pathToDelete); } } } } exports.FileWriter = FileWriter;