Last active
December 13, 2017 21:23
-
-
Save eschwartz/bc579753db90a92f6fd71579346506a6 to your computer and use it in GitHub Desktop.
Promise wrapper for node-tmp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const co = require('co'); | |
const _tmp = require('tmp'); | |
_tmp.setGracefulCleanup(); | |
// Promise wrappers around tmp | |
const tmp = { | |
/** @return {Promise<{path, cleanup}>} */ | |
file: () => co(function* () { | |
return yield cb => _tmp.file({ unsafeCleanup: true }, (err, path, fd, cleanup) => cb(err, { | |
path, | |
cleanup | |
})); | |
}), | |
/** @return {Promise<{path, cleanup}>} */ | |
dir: () => co(function* () { | |
return yield cb => _tmp.dir({ unsafeCleanup: true }, (err, path, cleanup) => cb(err, { | |
path, | |
cleanup | |
})); | |
}), | |
withDir: (callback) => co(function* () { | |
const tmpObj = yield tmp.dir(); | |
return yield tmp.withTmpObj(tmpObj, callback) | |
}), | |
withFile: (callback) => co(function* () { | |
const tmpObj = yield tmp.file(); | |
return yield tmp.withTmpObj(tmpObj, callback) | |
}), | |
withTmpObj: (tmpObj, callback) => co(function* () { | |
try { | |
const res = yield Promise.resolve(callback(tmpObj.path)); | |
return res; | |
} | |
finally { | |
tmpObj.cleanup(); | |
} | |
}) | |
}; | |
module.exports = tmp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as _tmp from 'tmp'; | |
_tmp.setGracefulCleanup(); | |
const tmp = { | |
file: (): Promise<ITmpObj> => { | |
return new Promise((onRes, onErr) => { | |
_tmp.file( | |
{ unsafeCleanup: true }, | |
(err, path, fs, cleanup) => err ? onErr(err) : onRes({ path, cleanup }) | |
) | |
}) | |
}, | |
dir: (): Promise<ITmpObj> => { | |
return new Promise((onRes, onErr) => { | |
_tmp.dir( | |
{ unsafeCleanup: true }, | |
(err, path, cleanup) => err ? onErr(err) : onRes({ path, cleanup }) | |
) | |
}) | |
} | |
}; | |
export default tmp; | |
export const file = tmp.file; | |
export const dir = tmp.dir; | |
interface ITmpObjConsumer<TRes> { | |
(tmpFilePath: string): Promise<TRes> | TRes; | |
} | |
export async function withDir<TRes>(fn: ITmpObjConsumer<TRes>):Promise<TRes> { | |
const tmpObj = await tmp.dir(); | |
return await withTmpObj(tmpObj, fn); | |
} | |
export async function withFile<TRes>(fn: ITmpObjConsumer<TRes>):Promise<TRes> { | |
const tmpObj = await tmp.file(); | |
return await withTmpObj(tmpObj, fn); | |
} | |
async function withTmpObj<TRes>(tmpObj: ITmpObj, fn: ITmpObjConsumer<TRes>):Promise<TRes> { | |
try { | |
const res = await Promise.resolve(fn(tmpObj.path)); | |
return res; | |
} | |
finally { | |
tmpObj.cleanup(); | |
} | |
} | |
export interface ITmpObj { | |
path: string; | |
cleanup: () => void; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment