UNPKG

2.01 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const path = tslib_1.__importStar(require("path"));
5const deps_1 = tslib_1.__importDefault(require("./deps"));
6const debug = require('debug')('heroku-cli:file');
7function exists(f) {
8 // debug('exists', f)
9 // @ts-ignore
10 return deps_1.default.fs.pathExists(f);
11}
12exports.exists = exists;
13async function stat(file) {
14 // debug('stat', file)
15 return deps_1.default.fs.stat(file);
16}
17exports.stat = stat;
18async function rename(from, to) {
19 debug('rename', from, to);
20 return deps_1.default.fs.rename(from, to);
21}
22exports.rename = rename;
23async function remove(file) {
24 if (!await exists(file))
25 return;
26 debug('remove', file);
27 return deps_1.default.fs.remove(file);
28}
29exports.remove = remove;
30async function ls(dir) {
31 let files = await deps_1.default.fs.readdir(dir);
32 let paths = files.map(f => path.join(dir, f));
33 return Promise.all(paths.map(path => deps_1.default.fs.stat(path).then(stat => ({ path, stat }))));
34}
35exports.ls = ls;
36async function removeEmptyDirs(dir) {
37 let files;
38 try {
39 files = await ls(dir);
40 }
41 catch (err) {
42 if (err.code === 'ENOENT')
43 return;
44 throw err;
45 }
46 let dirs = files.filter(f => f.stat.isDirectory()).map(f => f.path);
47 for (let p of dirs.map(removeEmptyDirs))
48 await p;
49 files = await ls(dir);
50 if (!files.length)
51 await remove(dir);
52}
53exports.removeEmptyDirs = removeEmptyDirs;
54async function readJSON(file) {
55 debug('readJSON', file);
56 return deps_1.default.fs.readJSON(file);
57}
58exports.readJSON = readJSON;
59async function outputJSON(file, data, options = {}) {
60 debug('outputJSON', file);
61 return deps_1.default.fs.outputJSON(file, data, Object.assign({ spaces: 2 }, options));
62}
63exports.outputJSON = outputJSON;
64function realpathSync(p) {
65 return deps_1.default.fs.realpathSync(p);
66}
67exports.realpathSync = realpathSync;