Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { join, resolve, basename, dirname, isAbsolute } from 'path'
- import { exists, stat, mkdir, readdir, rmdir, unlink } from 'fs'
- import { promisify } from 'util'
- const existsAsync = promisify(exists)
- const statAsync = promisify(stat)
- const mkdirAsync = promisify(mkdir)
- const readdirAsync = promisify(readdir)
- const rmdirAsync = promisify(rmdir)
- const unlinkAsync = promisify(unlink)
- export class NullEntry {
- constructor(public type: 'null',
- public name: string) { }
- }
- export class FileEntry {
- constructor(public type: 'file',
- public name: string,
- public size: number) { }
- }
- export class DirectoryEntry {
- constructor(public type: 'directory',
- public name: string) { }
- }
- export type Entry = NullEntry | FileEntry | DirectoryEntry
- /** Ensures that the given path is absolute. */
- export function toAbsolute(name: string): string {
- return !isAbsolute(name)
- ? resolve(name)
- : name
- }
- /** Returns the file system entry info for the given pathname. */
- export async function getEntry(name: string): Promise<Entry> {
- const absolute = toAbsolute(name)
- const exists = await existsAsync(absolute)
- if (exists) {
- const stat = await statAsync(absolute)
- if (stat.isDirectory()) {
- return new DirectoryEntry('directory', basename(name))
- } else if (stat.isFile()) {
- return new FileEntry('file', basename(name), stat.size)
- }
- }
- return new NullEntry('null', basename(name))
- }
- /** Creates the directory path of the given name. */
- export async function createDirectory(name: string): Promise<void> {
- function createList(name: string): string[] {
- const absolute = toAbsolute(name)
- const stack = [resolve(absolute)]
- const current = [resolve(absolute)]
- while (true) {
- const next = dirname(current[0])
- if (current[0] === next) {
- break
- } else {
- stack.push(next)
- current[0] = next
- }
- }
- return stack.reverse()
- }
- for (const dirname of createList(name)) {
- const entry = await getEntry(dirname)
- switch (entry.type) {
- case 'null': await mkdirAsync(dirname)
- case 'directory': continue
- case 'file': continue
- }
- }
- }
- /** Reads the entries out of the given directory name. */
- export async function readDirectory(name: string): Promise<Entry[]> {
- const absolute = toAbsolute(name)
- const contents = await readdirAsync(absolute)
- const entries = contents.map(content => getEntry(join(name, content)))
- return Promise.all(entries)
- }
- /** Deletes the directory of the given name. */
- export async function deleteDirectory(path: string): Promise<void> {
- const absolute = toAbsolute(path)
- if (!existsAsync(absolute)) {
- return
- }
- const entries = await readDirectory(absolute)
- const dirs = entries.filter(entry => entry.type === 'directory')
- const files = entries.filter(entry => entry.type === 'file')
- for (const entry of dirs) {
- await deleteDirectory(join(absolute, entry.name))
- }
- for (const entry of files) {
- await unlinkAsync(join(absolute, entry.name))
- }
- await rmdirAsync(absolute)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement