1 | import fs from 'node:fs';
|
2 | import fsPromises from 'node:fs/promises';
|
3 | import path from 'node:path';
|
4 | import stream from 'node:stream';
|
5 | import {promisify} from 'node:util';
|
6 | import uniqueString from 'unique-string';
|
7 | import tempDir from 'temp-dir';
|
8 | import {isStream} from 'is-stream';
|
9 |
|
10 | const pipeline = promisify(stream.pipeline);
|
11 |
|
12 | const getPath = (prefix = '') => path.join(tempDir, prefix + uniqueString());
|
13 |
|
14 | const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStream(filePath));
|
15 |
|
16 | async function runTask(temporaryPath, callback) {
|
17 | try {
|
18 | return await callback(temporaryPath);
|
19 | } finally {
|
20 | await fsPromises.rm(temporaryPath, {recursive: true, force: true, maxRetries: 2});
|
21 | }
|
22 | }
|
23 |
|
24 | export function temporaryFile({name, extension} = {}) {
|
25 | if (name) {
|
26 | if (extension !== undefined && extension !== null) {
|
27 | throw new Error('The `name` and `extension` options are mutually exclusive');
|
28 | }
|
29 |
|
30 | return path.join(temporaryDirectory(), name);
|
31 | }
|
32 |
|
33 | return getPath() + (extension === undefined || extension === null ? '' : '.' + extension.replace(/^\./, ''));
|
34 | }
|
35 |
|
36 | export const temporaryFileTask = async (callback, options) => runTask(temporaryFile(options), callback);
|
37 |
|
38 | export function temporaryDirectory({prefix = ''} = {}) {
|
39 | const directory = getPath(prefix);
|
40 | fs.mkdirSync(directory);
|
41 | return directory;
|
42 | }
|
43 |
|
44 | export const temporaryDirectoryTask = async (callback, options) => runTask(temporaryDirectory(options), callback);
|
45 |
|
46 | export async function temporaryWrite(fileContent, options) {
|
47 | const filename = temporaryFile(options);
|
48 | const write = isStream(fileContent) ? writeStream : fsPromises.writeFile;
|
49 | await write(filename, fileContent);
|
50 | return filename;
|
51 | }
|
52 |
|
53 | export const temporaryWriteTask = async (fileContent, callback, options) => runTask(await temporaryWrite(fileContent, options), callback);
|
54 |
|
55 | export function temporaryWriteSync(fileContent, options) {
|
56 | const filename = temporaryFile(options);
|
57 | fs.writeFileSync(filename, fileContent);
|
58 | return filename;
|
59 | }
|
60 |
|
61 | export {default as rootTemporaryDirectory} from 'temp-dir';
|