1 | 'use strict';
|
2 | const {promisify} = require('util');
|
3 | const fs = require('graceful-fs');
|
4 | const makeDir = require('make-dir');
|
5 | const pEvent = require('p-event');
|
6 | const CpFileError = require('./cp-file-error');
|
7 |
|
8 | const stat = promisify(fs.stat);
|
9 | const lstat = promisify(fs.lstat);
|
10 | const utimes = promisify(fs.utimes);
|
11 | const chmod = promisify(fs.chmod);
|
12 | const chown = promisify(fs.chown);
|
13 |
|
14 | exports.closeSync = fs.closeSync.bind(fs);
|
15 | exports.createWriteStream = fs.createWriteStream.bind(fs);
|
16 |
|
17 | exports.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 |
|
29 | exports.stat = path => stat(path).catch(error => {
|
30 | throw new CpFileError(`Cannot stat path \`${path}\`: ${error.message}`, error);
|
31 | });
|
32 |
|
33 | exports.lstat = path => lstat(path).catch(error => {
|
34 | throw new CpFileError(`lstat \`${path}\` failed: ${error.message}`, error);
|
35 | });
|
36 |
|
37 | exports.utimes = (path, atime, mtime) => utimes(path, atime, mtime).catch(error => {
|
38 | throw new CpFileError(`utimes \`${path}\` failed: ${error.message}`, error);
|
39 | });
|
40 |
|
41 | exports.chmod = (path, mode) => chmod(path, mode).catch(error => {
|
42 | throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
|
43 | });
|
44 |
|
45 | exports.chown = (path, uid, gid) => chown(path, uid, gid).catch(error => {
|
46 | throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
|
47 | });
|
48 |
|
49 | exports.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 |
|
57 | exports.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 |
|
65 | exports.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 |
|
73 | exports.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 |
|
81 | exports.makeDir = path => makeDir(path, {fs}).catch(error => {
|
82 | throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
|
83 | });
|
84 |
|
85 | exports.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 |
|
93 | exports.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 | };
|