UNPKG

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