UNPKG

file-manipulator

Version:

## Easy File operations library

59 lines (54 loc) 2.25 kB
import {ManipulatorInterfaceType} from '../types' import fs from 'fs-extra' import path from 'path' import utils from '../utils' type CopperFileConfig = { name: string //file name without ext ext?: string //name without ext from: string //folder path from copy to: string //folder path to paste renameTo?: string //new optional name for once file replaceExisting?: boolean //replace if there is exist the same file ? (default false) } type CopperFolderConfig = { renameTo?: string //new optional name for once file from: string //folder path from copy to: string //folder path to paste name: string //name folder replaceExisting?: boolean //replace if there is exist the same file (default false) (for files only) } class Copper implements ManipulatorInterfaceType { async file(config: CopperFileConfig) { try { //file names const startFullName = utils.getNameByConfig(config) const endFullName = utils.getNameRenameConfig(config) //file pathes const startFullPath = path.resolve(config.from, startFullName) const endFullPath = path.resolve(config.to, endFullName) //operations await fs.copy(startFullPath, endFullPath, {overwrite: !!config?.replaceExisting, errorOnExist: true,}) return true } catch (e) { console.log(this.constructor.name, `[${this.file.name}]`, config.name, e) return false } } async folder(config: CopperFolderConfig) { try { //folder pathes const startFullName = `${config.name}` const endFullName = config?.renameTo ? `${config?.renameTo}` : startFullName //folder pathes const startFullPath = path.resolve(config.from, startFullName) const endFullPath = path.resolve(config.to, config.name) //operations await fs.copy(startFullPath, endFullPath, {overwrite: !!config?.replaceExisting, errorOnExist: true}) return true } catch (e) { console.log(this.constructor.name, `[${this.folder.name}]`, config.name, e) return false } } } export default new Copper()