file-manipulator
Version:
## Easy File operations library
81 lines (71 loc) • 2.23 kB
text/typescript
import fileManipulator from '../index'
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()