From 381c29a46d3c7b82d2aafd9c0562b68759c3b882 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Wed, 23 Nov 2016 17:10:04 -0500 Subject: [PATCH] Add `buildTmpPath` method. Allows generating the path to be separated from creating the directory. There are a number of use cases where a path will likely not need to be created eagerly (i.e. broccoli-funnel in most cases has to remove its output path only to symlink it from an input path). --- README.md | 17 +++++++++++++++++ index.js | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e28735c..23e2c09 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,20 @@ quickTemp.remove(this, 'tmpDestDir'); This will also assign `this.tmpDestDir = null`. If `this.tmpDestDir` is already null or undefined, it will be a no-op. + +### Building a temporary directory path + +To generate a temporary directory path (but not create it) and assign its +path to `this.someThing`, call: + +```js +quickTemp.buildTmpPath(this, 'someThing') +``` + +An optional third argument lets you override the class-name component of the +temporary directory name: + +```js +quickTemp.buildTmpPath(this, 'tmpDestDir', 'TreeMerger'); +quickTemp.buildTmpPath(this, 'tmpDestDir', this.constructor.name); // default +``` diff --git a/index.js b/index.js index b27d91f..a9a4178 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,14 @@ var mktemp = require('mktemp') var rimraf = require('rimraf') var underscoreString = require('underscore.string') +exports.buildTmpPath = buildTmpPath +function buildTmpPath(obj, prop, className) { + if (className == null) className = obj.constructor && obj.constructor.name + + var tmpDirName = prettyTmpDirName(className, prop) + return obj[prop] = path.join(findBaseDir(), tmpDirName) +} + exports.makeOrRemake = makeOrRemake function makeOrRemake(obj, prop, className) { if (obj[prop] != null) { @@ -40,9 +48,8 @@ function remove(obj, prop) { function makeTmpDir(obj, prop, className) { - if (className == null) className = obj.constructor && obj.constructor.name - var tmpDirName = prettyTmpDirName(className, prop) - return mktemp.createDirSync(path.join(findBaseDir(), tmpDirName)) + var tmpDirPath = buildTmpPath(obj, prop, className) + return mktemp.createDirSync(tmpDirPath) } function currentTmp() {