Build temporary directories and files with YAML definition.
npm install tempdir-yamltempdir-yaml
============



Build temporary directories and files with YAML definition. Inspired by the filemaker of pydeps.
Installation
------------
``
`
npm install -D tempdir-yaml
`
Usage
-----
js
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:
yaml
`
- my-dir:
`
* A directory containing some files:
yaml
`
- my-dir:
- file1
- file2
`
* An empty file:
yaml
`
- file
`
* A file with content:
yaml
`
- file: |
the content of the file
`
API
----
$3
js
`
async makeDir(yaml: String) =>
({
resolve: (...args) => absPath: String,
cleanup: async () => undefined
})
resolve
Create a temporary file tree and return a function and a cleanup function. yaml is dedented before parsed.
name: data
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 pairs. If data is:
name
- a string - create a file that is the name and data is the content.
data
- null - create an empty folder.
- a list or a map - create a folder and use 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.
`
$3
js
`
async withDir(yaml:String, async onReady(resolve))
makeDir
A wrapper of that would automatically cleanup when onReady returns/throws.
`
js
it("my test", () =>
withDir(
- foo.txt: |
FOO
- bar.txt: |
BAR
, resolve => {
`
// test something with the files...
})
);
cleanup
Changelog
---------
* 0.3.0 (Jun 5, 2019)
- Bump dependencies.
- Breaking: now is an async function.
onReady
* 0.2.1 (Jun 27, 2018)
- Fix: ENOENT error when is async in withDir.
makeDir
* 0.2.0 (Jun 27, 2018)
- Change: the signature of is changed.
withDir`.
- Add:
* 0.1.0 (Jun 26, 2018)
- First release.