Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { join, resolve, basename, dirname, isAbsolute } from 'path'
  2. import { exists, stat, mkdir, readdir, rmdir, unlink } from 'fs'
  3. import { promisify } from 'util'
  4.  
  5. const existsAsync = promisify(exists)
  6. const statAsync = promisify(stat)
  7. const mkdirAsync = promisify(mkdir)
  8. const readdirAsync = promisify(readdir)
  9. const rmdirAsync = promisify(rmdir)
  10. const unlinkAsync = promisify(unlink)
  11.  
  12. export class NullEntry {
  13.     constructor(public type: 'null',
  14.         public name: string) { }
  15. }
  16. export class FileEntry {
  17.     constructor(public type: 'file',
  18.         public name: string,
  19.         public size: number) { }
  20. }
  21. export class DirectoryEntry {
  22.     constructor(public type: 'directory',
  23.         public name: string) { }
  24. }
  25.  
  26. export type Entry = NullEntry | FileEntry | DirectoryEntry
  27.  
  28. /** Ensures that the given path is absolute. */
  29. export function toAbsolute(name: string): string {
  30.     return !isAbsolute(name)
  31.         ? resolve(name)
  32.         : name
  33. }
  34.  
  35. /** Returns the file system entry info for the given pathname. */
  36. export async function getEntry(name: string): Promise<Entry> {
  37.     const absolute = toAbsolute(name)
  38.     const exists = await existsAsync(absolute)
  39.     if (exists) {
  40.         const stat = await statAsync(absolute)
  41.         if (stat.isDirectory()) {
  42.             return new DirectoryEntry('directory', basename(name))
  43.         } else if (stat.isFile()) {
  44.             return new FileEntry('file', basename(name), stat.size)
  45.         }
  46.     }
  47.     return new NullEntry('null', basename(name))
  48. }
  49.  
  50. /** Creates the directory path of the given name. */
  51. export async function createDirectory(name: string): Promise<void> {
  52.     function createList(name: string): string[] {
  53.         const absolute = toAbsolute(name)
  54.         const stack = [resolve(absolute)]
  55.         const current = [resolve(absolute)]
  56.         while (true) {
  57.             const next = dirname(current[0])
  58.             if (current[0] === next) {
  59.                 break
  60.             } else {
  61.                 stack.push(next)
  62.                 current[0] = next
  63.             }
  64.         }
  65.         return stack.reverse()
  66.     }
  67.     for (const dirname of createList(name)) {
  68.         const entry = await getEntry(dirname)
  69.         switch (entry.type) {
  70.             case 'null': await mkdirAsync(dirname)
  71.             case 'directory': continue
  72.             case 'file': continue
  73.         }
  74.     }
  75. }
  76.  
  77. /** Reads the entries out of the given directory name. */
  78. export async function readDirectory(name: string): Promise<Entry[]> {
  79.     const absolute = toAbsolute(name)
  80.     const contents = await readdirAsync(absolute)
  81.     const entries = contents.map(content => getEntry(join(name, content)))
  82.     return Promise.all(entries)
  83. }
  84.  
  85. /** Deletes the directory of the given name. */
  86. export async function deleteDirectory(path: string): Promise<void> {
  87.     const absolute = toAbsolute(path)
  88.     if (!existsAsync(absolute)) {
  89.         return
  90.     }
  91.     const entries = await readDirectory(absolute)
  92.     const dirs = entries.filter(entry => entry.type === 'directory')
  93.     const files = entries.filter(entry => entry.type === 'file')
  94.     for (const entry of dirs) {
  95.         await deleteDirectory(join(absolute, entry.name))
  96.     }
  97.     for (const entry of files) {
  98.         await unlinkAsync(join(absolute, entry.name))
  99.     }
  100.     await rmdirAsync(absolute)
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement