1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const fs = require("fs-extra");
|
5 | const path = require("path");
|
6 | const deps_1 = require("./deps");
|
7 | const debug = require('debug')('cli:file');
|
8 | function exists(f) {
|
9 |
|
10 |
|
11 | return fs.exists(f);
|
12 | }
|
13 | exports.exists = exists;
|
14 | function existsSync(f) {
|
15 | return fs.existsSync(f);
|
16 | }
|
17 | exports.existsSync = existsSync;
|
18 | const jsonFiles = {};
|
19 | function fetchJSONFile(f) {
|
20 | if (!jsonFiles[f]) {
|
21 | debug('fetchJSONFile', f);
|
22 | jsonFiles[f] = fs.readJSON(f);
|
23 | }
|
24 | return jsonFiles[f];
|
25 | }
|
26 | exports.fetchJSONFile = fetchJSONFile;
|
27 | function rename(from, to) {
|
28 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
29 | debug('rename', from, to);
|
30 | return fs.rename(from, to);
|
31 | });
|
32 | }
|
33 | exports.rename = rename;
|
34 | function mkdirp(dir) {
|
35 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
36 | debug('mkdirp', dir);
|
37 | return fs.mkdirp(dir);
|
38 | });
|
39 | }
|
40 | exports.mkdirp = mkdirp;
|
41 | function outputFile(file, data, options) {
|
42 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
43 | debug('outputFile', file);
|
44 | return fs.outputFile(file, data, options);
|
45 | });
|
46 | }
|
47 | exports.outputFile = outputFile;
|
48 | function outputFileSync(p, body) {
|
49 | debug('outputFileSync');
|
50 | return fs.outputFileSync(p, body);
|
51 | }
|
52 | exports.outputFileSync = outputFileSync;
|
53 | function outputJSON(file, data, options = {}) {
|
54 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
55 | debug('outputJSON', file);
|
56 | return fs.outputJSON(file, data, Object.assign({ spaces: 2 }, options));
|
57 | });
|
58 | }
|
59 | exports.outputJSON = outputJSON;
|
60 | function readJSON(file) {
|
61 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
62 | debug('readJSON', file);
|
63 | return fs.readJSON(file);
|
64 | });
|
65 | }
|
66 | exports.readJSON = readJSON;
|
67 | function readFile(file) {
|
68 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
69 | debug('read', file);
|
70 | return fs.readFile(file, 'utf8');
|
71 | });
|
72 | }
|
73 | exports.readFile = readFile;
|
74 | function remove(file) {
|
75 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
76 | if (!(yield exists(file)))
|
77 | return;
|
78 | debug('remove', file);
|
79 | return fs.remove(file);
|
80 | });
|
81 | }
|
82 | exports.remove = remove;
|
83 | function stat(file) {
|
84 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
85 | return fs.stat(file);
|
86 | });
|
87 | }
|
88 | exports.stat = stat;
|
89 | function open(path, flags, mode) {
|
90 | debug('open', path, flags, mode);
|
91 | return fs.open(path, flags, mode);
|
92 | }
|
93 | exports.open = open;
|
94 | function write(fd, data) {
|
95 | debug('write', fd);
|
96 |
|
97 | return fs.write(fd, data);
|
98 | }
|
99 | exports.write = write;
|
100 | function walk(root, opts = {}) {
|
101 | debug('walk', root);
|
102 | return new Promise((resolve, reject) => {
|
103 | const items = [];
|
104 | deps_1.default
|
105 | .klaw(root, Object.assign({}, opts, { depthLimit: 10000 }))
|
106 | .on('data', f => items.push(f))
|
107 | .on('error', reject)
|
108 | .on('end', () => resolve(items));
|
109 | });
|
110 | }
|
111 | exports.walk = walk;
|
112 | function ls(dir) {
|
113 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
114 | let files = yield fs.readdir(dir);
|
115 | let paths = files.map(f => path.join(dir, f));
|
116 | return Promise.all(paths.map(path => fs.stat(path).then(stat => ({ path, stat }))));
|
117 | });
|
118 | }
|
119 | exports.ls = ls;
|
120 | function cleanup(dir) {
|
121 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
122 | let files;
|
123 | try {
|
124 | files = yield ls(dir);
|
125 | }
|
126 | catch (err) {
|
127 | if (err.code === 'ENOENT')
|
128 | return;
|
129 | throw err;
|
130 | }
|
131 | let dirs = files.filter(f => f.stat.isDirectory()).map(f => f.path);
|
132 | for (let p of dirs.map(cleanup))
|
133 | yield p;
|
134 | files = yield ls(dir);
|
135 | if (!files.length)
|
136 | yield remove(dir);
|
137 | });
|
138 | }
|
139 | exports.cleanup = cleanup;
|
140 | function symlink(src, dst) {
|
141 | return fs.symlink(src, dst);
|
142 | }
|
143 | exports.symlink = symlink;
|
144 | function utimesSync(p, atime, mtime) {
|
145 | debug('utimeSync');
|
146 | return fs.utimesSync(p, atime, mtime);
|
147 | }
|
148 | exports.utimesSync = utimesSync;
|
149 | function touch(p) {
|
150 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
151 | debug('touch', p);
|
152 | try {
|
153 | return yield fs.utimes(p, new Date(), new Date());
|
154 | }
|
155 | catch (err) {
|
156 | return outputFile(p, '');
|
157 | }
|
158 | });
|
159 | }
|
160 | exports.touch = touch;
|
161 | function mkdirpSync(p) {
|
162 | debug('mkdirpSync', p);
|
163 | return fs.mkdirpSync(p);
|
164 | }
|
165 | exports.mkdirpSync = mkdirpSync;
|
166 | function emptyDir(p) {
|
167 | debug('emptyDir', p);
|
168 | return fs.emptyDir(p);
|
169 | }
|
170 | exports.emptyDir = emptyDir;
|