1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.fs = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const fs_1 = tslib_1.__importDefault(require("fs"));
|
6 | const util_1 = tslib_1.__importDefault(require("util"));
|
7 | const path_1 = tslib_1.__importDefault(require("path"));
|
8 | const utils_1 = require("./utils");
|
9 | const ENOENT = 'ENOENT';
|
10 | const EEXIST = 'EEXIST';
|
11 | function copy(src, dist, opts) {
|
12 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
13 | opts = Object.assign({ override: true }, opts);
|
14 | const srcStat = yield exports.fs.stat(src);
|
15 | const isSkiped = opts.skip ? yield opts.skip(src, srcStat) : false;
|
16 | if (isSkiped)
|
17 | return;
|
18 |
|
19 | if (srcStat.isDirectory()) {
|
20 | yield exports.fs.mkdirp(dist);
|
21 | let childs = yield exports.fs.readdir(src);
|
22 | yield Promise.all(childs.map(child => copy(path_1.default.join(src, child), path_1.default.join(dist, child), opts)));
|
23 | }
|
24 | else if (srcStat.isFile() || srcStat.isSymbolicLink()) {
|
25 | if (yield exports.fs.lexists(dist)) {
|
26 | let isDir = yield exports.fs.isDirectory(dist);
|
27 | if (isDir) {
|
28 | dist = path_1.default.join(dist, path_1.default.basename(src));
|
29 | }
|
30 | else if (opts.override) {
|
31 | yield exports.fs.rmrf(dist);
|
32 | }
|
33 | else {
|
34 | return;
|
35 | }
|
36 | }
|
37 | else {
|
38 | let lastChar = dist[dist.length - 1];
|
39 | if (lastChar === '/' || lastChar === '\\') {
|
40 | yield exports.fs.mkdirp(dist);
|
41 | dist = path_1.default.join(dist, path_1.default.basename(src));
|
42 | }
|
43 | else {
|
44 | let dir = path_1.default.dirname(dist);
|
45 | yield exports.fs.mkdirp(dir);
|
46 | }
|
47 | }
|
48 | yield exports.fs.copyFile(src, dist);
|
49 | }
|
50 | });
|
51 | }
|
52 | function watchDir(dir, options, cb) {
|
53 | if (utils_1.Is.fn(options)) {
|
54 | cb = options;
|
55 | options = void 0;
|
56 | }
|
57 | options = Object.assign({ persistent: true, threshold: 300 }, options);
|
58 | cb = cb && utils_1.promiseQueue(cb);
|
59 | if (options.threshold) {
|
60 | cb = cb && utils_1.debounce(cb, options.threshold);
|
61 | }
|
62 | if (process.platform === 'linux') {
|
63 |
|
64 | exports.fs.iter(dir, (file) => {
|
65 | exports.fs.watch(file, {
|
66 | recursive: false,
|
67 | persistent: options.persistent,
|
68 | }, cb);
|
69 | });
|
70 | }
|
71 | else {
|
72 | exports.fs.watch(dir, { recursive: true, persistent: options.persistent }, cb);
|
73 | }
|
74 | }
|
75 | exports.fs = Object.assign(Object.assign({}, fs_1.default), { access: util_1.default.promisify(fs_1.default.access), open: util_1.default.promisify(fs_1.default.open), rename: util_1.default.promisify(fs_1.default.rename), truncate: util_1.default.promisify(fs_1.default.truncate), rmdir: util_1.default.promisify(fs_1.default.rmdir), mkdir: util_1.default.promisify(fs_1.default.mkdir), readdir: util_1.default.promisify(fs_1.default.readdir), readlink: util_1.default.promisify(fs_1.default.readlink), symlink: util_1.default.promisify(fs_1.default.symlink), lstat: util_1.default.promisify(fs_1.default.lstat), stat: util_1.default.promisify(fs_1.default.stat), link: util_1.default.promisify(fs_1.default.link), unlink: util_1.default.promisify(fs_1.default.unlink), chmod: util_1.default.promisify(fs_1.default.chmod), chown: util_1.default.promisify(fs_1.default.chown), utimes: util_1.default.promisify(fs_1.default.utimes), realpath: util_1.default.promisify(fs_1.default.realpath), mkdtemp: util_1.default.promisify(fs_1.default.mkdtemp), writeFile: util_1.default.promisify(fs_1.default.writeFile), appendFile: util_1.default.promisify(fs_1.default.appendFile), readFile: util_1.default.promisify(fs_1.default.readFile), existsSync: fs_1.default.existsSync, createReadString: fs_1.default.createReadStream, createWriteStream: fs_1.default.createWriteStream, constants: fs_1.default.constants, watchDir,
|
76 | copy, exists(path) {
|
77 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
78 | try {
|
79 | yield exports.fs.stat(path);
|
80 | }
|
81 | catch (error) {
|
82 | if (error.code === ENOENT) {
|
83 | return false;
|
84 | }
|
85 | else {
|
86 | throw error;
|
87 | }
|
88 | }
|
89 | return true;
|
90 | });
|
91 | },
|
92 |
|
93 | lexists(path) {
|
94 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
95 | try {
|
96 | yield exports.fs.lstat(path);
|
97 | }
|
98 | catch (error) {
|
99 | if (error.code === ENOENT) {
|
100 | return false;
|
101 | }
|
102 | else {
|
103 | throw error;
|
104 | }
|
105 | }
|
106 | return true;
|
107 | });
|
108 | },
|
109 | isFile(path) {
|
110 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
111 | try {
|
112 | return (yield exports.fs.lstat(path)).isFile();
|
113 | }
|
114 | catch (error) {
|
115 | if (error.code === ENOENT) {
|
116 | return false;
|
117 | }
|
118 | else {
|
119 | throw error;
|
120 | }
|
121 | }
|
122 | });
|
123 | },
|
124 | isDirectory(path) {
|
125 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
126 | try {
|
127 | return (yield exports.fs.lstat(path)).isDirectory();
|
128 | }
|
129 | catch (error) {
|
130 | if (error.code === ENOENT) {
|
131 | return false;
|
132 | }
|
133 | else {
|
134 | throw error;
|
135 | }
|
136 | }
|
137 | });
|
138 | },
|
139 | isSymbolicLink(path) {
|
140 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
141 | try {
|
142 | return (yield exports.fs.lstat(path)).isSymbolicLink();
|
143 | }
|
144 | catch (error) {
|
145 | if (error.code === ENOENT) {
|
146 | return false;
|
147 | }
|
148 | else {
|
149 | throw error;
|
150 | }
|
151 | }
|
152 | });
|
153 | }, copyFile: fs_1.default.copyFile ? util_1.default.promisify(fs_1.default.copyFile) : (src, dist) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
154 | yield exports.fs.stat(src);
|
155 | return new Promise((res, rej) => {
|
156 | exports.fs
|
157 | .createReadStream(src, { highWaterMark: 2 * 1024 * 1024 })
|
158 | .pipe(exports.fs.createWriteStream(dist))
|
159 | .on('error', rej)
|
160 | .on('close', res);
|
161 | });
|
162 | }),
|
163 | |
164 |
|
165 |
|
166 |
|
167 | mkdirp(dir) {
|
168 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
169 | let parent = path_1.default.dirname(dir);
|
170 | if (!(yield exports.fs.exists(parent))) {
|
171 | yield exports.fs.mkdirp(parent);
|
172 | }
|
173 | return exports.fs.mkdir(dir).catch(error => {
|
174 | if (error.code !== EEXIST) {
|
175 | throw error;
|
176 | }
|
177 | });
|
178 | });
|
179 | },
|
180 | |
181 |
|
182 |
|
183 |
|
184 | mkdirpSync(dir) {
|
185 | if (dir === '/')
|
186 | return;
|
187 | let parent = path_1.default.dirname(dir);
|
188 | if (!exports.fs.existsSync(parent)) {
|
189 | exports.fs.mkdirpSync(parent);
|
190 | }
|
191 | if (!exports.fs.existsSync(dir)) {
|
192 | try {
|
193 | exports.fs.mkdirSync(dir);
|
194 | }
|
195 | catch (error) {
|
196 | if (error.code !== EEXIST) {
|
197 | throw error;
|
198 | }
|
199 | }
|
200 | }
|
201 | },
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 | rmrf(path) {
|
208 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
209 | let stat;
|
210 | try {
|
211 | stat = yield exports.fs.lstat(path);
|
212 | }
|
213 | catch (error) {
|
214 | if (error.code === ENOENT) {
|
215 | return;
|
216 | }
|
217 | throw error;
|
218 | }
|
219 | if (stat.isDirectory()) {
|
220 | const children = yield exports.fs.readdir(path);
|
221 | yield Promise.all(children
|
222 | .map(child => path_1.default.join(path, child))
|
223 | .map(exports.fs.rmrf));
|
224 | yield exports.fs.rmdir(path);
|
225 | }
|
226 | else {
|
227 | yield exports.fs.unlink(path);
|
228 | }
|
229 | });
|
230 | },
|
231 | outputFile(path, data, options) {
|
232 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
233 | let dir = path_1.default.dirname(path);
|
234 | yield exports.fs.mkdirp(dir);
|
235 | return exports.fs.writeFile(path, data, options);
|
236 | });
|
237 | },
|
238 | outputFileSync(path, data, options) {
|
239 | let dir = path_1.default.dirname(path);
|
240 | exports.fs.mkdirpSync(dir);
|
241 | return exports.fs.writeFileSync(path, data, options);
|
242 | },
|
243 | outputJson(path, data, options) {
|
244 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
245 | const [replacer, space] = utils_1.Is.obj(options)
|
246 | ? [options.replacer, options.space]
|
247 | : [void 0, void 0];
|
248 | return exports.fs.outputFile(path, JSON.stringify(data, replacer, space), options);
|
249 | });
|
250 | },
|
251 | outputJsonSync(path, data, options) {
|
252 | const [replacer, space] = utils_1.Is.obj(options)
|
253 | ? [options.replacer, options.space]
|
254 | : [void 0, void 0];
|
255 | return exports.fs.outputFileSync(path, JSON.stringify(data, replacer, space), options);
|
256 | },
|
257 | readJson(path, options) {
|
258 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
259 | let data = yield exports.fs.readFile(path, options);
|
260 | if (!utils_1.Is.str(data)) {
|
261 | data = data.toString('utf8');
|
262 | }
|
263 | return JSON.parse(data);
|
264 | });
|
265 | },
|
266 | readJsonSync(path, options) {
|
267 | let data = exports.fs.readFileSync(path, options);
|
268 | if (!utils_1.Is.str(data)) {
|
269 | data = data.toString('utf8');
|
270 | }
|
271 | return JSON.parse(data);
|
272 | },
|
273 | iter(dir,
|
274 |
|
275 | skip) {
|
276 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
277 | let children = yield exports.fs.readdir(dir);
|
278 | yield Promise.all(children.map((child) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
279 | let path = path_1.default.join(dir, child);
|
280 | let stat = yield exports.fs.stat(path);
|
281 | let isSkipped = yield skip(path, stat);
|
282 | if (isSkipped)
|
283 | return;
|
284 | if (stat.isDirectory()) {
|
285 | yield exports.fs.iter(path, skip);
|
286 | }
|
287 | })));
|
288 | });
|
289 | } });
|
290 |
|
\ | No newline at end of file |