-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
32 lines (25 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {promisify} from 'node:util';
import path from 'node:path';
import stream from 'node:stream';
import {randomUUID} from 'node:crypto';
import fs from 'graceful-fs';
import {isStream} from 'is-stream';
import tempDirectory from 'temp-dir';
const writeFileP = promisify(fs.writeFile);
const mkdirP = promisify(fs.mkdir);
const pipelineP = promisify(stream.pipeline);
const tempfile = (filePath = '') => path.join(tempDirectory, randomUUID(), filePath);
const writeStream = async (filePath, data) => pipelineP(data, fs.createWriteStream(filePath));
export default async function tempWrite(fileContent, filePath) { // eslint-disable-line unicorn/prevent-abbreviations
const temporaryPath = tempfile(filePath);
const write = isStream(fileContent) ? writeStream : writeFileP;
await mkdirP(path.dirname(temporaryPath), {recursive: true});
await write(temporaryPath, fileContent);
return temporaryPath;
}
tempWrite.sync = (fileContent, filePath) => {
const temporaryPath = tempfile(filePath);
fs.mkdirSync(path.dirname(temporaryPath), {recursive: true});
fs.writeFileSync(temporaryPath, fileContent);
return temporaryPath;
};