tempdir-yaml

2019-11-22 admin

tempdir-yaml是什么

什么是tempdir-yaml,Build temporary directories and files with YAML definition.

tempdir-yaml使用教程帮助文档

tempdir-yaml

Build Status codecov install size

Build temporary directories and files with YAML definition. Inspired by the filemaker of pydeps.

Installation

npm install -D tempdir-yaml

Usage

const fs = require("fs");
const {withDir} = require("tempdir-yaml");

describe("a test", () => {
  it("a case", () =>
    withDir(`
      - package.json
      - sub-dir:
        - foo.txt: |
           content of foo
    `, resolve => {
      const data = fs.readFileSync(resolve("sub-dir/foo.txt"), "utf-8");
      assert.equal(data, "content of foo\n");
    })
  );
});

Some quick examples:

  • An empty folder:

    - my-dir:
    
  • A directory containing some files:

    - my-dir:
      - file1
      - file2
    
  • An empty file:

    - file
    
  • A file with content:

    - file: |
       the content of the file
    

API

makeDir

async makeDir(yaml: String) =>
  ({
    resolve: (...args) => absPath: String,
    cleanup: async () => undefined
  })

Create a temporary file tree and return a resolve function and a cleanup function. yaml is dedented before parsed.

The file tree is created according to the type of the value:

  • a list - if the list item is:
    • a string - create an empty file. Use the item as its name.
    • a map - see below.
  • a map - a list of name: data pairs. If data is:
    • a string - create a file that name is the name and data is the content.
    • null - create an empty folder.
    • a list or a map - create a folder and use data as the children.

resolve function resolves relative paths to an absolute path based on the temporary folder. You can get the root path with resolve(".").

cleanup function would remove the temporary folder. If cleanup is not called, the temporary folder would be removed at the “exit” event.

withDir

async withDir(yaml:String, async onReady(resolve))

A wrapper of makeDir that would automatically cleanup when onReady returns/throws.

it("my test", () =>
  withDir(`
    - foo.txt: |
        FOO
    - bar.txt: |
        BAR
  `, resolve => {
    // test something with the files...
  })
);

Changelog

  • 0.3.0 (Jun 5, 2019)

    • Bump dependencies.
    • Breaking: now cleanup is an async function.
  • 0.2.1 (Jun 27, 2018)

    • Fix: ENOENT error when onReady is async in withDir.
  • 0.2.0 (Jun 27, 2018)

    • Change: the signature of makeDir is changed.
    • Add: withDir.
  • 0.1.0 (Jun 26, 2018)

    • First release.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-79686.html

文章标题:tempdir-yaml

回到顶部