UNPKG

21.2 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15exports.mkdirSync = exports.mkdir = exports.linkSync = exports.link = exports.fsyncSync = exports.fsync = exports.createWriteStream = exports.createReadStream = exports.closeSync = exports.close = exports.lchownSync = exports.lchown = exports.fchownSync = exports.fchown = exports.chownSync = exports.chown = exports.lchmodSync = exports.lchmod = exports.fchmodSync = exports.fchmod = exports.chmodSync = exports.chmod = exports.accessSync = exports.access = exports.ensureWriteStreamSync = exports.ensureWriteStream = exports.ensurePathSync = exports.ensurePath = exports.watch = exports.rmdirSync = exports.rmdir = exports.emptyDirSync = exports.emptyDir = exports.readFileSync = exports.readFile = exports.escapeFileContent = exports.escapeBOM = exports.escapeEOL = exports.listDirSync = exports.listDir = exports.copyDir = exports.copyFile = exports.appendFileSync = exports.appendFile = exports.writeFileSync = exports.writeFile = exports.mkdirsSync = exports.mkdirs = exports.existsSync = exports.exists = void 0;
16exports.WriteStream = exports.ReadStream = exports.Stats = exports.writeSync = exports.write = exports.unwatchFile = exports.watchFile = exports.futimesSync = exports.futimes = exports.utimesSync = exports.utimes = exports.unlinkSync = exports.unlink = exports.ftruncateSync = exports.ftruncate = exports.truncateSync = exports.truncate = exports.lstatSync = exports.lstat = exports.fstatSync = exports.fstat = exports.statSync = exports.stat = exports.renameSync = exports.rename = exports.realpathSync = exports.realpath = exports.readlinkSync = exports.readlink = exports.readdirSync = exports.readdir = exports.readSync = exports.read = exports.symlinkSync = exports.symlink = exports.openSync = exports.open = void 0;
17const chokidar_1 = __importDefault(require("chokidar"));
18const bluebird_1 = __importDefault(require("bluebird"));
19const path_1 = require("path");
20const hexo_util_1 = require("hexo-util");
21const graceful_fs_1 = __importDefault(require("graceful-fs"));
22const fsPromises = graceful_fs_1.default.promises;
23const rEOL = /\r\n/g;
24function exists(path) {
25 if (!path)
26 throw new TypeError('path is required!');
27 const promise = fsPromises.access(path).then(() => true, error => {
28 if (error.code !== 'ENOENT')
29 throw error;
30 return false;
31 });
32 return bluebird_1.default.resolve(promise);
33}
34exports.exists = exists;
35function existsSync(path) {
36 if (!path)
37 throw new TypeError('path is required!');
38 try {
39 graceful_fs_1.default.accessSync(path);
40 }
41 catch (err) {
42 if (err.code !== 'ENOENT')
43 throw err;
44 return false;
45 }
46 return true;
47}
48exports.existsSync = existsSync;
49function mkdirs(path) {
50 if (!path)
51 throw new TypeError('path is required!');
52 return bluebird_1.default.resolve(fsPromises.mkdir(path, { recursive: true }));
53}
54exports.mkdirs = mkdirs;
55function mkdirsSync(path) {
56 if (!path)
57 throw new TypeError('path is required!');
58 graceful_fs_1.default.mkdirSync(path, { recursive: true });
59}
60exports.mkdirsSync = mkdirsSync;
61function checkParent(path) {
62 return bluebird_1.default.resolve(fsPromises.mkdir((0, path_1.dirname)(path), { recursive: true }));
63}
64function writeFile(path, data, options) {
65 if (!path)
66 throw new TypeError('path is required!');
67 if (!data)
68 data = '';
69 return checkParent(path)
70 .then(() => fsPromises.writeFile(path, data, options));
71}
72exports.writeFile = writeFile;
73function writeFileSync(path, data, options) {
74 if (!path)
75 throw new TypeError('path is required!');
76 graceful_fs_1.default.mkdirSync((0, path_1.dirname)(path), { recursive: true });
77 graceful_fs_1.default.writeFileSync(path, data, options);
78}
79exports.writeFileSync = writeFileSync;
80function appendFile(path, data, options) {
81 if (!path)
82 throw new TypeError('path is required!');
83 return checkParent(path)
84 .then(() => fsPromises.appendFile(path, data, options));
85}
86exports.appendFile = appendFile;
87function appendFileSync(path, data, options) {
88 if (!path)
89 throw new TypeError('path is required!');
90 graceful_fs_1.default.mkdirSync((0, path_1.dirname)(path), { recursive: true });
91 graceful_fs_1.default.appendFileSync(path, data, options);
92}
93exports.appendFileSync = appendFileSync;
94function copyFile(src, dest, flags) {
95 if (!src)
96 throw new TypeError('src is required!');
97 if (!dest)
98 throw new TypeError('dest is required!');
99 return checkParent(dest)
100 .then(() => fsPromises.copyFile(src, dest, flags));
101}
102exports.copyFile = copyFile;
103const trueFn = () => true;
104function ignoreHiddenFiles(ignore) {
105 if (!ignore)
106 return trueFn;
107 return ({ name }) => !name.startsWith('.');
108}
109function ignoreFilesRegex(regex) {
110 if (!regex)
111 return trueFn;
112 return ({ name }) => !regex.test(name);
113}
114function ignoreExcludeFiles(arr, parent) {
115 if (!arr || !arr.length)
116 return trueFn;
117 const set = new Set(arr);
118 return ({ name }) => !set.has((0, path_1.join)(parent, name));
119}
120function _readAndFilterDir(path, options = {}) {
121 return __awaiter(this, void 0, void 0, function* () {
122 const { ignoreHidden = true, ignorePattern } = options;
123 return (yield fsPromises.readdir(path, Object.assign(Object.assign({}, options), { withFileTypes: true })))
124 .filter(ignoreHiddenFiles(ignoreHidden))
125 .filter(ignoreFilesRegex(ignorePattern));
126 });
127}
128function _readAndFilterDirSync(path, options) {
129 const { ignoreHidden = true, ignorePattern } = options;
130 return graceful_fs_1.default.readdirSync(path, Object.assign(Object.assign({}, options), { withFileTypes: true }))
131 .filter(ignoreHiddenFiles(ignoreHidden))
132 .filter(ignoreFilesRegex(ignorePattern));
133}
134function _copyDirWalker(src, dest, results, parent, options) {
135 return __awaiter(this, void 0, void 0, function* () {
136 return bluebird_1.default.map(_readAndFilterDir(src, options), item => {
137 const childSrc = (0, path_1.join)(src, item.name);
138 const childDest = (0, path_1.join)(dest, item.name);
139 const currentPath = (0, path_1.join)(parent, item.name);
140 if (item.isDirectory()) {
141 return _copyDirWalker(childSrc, childDest, results, currentPath, options);
142 }
143 results.push(currentPath);
144 return copyFile(childSrc, childDest, 0);
145 });
146 });
147}
148function copyDir(src, dest, options = {}) {
149 if (!src)
150 throw new TypeError('src is required!');
151 if (!dest)
152 throw new TypeError('dest is required!');
153 const results = [];
154 return checkParent(dest)
155 .then(() => _copyDirWalker(src, dest, results, '', options))
156 .return(results);
157}
158exports.copyDir = copyDir;
159function _listDirWalker(path, results, parent, options) {
160 return __awaiter(this, void 0, void 0, function* () {
161 const promises = [];
162 for (const item of yield _readAndFilterDir(path, options)) {
163 const currentPath = (0, path_1.join)(parent, item.name);
164 if (item.isDirectory()) {
165 promises.push(_listDirWalker((0, path_1.join)(path, item.name), results, currentPath, options));
166 }
167 else {
168 results.push(currentPath);
169 }
170 }
171 yield bluebird_1.default.all(promises);
172 });
173}
174function listDir(path, options = {}) {
175 if (!path)
176 throw new TypeError('path is required!');
177 const results = [];
178 return bluebird_1.default.resolve(_listDirWalker(path, results, '', options))
179 .return(results);
180}
181exports.listDir = listDir;
182function _listDirSyncWalker(path, results, parent, options) {
183 for (const item of _readAndFilterDirSync(path, options)) {
184 const currentPath = (0, path_1.join)(parent, item.name);
185 if (item.isDirectory()) {
186 _listDirSyncWalker((0, path_1.join)(path, item.name), results, currentPath, options);
187 }
188 else {
189 results.push(currentPath);
190 }
191 }
192}
193function listDirSync(path, options = {}) {
194 if (!path)
195 throw new TypeError('path is required!');
196 const results = [];
197 _listDirSyncWalker(path, results, '', options);
198 return results;
199}
200exports.listDirSync = listDirSync;
201function escapeEOL(str) {
202 return str.replace(rEOL, '\n');
203}
204exports.escapeEOL = escapeEOL;
205function escapeBOM(str) {
206 return str.charCodeAt(0) === 0xFEFF ? str.substring(1) : str;
207}
208exports.escapeBOM = escapeBOM;
209function escapeFileContent(content) {
210 return escapeBOM(escapeEOL(content));
211}
212exports.escapeFileContent = escapeFileContent;
213function _readFile(path, options = {}) {
214 return __awaiter(this, void 0, void 0, function* () {
215 if (!Object.prototype.hasOwnProperty.call(options, 'encoding'))
216 options.encoding = 'utf8';
217 const content = yield fsPromises.readFile(path, options);
218 if (options.escape == null || options.escape) {
219 return escapeFileContent(content);
220 }
221 return content;
222 });
223}
224function readFile(path, options) {
225 if (!path)
226 throw new TypeError('path is required!');
227 return bluebird_1.default.resolve(_readFile(path, options));
228}
229exports.readFile = readFile;
230function readFileSync(path, options = {}) {
231 if (!path)
232 throw new TypeError('path is required!');
233 if (!Object.prototype.hasOwnProperty.call(options, 'encoding'))
234 options.encoding = 'utf8';
235 const content = graceful_fs_1.default.readFileSync(path, options);
236 if (options.escape == null || options.escape) {
237 return escapeFileContent(content);
238 }
239 return content;
240}
241exports.readFileSync = readFileSync;
242function _emptyDir(path, parent, options) {
243 return __awaiter(this, void 0, void 0, function* () {
244 const entries = (yield _readAndFilterDir(path, options)).filter(ignoreExcludeFiles(options.exclude, parent));
245 const results = [];
246 yield bluebird_1.default.map(entries, (item) => {
247 const fullPath = (0, path_1.join)(path, item.name);
248 const currentPath = (0, path_1.join)(parent, item.name);
249 if (item.isDirectory()) {
250 return _emptyDir(fullPath, currentPath, options).then(files => {
251 if (!files.length) {
252 return fsPromises.rmdir(fullPath);
253 }
254 results.push(...files);
255 });
256 }
257 results.push(currentPath);
258 return fsPromises.unlink(fullPath);
259 });
260 return results;
261 });
262}
263function emptyDir(path, options = {}) {
264 if (!path)
265 throw new TypeError('path is required!');
266 return bluebird_1.default.resolve(_emptyDir(path, '', options));
267}
268exports.emptyDir = emptyDir;
269function _emptyDirSync(path, options, parent) {
270 const entries = _readAndFilterDirSync(path, options)
271 .filter(ignoreExcludeFiles(options.exclude, parent));
272 const results = [];
273 for (const item of entries) {
274 const childPath = (0, path_1.join)(path, item.name);
275 const currentPath = (0, path_1.join)(parent, item.name);
276 if (item.isDirectory()) {
277 const removed = _emptyDirSync(childPath, options, currentPath);
278 if (!graceful_fs_1.default.readdirSync(childPath).length) {
279 rmdirSync(childPath);
280 }
281 results.push(...removed);
282 }
283 else {
284 graceful_fs_1.default.unlinkSync(childPath);
285 results.push(currentPath);
286 }
287 }
288 return results;
289}
290function emptyDirSync(path, options = {}) {
291 if (!path)
292 throw new TypeError('path is required!');
293 return _emptyDirSync(path, options, '');
294}
295exports.emptyDirSync = emptyDirSync;
296function _rmdir(path) {
297 return __awaiter(this, void 0, void 0, function* () {
298 const files = fsPromises.readdir(path, { withFileTypes: true });
299 yield bluebird_1.default.map(files, (item) => {
300 const childPath = (0, path_1.join)(path, item.name);
301 return item.isDirectory() ? _rmdir(childPath) : fsPromises.unlink(childPath);
302 });
303 return fsPromises.rmdir(path);
304 });
305}
306function rmdir(path) {
307 if (!path)
308 throw new TypeError('path is required!');
309 return bluebird_1.default.resolve(_rmdir(path));
310}
311exports.rmdir = rmdir;
312function _rmdirSync(path) {
313 const files = graceful_fs_1.default.readdirSync(path, { withFileTypes: true });
314 for (let i = 0, len = files.length; i < len; i++) {
315 const item = files[i];
316 const childPath = (0, path_1.join)(path, item.name);
317 if (item.isDirectory()) {
318 _rmdirSync(childPath);
319 }
320 else {
321 graceful_fs_1.default.unlinkSync(childPath);
322 }
323 }
324 graceful_fs_1.default.rmdirSync(path);
325}
326function rmdirSync(path) {
327 if (!path)
328 throw new TypeError('path is required!');
329 _rmdirSync(path);
330}
331exports.rmdirSync = rmdirSync;
332function watch(path, options) {
333 if (!path)
334 throw new TypeError('path is required!');
335 const watcher = chokidar_1.default.watch(path, options);
336 return new bluebird_1.default((resolve, reject) => {
337 watcher.on('ready', resolve);
338 watcher.on('error', reject);
339 }).thenReturn(watcher);
340}
341exports.watch = watch;
342function _findUnusedPath(path, files) {
343 const ext = (0, path_1.extname)(path);
344 const base = (0, path_1.basename)(path, ext);
345 const regex = new RegExp(`^${(0, hexo_util_1.escapeRegExp)(base)}(?:-(\\d+))?${(0, hexo_util_1.escapeRegExp)(ext)}$`);
346 let num = -1;
347 for (let i = 0, len = files.length; i < len; i++) {
348 const item = files[i];
349 const match = item.match(regex);
350 if (match == null)
351 continue;
352 const matchNum = match[1] ? parseInt(match[1], 10) : 0;
353 if (matchNum > num) {
354 num = matchNum;
355 }
356 }
357 return (0, path_1.join)((0, path_1.dirname)(path), `${base}-${num + 1}${ext}`);
358}
359function _ensurePath(path) {
360 return __awaiter(this, void 0, void 0, function* () {
361 if (!(yield exists(path)))
362 return path;
363 const files = yield fsPromises.readdir((0, path_1.dirname)(path));
364 return _findUnusedPath(path, files);
365 });
366}
367function ensurePath(path) {
368 if (!path)
369 throw new TypeError('path is required!');
370 return bluebird_1.default.resolve(_ensurePath(path));
371}
372exports.ensurePath = ensurePath;
373function ensurePathSync(path) {
374 if (!path)
375 throw new TypeError('path is required!');
376 if (!graceful_fs_1.default.existsSync(path))
377 return path;
378 const files = graceful_fs_1.default.readdirSync((0, path_1.dirname)(path));
379 return _findUnusedPath(path, files);
380}
381exports.ensurePathSync = ensurePathSync;
382function ensureWriteStream(path, options) {
383 if (!path)
384 throw new TypeError('path is required!');
385 return checkParent(path)
386 .then(() => graceful_fs_1.default.createWriteStream(path, options));
387}
388exports.ensureWriteStream = ensureWriteStream;
389function ensureWriteStreamSync(path, options) {
390 if (!path)
391 throw new TypeError('path is required!');
392 graceful_fs_1.default.mkdirSync((0, path_1.dirname)(path), { recursive: true });
393 return graceful_fs_1.default.createWriteStream(path, options);
394}
395exports.ensureWriteStreamSync = ensureWriteStreamSync;
396// access
397['F_OK', 'R_OK', 'W_OK', 'X_OK'].forEach(key => {
398 Object.defineProperty(exports, key, {
399 enumerable: true,
400 value: graceful_fs_1.default.constants[key],
401 writable: false
402 });
403});
404exports.access = bluebird_1.default.promisify(graceful_fs_1.default.access);
405exports.accessSync = graceful_fs_1.default.accessSync;
406// chmod
407exports.chmod = bluebird_1.default.promisify(graceful_fs_1.default.chmod);
408exports.chmodSync = graceful_fs_1.default.chmodSync;
409exports.fchmod = bluebird_1.default.promisify(graceful_fs_1.default.fchmod);
410exports.fchmodSync = graceful_fs_1.default.fchmodSync;
411exports.lchmod = bluebird_1.default.promisify(graceful_fs_1.default.lchmod);
412exports.lchmodSync = graceful_fs_1.default.lchmodSync;
413// chown
414exports.chown = bluebird_1.default.promisify(graceful_fs_1.default.chown);
415exports.chownSync = graceful_fs_1.default.chownSync;
416exports.fchown = bluebird_1.default.promisify(graceful_fs_1.default.fchown);
417exports.fchownSync = graceful_fs_1.default.fchownSync;
418exports.lchown = bluebird_1.default.promisify(graceful_fs_1.default.lchown);
419exports.lchownSync = graceful_fs_1.default.lchownSync;
420// close
421exports.close = bluebird_1.default.promisify(graceful_fs_1.default.close);
422exports.closeSync = graceful_fs_1.default.closeSync;
423// createStream
424exports.createReadStream = graceful_fs_1.default.createReadStream;
425exports.createWriteStream = graceful_fs_1.default.createWriteStream;
426// fsync
427exports.fsync = bluebird_1.default.promisify(graceful_fs_1.default.fsync);
428exports.fsyncSync = graceful_fs_1.default.fsyncSync;
429// link
430exports.link = bluebird_1.default.promisify(graceful_fs_1.default.link);
431exports.linkSync = graceful_fs_1.default.linkSync;
432// mkdir
433exports.mkdir = bluebird_1.default.promisify(graceful_fs_1.default.mkdir);
434exports.mkdirSync = graceful_fs_1.default.mkdirSync;
435// open
436exports.open = bluebird_1.default.promisify(graceful_fs_1.default.open);
437exports.openSync = graceful_fs_1.default.openSync;
438// symlink
439exports.symlink = bluebird_1.default.promisify(graceful_fs_1.default.symlink);
440exports.symlinkSync = graceful_fs_1.default.symlinkSync;
441// read
442exports.read = bluebird_1.default.promisify(graceful_fs_1.default.read);
443exports.readSync = graceful_fs_1.default.readSync;
444// readdir
445exports.readdir = bluebird_1.default.promisify(graceful_fs_1.default.readdir);
446exports.readdirSync = graceful_fs_1.default.readdirSync;
447// readlink
448exports.readlink = bluebird_1.default.promisify(graceful_fs_1.default.readlink);
449exports.readlinkSync = graceful_fs_1.default.readlinkSync;
450// realpath
451exports.realpath = bluebird_1.default.promisify(graceful_fs_1.default.realpath);
452exports.realpathSync = graceful_fs_1.default.realpathSync;
453// rename
454exports.rename = bluebird_1.default.promisify(graceful_fs_1.default.rename);
455exports.renameSync = graceful_fs_1.default.renameSync;
456// stat
457exports.stat = bluebird_1.default.promisify(graceful_fs_1.default.stat);
458exports.statSync = graceful_fs_1.default.statSync;
459exports.fstat = bluebird_1.default.promisify(graceful_fs_1.default.fstat);
460exports.fstatSync = graceful_fs_1.default.fstatSync;
461exports.lstat = bluebird_1.default.promisify(graceful_fs_1.default.lstat);
462exports.lstatSync = graceful_fs_1.default.lstatSync;
463// truncate
464exports.truncate = bluebird_1.default.promisify(graceful_fs_1.default.truncate);
465exports.truncateSync = graceful_fs_1.default.truncateSync;
466exports.ftruncate = bluebird_1.default.promisify(graceful_fs_1.default.ftruncate);
467exports.ftruncateSync = graceful_fs_1.default.ftruncateSync;
468// unlink
469exports.unlink = bluebird_1.default.promisify(graceful_fs_1.default.unlink);
470exports.unlinkSync = graceful_fs_1.default.unlinkSync;
471// utimes
472exports.utimes = bluebird_1.default.promisify(graceful_fs_1.default.utimes);
473exports.utimesSync = graceful_fs_1.default.utimesSync;
474exports.futimes = bluebird_1.default.promisify(graceful_fs_1.default.futimes);
475exports.futimesSync = graceful_fs_1.default.futimesSync;
476// watch
477exports.watchFile = graceful_fs_1.default.watchFile;
478exports.unwatchFile = graceful_fs_1.default.unwatchFile;
479// write
480exports.write = bluebird_1.default.promisify(graceful_fs_1.default.write);
481exports.writeSync = graceful_fs_1.default.writeSync;
482// Static classes
483exports.Stats = graceful_fs_1.default.Stats;
484exports.ReadStream = graceful_fs_1.default.ReadStream;
485exports.WriteStream = graceful_fs_1.default.WriteStream;
486//# sourceMappingURL=fs.js.map
\No newline at end of file