tmp-promise
Advanced tools
Comparing version 1.1.0 to 2.0.1
@@ -1,21 +0,27 @@ | ||
import { fileSync, dirSync, tmpNameSync, setGracefulCleanup } from 'tmp'; | ||
import { Options, SimpleOptions } from 'tmp'; | ||
import { fileSync, dirSync, tmpNameSync, setGracefulCleanup } from "tmp"; | ||
import { FileOptions, DirOptions, TmpNameOptions } from "tmp"; | ||
export interface DirectoryResult { | ||
path: string; | ||
cleanup(): void; | ||
path: string; | ||
cleanup(): Promise<void>; | ||
} | ||
export interface FileResult extends DirectoryResult { | ||
fd: number; | ||
fd: number; | ||
} | ||
export function file(options?: Options): Promise<FileResult>; | ||
export function withFile<T>(fn: (result: FileResult) => Promise<T>, options?: Options): Promise<T>; | ||
export function file(options?: FileOptions): Promise<FileResult>; | ||
export function withFile<T>( | ||
fn: (result: FileResult) => Promise<T>, | ||
options?: FileOptions | ||
): Promise<T>; | ||
export function dir(options?: Options): Promise<DirectoryResult>; | ||
export function withDir<T>(fn: (results: DirectoryResult) => Promise<T>, options?: Options): Promise<T>; | ||
export function dir(options?: DirOptions): Promise<DirectoryResult>; | ||
export function withDir<T>( | ||
fn: (results: DirectoryResult) => Promise<T>, | ||
options?: DirOptions | ||
): Promise<T>; | ||
export function tmpName(options?: SimpleOptions): Promise<string>; | ||
export function tmpName(options?: TmpNameOptions): Promise<string>; | ||
export { fileSync, dirSync, tmpNameSync, setGracefulCleanup } | ||
export { fileSync, dirSync, tmpNameSync, setGracefulCleanup }; |
79
index.js
@@ -1,64 +0,47 @@ | ||
var tmp = require("tmp"); | ||
var Promise = require("bluebird"); | ||
const {promisify} = require("util"); | ||
const tmp = require("tmp"); | ||
// file | ||
module.exports.fileSync = tmp.fileSync; | ||
var file = Promise.promisify(tmp.file, {multiArgs: true}); | ||
const fileWithOptions = promisify( | ||
(options, cb) => tmp.file( | ||
options, | ||
(err, path, fd, cleanup) => cb(err, {path, fd, cleanup: promisify(cleanup)}) | ||
) | ||
); | ||
module.exports.file = async (options) => fileWithOptions(options); | ||
module.exports.file = function file$promise() { | ||
return file.apply(tmp, arguments).spread(function (path, fd, cleanup) { | ||
return {path: path, fd: fd, cleanup : cleanup }; | ||
}); | ||
module.exports.withFile = async function withFile(fn, options) { | ||
const { path, fd, cleanup } = await module.exports.file(options); | ||
try { | ||
return await fn({ path, fd }); | ||
} finally { | ||
await cleanup(); | ||
} | ||
}; | ||
module.exports.withFile = function withFile(fn) { | ||
var cleanup; | ||
var params = Array.prototype.slice.call(arguments, 1); | ||
return module.exports.file.apply(tmp, params).then(function context(o) { | ||
cleanup = o.cleanup; | ||
delete o.cleanup; | ||
return fn(o); | ||
}).finally(function () { | ||
// May not pass any arguments to cleanup() or it fails. | ||
if (cleanup) { | ||
cleanup(); | ||
} | ||
}); | ||
}; | ||
// directory | ||
module.exports.dirSync = tmp.dirSync; | ||
var dir = Promise.promisify(tmp.dir, {multiArgs: true}); | ||
const dirWithOptions = promisify( | ||
(options, cb) => tmp.dir( | ||
options, | ||
(err, path, cleanup) => cb(err, {path, cleanup: promisify(cleanup)}) | ||
) | ||
); | ||
module.exports.dir = async (options) => dirWithOptions(options); | ||
module.exports.dir = function dir$promise() { | ||
return dir.apply(tmp, arguments).spread(function (path, cleanup) { | ||
return {path: path, cleanup: cleanup}; | ||
}); | ||
module.exports.withDir = async function withDir(fn, options) { | ||
const { path, cleanup } = await module.exports.dir(options); | ||
try { | ||
return await fn({ path }); | ||
} finally { | ||
await cleanup(); | ||
} | ||
}; | ||
module.exports.withDir = function withDir(fn) { | ||
var cleanup; | ||
var params = Array.prototype.slice.call(arguments, 1); | ||
return module.exports.dir.apply(tmp, params).then(function context(o) { | ||
cleanup = o.cleanup; | ||
delete o.cleanup; | ||
return fn(o); | ||
}).finally(function () { | ||
// May not pass any arguments to cleanup() or it fails. | ||
if (cleanup) { | ||
cleanup(); | ||
} | ||
}); | ||
}; | ||
// name generation | ||
module.exports.tmpNameSync = tmp.tmpNameSync; | ||
module.exports.tmpName = Promise.promisify(tmp.tmpName); | ||
module.exports.tmpName = promisify(tmp.tmpName); | ||
@@ -65,0 +48,0 @@ module.exports.tmpdir = tmp.tmpdir; |
{ | ||
"name": "tmp-promise", | ||
"version": "1.1.0", | ||
"version": "2.0.1", | ||
"description": "The tmp package with promises support and disposers.", | ||
@@ -8,3 +8,5 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"mocha": "mocha", | ||
"check-types": "tsd", | ||
"test": "npm run mocha && npm run check-types" | ||
}, | ||
@@ -25,9 +27,9 @@ "keywords": [ | ||
"dependencies": { | ||
"bluebird": "^3.5.0", | ||
"tmp": "0.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/tmp": "0.0.33", | ||
"mocha": "^3.1.2" | ||
"@types/tmp": "0.1.0", | ||
"mocha": "^6.1.4", | ||
"tsd": "^0.7.2" | ||
} | ||
} |
@@ -13,5 +13,10 @@ # tmp-promise | ||
**Note:** Node.js 8+ is supported - older versions of Node.js are not supported by the Node.js foundation. If you need to use an older version of Node.js install tmp-promise@1.10 | ||
npm i tmp-promise@1.1.0 | ||
## About | ||
This adds promises support to a [widely used library][2]. This package is used to create temporary files and directories in a [Node.js][1] environment. | ||
@@ -18,0 +23,0 @@ tmp-promise offers both an asynchronous and a synchronous API. For all API calls, all |
102
test.js
@@ -1,11 +0,91 @@ | ||
var accessSync = require('fs').accessSync | ||
var assert = require('assert') | ||
var extname = require('path').extname | ||
var existsSync = require('fs').existsSync | ||
const fs = require('fs') | ||
const {promisify} = require('util') | ||
const assert = require('assert') | ||
const {extname} = require('path') | ||
var tmp = require('.') | ||
const tmp = require('.') | ||
async function checkFileResult(result) { | ||
assert.deepEqual(Object.keys(result).sort(), ['cleanup', 'fd', 'path']) | ||
describe('withFile', function() | ||
const { path, fd, cleanup } = result | ||
assert.ok(typeof path === 'string') | ||
assert.ok(typeof fd === 'number') | ||
assert.ok(typeof cleanup === 'function') | ||
// Check that the path is a fille. | ||
assert.ok(fs.statSync(path).isFile()) | ||
// Check that the fd is correct and points to the file. | ||
const message = 'hello there!' | ||
fs.writeSync(fd, message) | ||
fs.fdatasyncSync(fd) | ||
assert.equal(fs.readFileSync(path), message) | ||
// Check that the cleanup works. | ||
await cleanup() | ||
assert.throws(() => fs.statSync(path)) | ||
} | ||
describe('file()', function() | ||
{ | ||
context('when called without options', function() | ||
{ | ||
it('creates the file, returns the expected result, and the cleanup function works', async function() | ||
{ | ||
const result = await tmp.file() | ||
await checkFileResult(result) | ||
}) | ||
}) | ||
context('when called with options', function() | ||
{ | ||
it('creates the file, returns the expected result, and the cleanup function works', async function() | ||
{ | ||
const prefix = 'myTmpDir_' | ||
const result = await tmp.file({ prefix }) | ||
await checkFileResult(result) | ||
assert.ok(result.path.includes(prefix)) | ||
}) | ||
}) | ||
}) | ||
async function checkDirResult(result) { | ||
assert.deepEqual(Object.keys(result).sort(), ['cleanup', 'path']) | ||
const { path, cleanup } = result | ||
assert.ok(typeof path === 'string') | ||
assert.ok(typeof cleanup === 'function') | ||
assert.ok(fs.statSync(path).isDirectory()) | ||
await cleanup() | ||
assert.throws(() => fs.statSync(path)) | ||
} | ||
describe('dir()', function() | ||
{ | ||
context('when called without options', function() | ||
{ | ||
it('creates the directory, returns the expected result, and the cleanup function works', async function() | ||
{ | ||
const result = await tmp.dir() | ||
await checkDirResult(result) | ||
}) | ||
}) | ||
context('when called with options', function() | ||
{ | ||
it('creates the directory, returns the expected result, and the cleanup function works', async function() | ||
{ | ||
const prefix = 'myTmpDir_' | ||
const result = await tmp.dir({ prefix }) | ||
await checkDirResult(result) | ||
assert.ok(result.path.includes(prefix)) | ||
}) | ||
}) | ||
}) | ||
describe('withFile()', function() | ||
{ | ||
it("file doesn't exist after going out of scope", function() | ||
@@ -19,3 +99,3 @@ { | ||
accessSync(filepath) | ||
fs.accessSync(filepath) | ||
assert.strictEqual(extname(filepath), '.txt') | ||
@@ -27,3 +107,3 @@ }, {discardDescriptor: true, postfix: '.txt'}) | ||
{ | ||
accessSync(filepath) | ||
fs.accessSync(filepath) | ||
}, filepath + ' still exists') | ||
@@ -35,3 +115,3 @@ }) | ||
describe('withDir', function() | ||
describe('withDir()', function() | ||
{ | ||
@@ -46,3 +126,3 @@ it("dir doesn't exist after going out of scope", function() | ||
accessSync(filepath) | ||
fs.accessSync(filepath) | ||
assert.strictEqual(extname(filepath), '.dir') | ||
@@ -54,3 +134,3 @@ }, {postfix: '.dir'}) | ||
{ | ||
accessSync(filepath) | ||
fs.accessSync(filepath) | ||
}, filepath + ' still exists') | ||
@@ -57,0 +137,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15873
1
7
209
314
1
3
1
- Removedbluebird@^3.5.0
- Removedbluebird@3.7.2(transitive)