Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to util.promisify and async/await #25

Merged
merged 1 commit into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 31 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,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});

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 = 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();
}
});
module.exports.file = promisify(
(options, cb) => tmp.file(
options,
(err, path, fd, cleanup) => cb(err, {path, fd, 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 promisify(cleanup)();
}
};


// directory
module.exports.dirSync = tmp.dirSync;
var dir = Promise.promisify(tmp.dir, {multiArgs: true});

module.exports.dir = function dir$promise() {
return dir.apply(tmp, arguments).spread(function (path, cleanup) {
return {path: path, cleanup: 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();
}
});
module.exports.dir = promisify(
(options, cb) => tmp.dir(
options,
(err, path, cleanup) => cb(err, {path, cleanup})
)
);

module.exports.withDir = async function withDir(fn, options) {
const { path, cleanup } = await module.exports.dir(options);
try {
return await fn({ path });
} finally {
await promisify(cleanup)();
}
};


// name generation
module.exports.tmpNameSync = tmp.tmpNameSync;
module.exports.tmpName = Promise.promisify(tmp.tmpName);
module.exports.tmpName = promisify(tmp.tmpName);

module.exports.tmpdir = tmp.tmpdir;

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"url": "git://github.com/benjamingr/tmp-promise.git"
},
"dependencies": {
"bluebird": "^3.5.0",
"tmp": "0.1.0"
},
"devDependencies": {
Expand Down