tmpgen
Create unique nested temporary directories. Makes factories according to a path spec with wildcards. Each wildcard gets replaced with a generated name, by monotonic-timestamp (the default), hat or a custom generator. If this results in an existing path, the generator is called again, and if that doesn't work, tmpgen
starts concatenating names. Has utilities to remove the created directories, but does not delete anything by default.
concrete example
const levelup = tmp = 'my-awesome-module/*' const db1 = db2 =
usage
The basic premise: create a factory with tmpgen(spec)
, then call the factory to create a unique directory and get its path. Only the last wildcard in a spec is made to be unique each time.
const tmpgen = // Create a factory, using the default generatorconst tmp = // /tmp/a/1453821919917/b-1453821919918console // /tmp/a/1453821919917/b-1453821919921console // Create a sub-factory, using node-hat as generator.const sub = tmp // /tmp/a/1453821919917/b-1453821919923/beep-eae732..console // /tmp/a/1453821919917/b-1453821919923/beep-881ce2..console // Recursively delete every directory created by `sub`sub // Create another directoryconst p1 = // Append (and create) an extra pathconst p2 = // Delete specific directoriessubsub // Throws, because `sub` did not create this path or an ancestorsub // Delete every directory created by `tmp` and `sub`tmp
api
factory = tmpgen([spec][, opts])
- spec: must be a relative path. Defaults to "[module-name]/*". If
spec
does not contain wildcards, and the path it resolves to already exists, the factory will throw an error. - opts.gen: "timestamp" or "ts", "hat" or "random" or a function to be called without any arguments. These are equal:
- opts.root: where to mount directories. Defaults to
osenv.tmpdir()
- opts.clean: call
factory.del()
on process exit, if the exit code is zero or opts.always is true. Essentially:
// opts.cleanprocess // opts.clean && opts.alwaysprocess
path = factory(...extra)
Create a new directory. Any extra arguments are appended to the generated path and resolved. Throws if the resolved path is not inside the generated path:
factory = factory.sub(spec[, opts])
Create a sub-factory, inheriting the options from its parent. Unless you specify opts.root
, the sub will call its parent (once) to generate a root path. For sub-factories, spec
is required.
factory.del([path])
Recursively and synchronously delete a path. Throws if path
(or an ancestor) was not created by the factory. If path
is omitted, all previously created directories are deleted. Note that in the following example, the complete /tmp/a
will be deleted if it did not exist before.
const tmp = const path = tmp
If you use a custom root
, it will be created for you, but never deleted. In this example, /home/beep/tmp-a
and /home/beep/tmp-aa
are deleted at the end, but not /home/beep
.
const gen = 'a'const tmp = const path1 = const path2 = tmp
install
With npm do:
npm install tmpgen
license
MIT © Vincent Weevers