1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const path = tslib_1.__importStar(require("path"));
|
5 | const deps_1 = tslib_1.__importDefault(require("./deps"));
|
6 | const debug = require('debug')('heroku-cli:file');
|
7 | function exists(f) {
|
8 |
|
9 |
|
10 | return deps_1.default.fs.pathExists(f);
|
11 | }
|
12 | exports.exists = exists;
|
13 | async function stat(file) {
|
14 |
|
15 | return deps_1.default.fs.stat(file);
|
16 | }
|
17 | exports.stat = stat;
|
18 | async function rename(from, to) {
|
19 | debug('rename', from, to);
|
20 | return deps_1.default.fs.rename(from, to);
|
21 | }
|
22 | exports.rename = rename;
|
23 | async function remove(file) {
|
24 | if (!await exists(file))
|
25 | return;
|
26 | debug('remove', file);
|
27 | return deps_1.default.fs.remove(file);
|
28 | }
|
29 | exports.remove = remove;
|
30 | async 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 | }
|
35 | exports.ls = ls;
|
36 | async 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 | }
|
53 | exports.removeEmptyDirs = removeEmptyDirs;
|
54 | async function readJSON(file) {
|
55 | debug('readJSON', file);
|
56 | return deps_1.default.fs.readJSON(file);
|
57 | }
|
58 | exports.readJSON = readJSON;
|
59 | async function outputJSON(file, data, options = {}) {
|
60 | debug('outputJSON', file);
|
61 | return deps_1.default.fs.outputJSON(file, data, Object.assign({ spaces: 2 }, options));
|
62 | }
|
63 | exports.outputJSON = outputJSON;
|
64 | function realpathSync(p) {
|
65 | return deps_1.default.fs.realpathSync(p);
|
66 | }
|
67 | exports.realpathSync = realpathSync;
|