UNPKG

file-manipulator

Version:

## Easy File operations library

284 lines (226 loc) β€’ 7.68 kB
# FILE-MANIPULATOR ## Easy File operations library ### 1. ways of using - for file system operation πŸ€– - node js fs wrapper for CRUD to files and folders βœ… - create your custom CLI πŸŽ‰ ( fs / nodejs / react-native / flutter / ember/eth... ) - easy and readable documentation πŸ— - high-level wrapper for every-level projects 🍎 - good supporting πŸ₯½ - except error handling and using boolean operations methods result βœ… - UPD: support js and ts already βœ… βœ… βœ… install > yarn add file-manipulator or > npm i file-manipulator ## 2. one`s principe for each method every operation need to: - file - file name - file extension - file folder path - operation (CRUD) - folder - folder name - folder path - operation (CRUD) ### 3. example of using ```ts import fileManipulator from 'file-manipulator' const start = async () => { //create file const isCreatedFile = await fileManipulator.create.file({ name: 'example-file', ext: 'txt', path: './', content: 'Hello wold πŸŽ‰', replaceExisting: true }) console.log({isCreatedFile}) //create folder const isCreatedFolder = await fileManipulator.create.folder({ name: 'example-folder', path: './', replaceExisting: true }) console.log({isCreatedFolder}) //copy file to folder const isCopiedFile = await fileManipulator.copy.file({ name: 'example-file', ext: 'txt', from: './', to: './example-folder', replaceExisting: true }) console.log({isCopiedFile}) //move example file to example folder const isMovedFile = await fileManipulator.move.file({ name: 'example-file', ext: 'txt', from: './', to: './example-folder', renameTo: 'example-file-1', }) console.log({isMovedFile}) //delete excess file const isDeletedFile = await fileManipulator.delete.file({ name: 'example-file', ext: 'txt', path: './example-folder', }) console.log({isDeletedFile}) //edit file name const isRenamedFile = await fileManipulator.update.file({ name: 'example-file-1', ext: 'txt', renameTo: 'example-file', path: './example-folder' }) console.log({isRenamedFile}) //replace file inner const isReplacedInnerText = await fileManipulator.update.file({ name: 'example-file', ext: 'txt', path: './example-folder', stringFrom: 'Hello ', //string or regexp stringTo: 'Wow πŸŽ‰', }) console.log({isReplacedInnerText}) //replace file inner text by coordinates const isEditedInnerText = await fileManipulator.update.file({ name: 'example-file', ext: 'txt', path: './example-folder', lineParams: {line: 0, space: 6, len: 7}, //replace from row 0, col 6, to row 0, col 13, to "stringTo" value stringTo: 'Wow πŸŽ‰', }) console.log({isEditedInnerText}) } start() ``` ### 4. types and API docs ```ts //main defult types class FileManipulator { copy: Copper move: Mover delete: Deleter create: Creator update: Updater read: Reader } export default new FileManipulator() //Copper types 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) } fileManipulator.copy.file(config: CopperFileConfig): Promise<boolean> fileManipulator.copy.folder(config: CopperFolderConfig): Promise<boolean> //Mover types type MoverFileConfig = { 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 MoverFolderOutConfig = { from: string //folder path from copy to: string //folder path to paste name: string //name folder renameTo?: string //new optional name for once folder replaceExisting?: boolean //replace if there is exist the same file ? (default false) } fileManipulator.move.file(config: MoverFileConfig): Promise<boolean> fileManipulator.move.folder(config: MoverFolderOutConfig): Promise<boolean> //Deleter types type DeleterFileConfig = { name: string //name without ext ext: string //name without ext path: string //path file locate } type DeleterFolderOutConfig = { path: string //path file locate name: string //name folder } fileManipulator.delete.file(config: DeleterFileConfig): Promise<boolean> fileManipulator.delete.folder(config: DeleterFolderOutConfig): Promise<boolean> //Creator types type CreatorFileConfig = { path: string //path without name name: string //name without ext ext: string //name without ext content?: string //default empty string (inside) replaceExisting?: boolean //replace if there is exist the same file ? (default false) encoding?: BufferEncoding //default "utf8" } type CreatorFolderOutConfig = { path: string //path without name name: string //name folder replaceExisting?: boolean //replace if there is exist the same file (default false) } fileManipulator.create.file(config: CreatorFileConfig): Promise<boolean> fileManipulator.create.folder(config: CreatorFolderOutConfig): Promise<boolean> //Updater types type UpdateFileInnerConfig = { //together params lineParams?: { line: number //row number for replace string space: number //space number for replace string (from 0) len: number //length for replace string to "stringTo" params (iclude first char) } stringFrom?: string | RegExp //replace match string = string / regexp to find stringTo?: string } type UpdateFileOutConfig = { path: string //path without name name: string //name without ext ext: string //name without ext renameTo?: string //new name replaceExisting?: boolean //replace if there is exist the same file ? (default false) } type UpdateFolderOutConfig = { path: string //path without name name: string //current name without ext (before rename) renameTo: string //new name replaceExisting?: boolean //replace if there is exist the same file ? (default false) } fileManipulator.update.file(config: UpdateFileInnerConfig & UpdateFileOutConfig): Promise<boolean> fileManipulator.update.folder(config: UpdateFolderOutConfig): Promise<boolean> //Reader types type ReaderFileConfig = { name: string //name without ext ext: string //name without ext path: string //path file locate folder parent encoding?: BufferEncoding //result file code (default urf8) } type ReaderFolderOutConfig = { path: string //path folder locate parent name: string //name folder } export type ReturnReadFileType = { readed: boolean, //is readed file or no result: string // file content } export type ReturnReadFolderType = { readed: boolean, //is readed folder or no result: string[] // folder content array (with extensions) (ex: [f1.txt] ) } fileManipulator.read.file(config: ReaderFileConfig): Promise<ReturnReadFileType> fileManipulator.read.folder(config: ReaderFolderOutConfig): Promise<ReturnReadFolderType> ``` #Contact ✨Lib going to grow up, and you can send your questions and offers to my telegram [Stepan_Turchenko](https://telegram.me/Stepan_Turchenko) πŸ›¬