UNPKG

4.76 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const fs = require("fs-extra");
5const path = require("path");
6const deps_1 = require("./deps");
7const debug = require('debug')('cli:file');
8function exists(f) {
9 // debug('exists', f)
10 // @ts-ignore
11 return fs.exists(f);
12}
13exports.exists = exists;
14function existsSync(f) {
15 return fs.existsSync(f);
16}
17exports.existsSync = existsSync;
18const jsonFiles = {};
19function fetchJSONFile(f) {
20 if (!jsonFiles[f]) {
21 debug('fetchJSONFile', f);
22 jsonFiles[f] = fs.readJSON(f);
23 }
24 return jsonFiles[f];
25}
26exports.fetchJSONFile = fetchJSONFile;
27function 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}
33exports.rename = rename;
34function mkdirp(dir) {
35 return tslib_1.__awaiter(this, void 0, void 0, function* () {
36 debug('mkdirp', dir);
37 return fs.mkdirp(dir);
38 });
39}
40exports.mkdirp = mkdirp;
41function 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}
47exports.outputFile = outputFile;
48function outputFileSync(p, body) {
49 debug('outputFileSync');
50 return fs.outputFileSync(p, body);
51}
52exports.outputFileSync = outputFileSync;
53function 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}
59exports.outputJSON = outputJSON;
60function readJSON(file) {
61 return tslib_1.__awaiter(this, void 0, void 0, function* () {
62 debug('readJSON', file);
63 return fs.readJSON(file);
64 });
65}
66exports.readJSON = readJSON;
67function 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}
73exports.readFile = readFile;
74function 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}
82exports.remove = remove;
83function stat(file) {
84 return tslib_1.__awaiter(this, void 0, void 0, function* () {
85 return fs.stat(file);
86 });
87}
88exports.stat = stat;
89function open(path, flags, mode) {
90 debug('open', path, flags, mode);
91 return fs.open(path, flags, mode);
92}
93exports.open = open;
94function write(fd, data) {
95 debug('write', fd);
96 // @ts-ignore
97 return fs.write(fd, data);
98}
99exports.write = write;
100function 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}
111exports.walk = walk;
112function 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}
119exports.ls = ls;
120function 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}
139exports.cleanup = cleanup;
140function symlink(src, dst) {
141 return fs.symlink(src, dst);
142}
143exports.symlink = symlink;
144function utimesSync(p, atime, mtime) {
145 debug('utimeSync');
146 return fs.utimesSync(p, atime, mtime);
147}
148exports.utimesSync = utimesSync;
149function 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}
160exports.touch = touch;
161function mkdirpSync(p) {
162 debug('mkdirpSync', p);
163 return fs.mkdirpSync(p);
164}
165exports.mkdirpSync = mkdirpSync;
166function emptyDir(p) {
167 debug('emptyDir', p);
168 return fs.emptyDir(p);
169}
170exports.emptyDir = emptyDir;