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

BREAKING CHANGE: Promisify the cleanup function #32

Merged
merged 1 commit into from
May 21, 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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FileOptions, DirOptions, TmpNameOptions } from "tmp";

export interface DirectoryResult {
path: string;
cleanup(): void;
cleanup(): Promise<void>;
}

export interface FileResult extends DirectoryResult {
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports.fileSync = tmp.fileSync;
const fileWithOptions = promisify(
(options, cb) => tmp.file(
options,
(err, path, fd, cleanup) => cb(err, {path, fd, cleanup})
(err, path, fd, cleanup) => cb(err, {path, fd, cleanup: promisify(cleanup)})
)
);
module.exports.file = async (options) => fileWithOptions(options);
Expand All @@ -16,7 +16,7 @@ module.exports.withFile = async function withFile(fn, options) {
try {
return await fn({ path, fd });
} finally {
await promisify(cleanup)();
await cleanup();
}
};

Expand All @@ -26,7 +26,7 @@ module.exports.dirSync = tmp.dirSync;
const dirWithOptions = promisify(
(options, cb) => tmp.dir(
options,
(err, path, cleanup) => cb(err, {path, cleanup})
(err, path, cleanup) => cb(err, {path, cleanup: promisify(cleanup)})
)
);
module.exports.dir = async (options) => dirWithOptions(options);
Expand All @@ -36,7 +36,7 @@ module.exports.withDir = async function withDir(fn, options) {
try {
return await fn({ path });
} finally {
await promisify(cleanup)();
await cleanup();
}
};

Expand Down
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ import { file, withFile, dir, withDir, tmpName } from ".";

async function fileExample() {
const { path, fd, cleanup } = await file({ discardDescriptor: true });
await cleanup();

await withFile(
async ({ path, fd, cleanup }) => {
console.log(fd);
await cleanup();
},
{ discardDescriptor: true }
);
}

async function dirExample() {
const { path, cleanup } = await dir({ unsafeCleanup: true });
await cleanup();

await withDir(
async ({ path, cleanup }) => {
console.log(path);
await cleanup();
},
{ unsafeCleanup: true }
);
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function checkFileResult(result) {
assert.equal(fs.readFileSync(path), message)

// Check that the cleanup works.
await promisify(cleanup)()
await cleanup()
assert.throws(() => fs.statSync(path))
}

Expand Down Expand Up @@ -59,7 +59,7 @@ async function checkDirResult(result) {

assert.ok(fs.statSync(path).isDirectory())

await promisify(cleanup)()
await cleanup()
assert.throws(() => fs.statSync(path))
}

Expand Down