UNPKG

2.84 kBJavaScriptView Raw
1'use strict';
2const {promisify} = require('util');
3const fs = require('graceful-fs');
4const makeDir = require('make-dir');
5const pEvent = require('p-event');
6const CpFileError = require('./cp-file-error');
7
8const stat = promisify(fs.stat);
9const lstat = promisify(fs.lstat);
10const utimes = promisify(fs.utimes);
11const chmod = promisify(fs.chmod);
12const chown = promisify(fs.chown);
13
14exports.closeSync = fs.closeSync.bind(fs);
15exports.createWriteStream = fs.createWriteStream.bind(fs);
16
17exports.createReadStream = async (path, options) => {
18 const read = fs.createReadStream(path, options);
19
20 try {
21 await pEvent(read, ['readable', 'end']);
22 } catch (error) {
23 throw new CpFileError(`Cannot read from \`${path}\`: ${error.message}`, error);
24 }
25
26 return read;
27};
28
29exports.stat = path => stat(path).catch(error => {
30 throw new CpFileError(`Cannot stat path \`${path}\`: ${error.message}`, error);
31});
32
33exports.lstat = path => lstat(path).catch(error => {
34 throw new CpFileError(`lstat \`${path}\` failed: ${error.message}`, error);
35});
36
37exports.utimes = (path, atime, mtime) => utimes(path, atime, mtime).catch(error => {
38 throw new CpFileError(`utimes \`${path}\` failed: ${error.message}`, error);
39});
40
41exports.chmod = (path, mode) => chmod(path, mode).catch(error => {
42 throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
43});
44
45exports.chown = (path, uid, gid) => chown(path, uid, gid).catch(error => {
46 throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
47});
48
49exports.statSync = path => {
50 try {
51 return fs.statSync(path);
52 } catch (error) {
53 throw new CpFileError(`stat \`${path}\` failed: ${error.message}`, error);
54 }
55};
56
57exports.utimesSync = (path, atime, mtime) => {
58 try {
59 return fs.utimesSync(path, atime, mtime);
60 } catch (error) {
61 throw new CpFileError(`utimes \`${path}\` failed: ${error.message}`, error);
62 }
63};
64
65exports.chmodSync = (path, mode) => {
66 try {
67 return fs.chmodSync(path, mode);
68 } catch (error) {
69 throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
70 }
71};
72
73exports.chownSync = (path, uid, gid) => {
74 try {
75 return fs.chownSync(path, uid, gid);
76 } catch (error) {
77 throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
78 }
79};
80
81exports.makeDir = path => makeDir(path, {fs}).catch(error => {
82 throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
83});
84
85exports.makeDirSync = path => {
86 try {
87 makeDir.sync(path, {fs});
88 } catch (error) {
89 throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
90 }
91};
92
93exports.copyFileSync = (source, destination, flags) => {
94 try {
95 fs.copyFileSync(source, destination, flags);
96 } catch (error) {
97 throw new CpFileError(`Cannot copy from \`${source}\` to \`${destination}\`: ${error.message}`, error);
98 }
99};