tempdir-yaml

Build temporary directories and files with YAML definition.

tempdir-yaml

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 resolvefunction and a cleanupfunction. yamlis 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: datapairs. If datais:
    • a string- create a file that nameis the name and datais the content.
    • null- create an empty folder.
    • a list or a map- create a folder and use dataas the children.

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

cleanupfunction would remove the temporary folder. If cleanupis not called, the temporary folder would be removed at the "exit" event.

withDir

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

A wrapper of makeDirthat would automatically cleanup when onReadyreturns/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 cleanupis an async function.
  • 0.2.1 (Jun 27, 2018)

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

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

    • First release.

Repository

eight04/tempdir-yaml


上一篇:ordered-json
下一篇:ordered-object

相关推荐

  • 前端vue2 vue-cli3项目实现国际化实战 vue-i18n +yaml文件格式

    最近公司的新项目需要做国际化,顾名思义,国际化就是可以在页面上切换显示语言,在国内大多数项目中只要支持中文和英文的切换就够了。在这里主要记录一下用vue技术栈,用到的 vuei18n插件的使用。

    1 个月前
  • yawn-yaml

    YAML parser that preserves comments and styling YAWN YAML Build Status(https://travisci.org/mohse...

    3 个月前
  • yamlparser

    A YAML parser written in javascript. This is the port to CommonJS. Clientside script is available at...

    2 年前
  • yamljs

    Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Als...

    2 年前
  • yamlish

    Parser/encoder for the yamlish format This is a thingie to parse the "yamlish" format used to seria...

    2 年前
  • yamlify-object

    Stringify object/array with yaml syntax yamlifyobject Build Statustravisimgtravisurl Code Coverag...

    2 年前
  • yamlify

    YAML for browserify and node.js; YAML includes yamlify YAML (, ) files support for browserify an...

    1 年前
  • yamlcss

    yamlcss是什么 什么是yamlcss,A modular CSS framework for truly flexible, accessible and responsive websit...

    2 年前
  • yaml2resume

    No name given yet yaml2resume No name given yet Install Install it with Usage Author ...

    8 个月前
  • yaml-parser

    YAML 1.2 parser and serializer YAMLPARSER YAML 1.2 parser / writer for JavaScript ================...

    1 年前

官方社区

扫码加入 JavaScript 社区