0.4.0-alpha.1 • Published 5 years ago
@peregrine/filesystem v0.4.0-alpha.1
@peregrine/filesystem
Simple FileSystem API for NodeJS, with Promise and Stream support.
Code example
Files
printInfo.ts
import { Directory, File } from "@peregrine/filesystem"
export default async function printInfo(item: Directory | File){
// Print directory or file info
console.log(`Path: ${item.path}\n`)
console.log(`Name: ${item.name}\n`)
console.log(`Type: ${item instanceof Directory ? "Directory" : "File"}\n\n`)
if(item instanceof Directory){
// Print all the children
let items = (await item.children).map(item => ` - ${item.name} (${item instanceof Directory ? "Directory" : "File"})`)
console.log(`Children:\n${items.join("\n")}`)
}
}
example1.ts
import { Directory, File } from "@peregrine/filesystem"
import printInfo from "./printInfo"
// Get current directory
const currentDir = Directory.currentDir
// Get parent directory
const parentDir = currentDir.parent
// The parent can be null if there's no parent directory (C:/ does not have any parent directory)
if(parentDir != null) printInfo(parentDir)
example2.ts
import { Directory, File } from "@peregrine/filesystem"
import printInfo from "./printInfo"
(async() => {
const file = await File.createItem(`C:/Users/Someone/Documents/test.docx`)
if(file != null) printInfo(file)
})()
Output
example1.ts
Path: C:/Users/Someone/Documents
Name: Documents
Type: Directory
Children:
- test.docx (File)
- CloudStorage (Directory)
- test.txt (File)
example2.ts
Path: C:/Users/Someone/Documents/test.docx
Name: test.docx
Type: File