UNPKG

st-cp

Version:

CLI tool for platform independent, recursive copying of files and folders.

66 lines 2.64 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var path_1 = require("path"); var chalk = require("chalk"); var fs_1 = require("fs"); var os_1 = require("os"); var is_directory_1 = require("./is-directory"); var st_cp_linux_1 = require("./st-cp-linux"); var st_cp_windows_1 = require("./st-cp-windows"); var DEFAULT_COPY_PATH_OR_FILE_OPTION = { printInfo: true, printWarning: true, printError: true }; exports.copyPathOrFile = function (sourcePath, destination, option) { if (option === void 0) { option = DEFAULT_COPY_PATH_OR_FILE_OPTION; } try { var copy = os_1.platform() == "win32" ? st_cp_windows_1.copyPathOrFile : st_cp_linux_1.copyPathOrFile; var currentPath = process.cwd(); sourcePath = copy.resolve(currentPath, sourcePath); if (!fs_1.existsSync(sourcePath)) { if (option.printError) { console.log(chalk.red("[!] Warning: Source does not exist " + copy.relative(currentPath, sourcePath))); } return; } var isFolder = is_directory_1.isDirectory(sourcePath); var destinationPath = copy.resolve(currentPath, destination.path); if (option.printInfo) { console.log(chalk.cyan("[*] Copying " + (isFolder ? "folder" : "file") + " " + chalk.white(copy.relative(currentPath, sourcePath)) + " to " + chalk.white(copy.relative(currentPath, destinationPath)) + "...")); } //create folder if not exist if (destination.isDirectory) { if (!fs_1.existsSync(destinationPath)) { fs_1.mkdirSync(destinationPath, { recursive: true, }); } } else { if (destinationPath.indexOf(path_1.sep) > 0) { fs_1.mkdirSync(destinationPath.substring(0, destinationPath.lastIndexOf(path_1.sep)), { recursive: true, }); } } copy.copyPathOrFile(sourcePath, __assign(__assign({}, destination), { path: destinationPath }), option); } catch (err) { if (option.printError) { console.log(chalk.red("[!] Warning: ") + err); } } }; //# sourceMappingURL=st-cp.js.map