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