source: frontend/node_modules/tempy/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.5 KB
Line 
1'use strict';
2const fs = require('fs');
3const path = require('path');
4const uniqueString = require('unique-string');
5const tempDir = require('temp-dir');
6const isStream = require('is-stream');
7const stream = require('stream');
8const {promisify} = require('util');
9
10const pipeline = promisify(stream.pipeline);
11const {writeFile} = fs.promises;
12
13const getPath = (prefix = '') => path.join(tempDir, prefix + uniqueString());
14
15const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStream(filePath));
16
17module.exports.file = options => {
18 options = {
19 ...options
20 };
21
22 if (options.name) {
23 if (options.extension !== undefined && options.extension !== null) {
24 throw new Error('The `name` and `extension` options are mutually exclusive');
25 }
26
27 return path.join(module.exports.directory(), options.name);
28 }
29
30 return getPath() + (options.extension === undefined || options.extension === null ? '' : '.' + options.extension.replace(/^\./, ''));
31};
32
33module.exports.directory = ({prefix = ''} = {}) => {
34 const directory = getPath(prefix);
35 fs.mkdirSync(directory);
36 return directory;
37};
38
39module.exports.write = async (data, options) => {
40 const filename = module.exports.file(options);
41 const write = isStream(data) ? writeStream : writeFile;
42 await write(filename, data);
43 return filename;
44};
45
46module.exports.writeSync = (data, options) => {
47 const filename = module.exports.file(options);
48 fs.writeFileSync(filename, data);
49 return filename;
50};
51
52Object.defineProperty(module.exports, 'root', {
53 get() {
54 return tempDir;
55 }
56});
Note: See TracBrowser for help on using the repository browser.