npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tmpgen

v1.1.1

Published

Create unique nested temporary directories

Downloads

34

Readme

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.

npm status Travis build status AppVeyor build status Dependency status

concrete example

const levelup = require('levelup')
    , tmp = require('tmpgen')('my-awesome-module/*')

const db1 = levelup(tmp())
    , db2 = levelup(tmp())

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 = require('tmpgen')

// Create a factory, using the default generator
const tmp = tmpgen('a/*/b-*')

// /tmp/a/1453821919917/b-1453821919918
console.log(tmp())

// /tmp/a/1453821919917/b-1453821919921
console.log(tmp())

// Create a sub-factory, using node-hat as generator.
const sub = tmp.sub('beep-*', { gen: 'hat' })

// /tmp/a/1453821919917/b-1453821919923/beep-eae732..
console.log(sub())

// /tmp/a/1453821919917/b-1453821919923/beep-881ce2..
console.log(sub())

// Recursively delete every directory created by `sub`
sub.del()

// Create another directory
const p1 = sub()

// Append (and create) an extra path
const p2 = sub('even/deeper')

// Delete specific directories
sub.del(p1)
sub.del(p2)

// Throws, because `sub` did not create this path or an ancestor
sub.del('/home')

// Delete every directory created by `tmp` and `sub`
tmp.del()

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:
tmpgen({ gen: 'ts' })
tmpgen({ gen: require('monotonic-timestamp') })
  • 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.clean
process.on('exit', (code) => code || tmp.del())

// opts.clean && opts.always
process.on('exit', () => tmp.del())

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('foo/..', 'bar', '..')

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 = tmpgen('a/b/c/*')
const path = tmp()

tmp.del()

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 = tmpgen('tmp-*', { gen, root: '/home/beep' })
const path1 = tmp()
const path2 = tmp()

tmp.del()

install

With npm do:

npm install tmpgen

license

MIT © Vincent Weevers