1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const crypto = require("crypto");
|
4 | const fs = require("fs-extra");
|
5 | const path = require("path");
|
6 | const util_1 = require("util");
|
7 | const log_1 = require("./log");
|
8 | const path_1 = require("./path");
|
9 | const deps = {
|
10 | m: {},
|
11 | get tmp() { return this.m.tmp = this.m.globby || require('tmp'); },
|
12 | get globby() { return this.m.globby = this.m.globby || require('globby'); },
|
13 | };
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | async function mkdirp(...filepaths) {
|
19 | for (let f of filepaths.map(path_1.join)) {
|
20 | log_1.log('mkdirp', f);
|
21 | await fs.mkdirp(f);
|
22 | }
|
23 | }
|
24 | exports.mkdirp = mkdirp;
|
25 | (function (mkdirp) {
|
26 | function sync(...filepaths) {
|
27 | for (let f of filepaths.map(path_1.join)) {
|
28 | log_1.log('mkdirpSync', f);
|
29 | fs.mkdirpSync(f);
|
30 | }
|
31 | }
|
32 | mkdirp.sync = sync;
|
33 | })(mkdirp = exports.mkdirp || (exports.mkdirp = {}));
|
34 |
|
35 |
|
36 |
|
37 | function globby(patterns, options = {}) {
|
38 | log_1.log('globby', ...patterns);
|
39 | return deps.globby(patterns, options);
|
40 | }
|
41 | exports.globby = globby;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function write(filepaths, data, options = {}) {
|
47 | const filepath = path_1.join(filepaths);
|
48 | log_1.log('write', filepath);
|
49 | return fs.outputFile(filepath, data, options);
|
50 | }
|
51 | exports.write = write;
|
52 |
|
53 |
|
54 |
|
55 | function read(filepaths, options = {}) {
|
56 | const filepath = path_1.join(filepaths);
|
57 | log_1.log('read', filepath);
|
58 | return fs.readFile(filepath, Object.assign({ encoding: 'utf8' }, options));
|
59 | }
|
60 | exports.read = read;
|
61 |
|
62 |
|
63 |
|
64 | async function ls(filepaths, options = {}) {
|
65 | const filepath = path_1.join(filepaths);
|
66 |
|
67 | const files = await fs.readdir(filepath);
|
68 | if (options.fullpath)
|
69 | return files.map(f => path_1.join([filepath, f]));
|
70 | else
|
71 | return files;
|
72 | }
|
73 | exports.ls = ls;
|
74 | async function fileType(fp) {
|
75 | try {
|
76 | const stats = await fs.stat(path_1.join(fp));
|
77 | if (stats.isSymbolicLink())
|
78 | return 'symlink';
|
79 | if (stats.isDirectory())
|
80 | return 'directory';
|
81 | if (stats.isFile())
|
82 | return 'file';
|
83 | }
|
84 | catch (err) {
|
85 | if (err.code === 'ENOENT')
|
86 | return;
|
87 | throw err;
|
88 | }
|
89 | }
|
90 | exports.fileType = fileType;
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | async function cp(source, destinationpaths, options = {}) {
|
96 | source = path_1.join(source);
|
97 | let dest = path_1.join(destinationpaths);
|
98 | switch (await fileType(dest)) {
|
99 | case 'directory':
|
100 | dest = path.join(dest, path.basename(source));
|
101 | break;
|
102 | case 'file':
|
103 | await rm(dest);
|
104 | }
|
105 | log_1.log('cp', source, dest);
|
106 | return fs.copy(source, dest, options);
|
107 | }
|
108 | exports.cp = cp;
|
109 |
|
110 |
|
111 |
|
112 | async function rm(...filesArray) {
|
113 | for (let f of filesArray.map(path_1.join)) {
|
114 | log_1.log('rm', f);
|
115 | await fs.remove(f);
|
116 | }
|
117 | }
|
118 | exports.rm = rm;
|
119 | async function rmIfEmpty(...filesArray) {
|
120 | const rmdir = async (f) => {
|
121 | let removedSomething = false;
|
122 | const getFiles = async () => (await ls(f)).map(s => path_1.join([f, s]));
|
123 | let files = await getFiles();
|
124 | for (let subdir of files) {
|
125 | if ((await fileType(subdir)) === 'directory') {
|
126 | await rmdir(subdir);
|
127 | removedSomething = true;
|
128 | }
|
129 | }
|
130 |
|
131 | if (removedSomething)
|
132 | files = await getFiles();
|
133 | if (files.length === 0)
|
134 | await rm(f);
|
135 | };
|
136 | for (let f of filesArray.map(path_1.join)) {
|
137 | log_1.log('rmIfEmpty', f);
|
138 | await rmdir(f);
|
139 | }
|
140 | }
|
141 | exports.rmIfEmpty = rmIfEmpty;
|
142 | async function mv(source, dest) {
|
143 | source = path_1.join(source);
|
144 | dest = path_1.join(dest);
|
145 | switch (await fileType(dest)) {
|
146 | case 'directory':
|
147 | dest = path.join(dest, path.basename(source));
|
148 | break;
|
149 | case 'file':
|
150 | await rm(dest);
|
151 | }
|
152 | log_1.log('mv', source, dest);
|
153 | return fs.move(source, dest);
|
154 | }
|
155 | exports.mv = mv;
|
156 | async function exists(filepath) {
|
157 | filepath = path_1.join(filepath);
|
158 | const exists = await fs.pathExists(filepath);
|
159 | log_1.log('exists', filepath, exists);
|
160 | return exists;
|
161 | }
|
162 | exports.exists = exists;
|
163 | (function (exists_1) {
|
164 | function sync(filepath) {
|
165 | filepath = path_1.join(filepath);
|
166 | const exists = fs.pathExistsSync(filepath);
|
167 | log_1.log('exists.sync', filepath, exists);
|
168 | return exists;
|
169 | }
|
170 | exists_1.sync = sync;
|
171 | })(exists = exports.exists || (exports.exists = {}));
|
172 | function chmod(filepath, mode) {
|
173 | filepath = path_1.join(filepath);
|
174 | log_1.log('chmod', filepath, mode.toString(8));
|
175 | return fs.chmod(filepath, mode);
|
176 | }
|
177 | exports.chmod = chmod;
|
178 | function ln(from, to) {
|
179 | from = path_1.join(from);
|
180 | to = path_1.join(to);
|
181 | log_1.log('ln', from, to);
|
182 | return fs.link(from, to);
|
183 | }
|
184 | exports.ln = ln;
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | async function tmpDir() {
|
190 | const output = await util_1.promisify(deps.tmp.dir)();
|
191 | log_1.log('tmpDir', output.name);
|
192 | return output.name;
|
193 | }
|
194 | exports.tmpDir = tmpDir;
|
195 | async function emptyDir(filepath) {
|
196 | filepath = path_1.join(filepath);
|
197 | log_1.log('emptyDir', filepath);
|
198 | await fs.mkdirp(path.dirname(filepath));
|
199 | return fs.emptyDir(filepath);
|
200 | }
|
201 | exports.emptyDir = emptyDir;
|
202 | async function hash(algo, fp) {
|
203 | const f = path_1.join(fp);
|
204 | log_1.log('hash', algo, f);
|
205 | return new Promise((resolve, reject) => {
|
206 | const hash = crypto.createHash(algo);
|
207 | const stream = fs.createReadStream(f);
|
208 | stream.on('error', err => reject(err));
|
209 | stream.on('data', chunk => hash.update(chunk));
|
210 | stream.on('end', () => resolve(hash.digest('hex')));
|
211 | });
|
212 | }
|
213 | exports.hash = hash;
|