edwinjhlee/path-utils

ensure

Opened this issue · 0 comments

  async assertDirectory(){
        const p = this.dump()
        const s = await fs.stat(p)
        if (s.isDirectory()) {
            return result.success(this.toDirectory())
        } else {
            return result.failure<this>(new Error("Not a directory"))
        }
    }

    async ensureDirectory(){
        const p = this.dump()
        await fs.mkdirp(p)
        return this.assertDirectory()
    }

    async assertParent(){
        await this.parent().assertDirectory()
        return this
    }

    async ensureParent(){
        await this.parent().ensureDirectory()
        return this
    }

    assertDirectory$(){
        const p = this.dump()
        const s = fs.statSync(p)
        if (s.isDirectory()) {
            return result.success(new Directory(this.start, this.seperator, this.content))
        } else {
            return result.failure<Directory>(new Error("Not a directory"))
        }
    }

    ensureDirectory$(){
        const p = this.dump()
        fs.mkdirpSync(p)
        return this.assertDirectory$()
    }

    assertParent$(){
        this.parent().assertDirectory$()
        return this
    }

    ensureParent$(){
        this.parent().ensureDirectory$()
        return this
    }