UNPKG

6.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const crypto = require("crypto");
4const fs = require("fs-extra");
5const path = require("path");
6const util_1 = require("util");
7const log_1 = require("./log");
8const path_1 = require("./path");
9const deps = {
10 m: {},
11 get tmp() { return this.m.tmp = this.m.globby || require('tmp'); },
12 get globby() { return this.m.globby = this.m.globby || require('globby'); },
13};
14/**
15 * creates a directory if it does not exist
16 * this will automatically join the paths if you pass multiple strings with path.join()
17 */
18async function mkdirp(...filepaths) {
19 for (let f of filepaths.map(path_1.join)) {
20 log_1.log('mkdirp', f);
21 await fs.mkdirp(f);
22 }
23}
24exports.mkdirp = mkdirp;
25(function (mkdirp) {
26 function sync(...filepaths) {
27 for (let f of filepaths.map(path_1.join)) {
28 log_1.log('mkdirpSync', f);
29 fs.mkdirpSync(f);
30 }
31 }
32 mkdirp.sync = sync;
33})(mkdirp = exports.mkdirp || (exports.mkdirp = {}));
34/**
35 * glob matcher (find files)
36 */
37function globby(patterns, options = {}) {
38 log_1.log('globby', ...patterns);
39 return deps.globby(patterns, options);
40}
41exports.globby = globby;
42/**
43 * output string to file
44 * creates directory if not exists
45 */
46function write(filepaths, data, options = {}) {
47 const filepath = path_1.join(filepaths);
48 log_1.log('write', filepath);
49 return fs.outputFile(filepath, data, options);
50}
51exports.write = write;
52/**
53 * read file into string
54 */
55function read(filepaths, options = {}) {
56 const filepath = path_1.join(filepaths);
57 log_1.log('read', filepath);
58 return fs.readFile(filepath, Object.assign({ encoding: 'utf8' }, options));
59}
60exports.read = read;
61/**
62 * list files in directory
63 */
64async function ls(filepaths, options = {}) {
65 const filepath = path_1.join(filepaths);
66 // log('ls', filepath)
67 const files = await fs.readdir(filepath);
68 if (options.fullpath)
69 return files.map(f => path_1.join([filepath, f]));
70 else
71 return files;
72}
73exports.ls = ls;
74async function fileType(fp) {
75 try {
76 const stats = await fs.stat(path_1.join(fp));
77 if (stats.isSymbolicLink())
78 return 'symlink';
79 if (stats.isDirectory())
80 return 'directory';
81 if (stats.isFile())
82 return 'file';
83 }
84 catch (err) {
85 if (err.code === 'ENOENT')
86 return;
87 throw err;
88 }
89}
90exports.fileType = fileType;
91/**
92 * copy files with fs.copy
93 * can copy directories
94 */
95async function cp(source, destinationpaths, options = {}) {
96 source = path_1.join(source);
97 let dest = path_1.join(destinationpaths);
98 switch (await fileType(dest)) {
99 case 'directory':
100 dest = path.join(dest, path.basename(source));
101 break;
102 case 'file':
103 await rm(dest);
104 }
105 log_1.log('cp', source, dest);
106 return fs.copy(source, dest, options);
107}
108exports.cp = cp;
109/**
110 * rm -rf
111 */
112async function rm(...filesArray) {
113 for (let f of filesArray.map(path_1.join)) {
114 log_1.log('rm', f);
115 await fs.remove(f);
116 }
117}
118exports.rm = rm;
119async function rmIfEmpty(...filesArray) {
120 const rmdir = async (f) => {
121 let removedSomething = false;
122 const getFiles = async () => (await ls(f)).map(s => path_1.join([f, s]));
123 let files = await getFiles();
124 for (let subdir of files) {
125 if ((await fileType(subdir)) === 'directory') {
126 await rmdir(subdir);
127 removedSomething = true;
128 }
129 }
130 // check files again if we removed any
131 if (removedSomething)
132 files = await getFiles();
133 if (files.length === 0)
134 await rm(f);
135 };
136 for (let f of filesArray.map(path_1.join)) {
137 log_1.log('rmIfEmpty', f);
138 await rmdir(f);
139 }
140}
141exports.rmIfEmpty = rmIfEmpty;
142async function mv(source, dest) {
143 source = path_1.join(source);
144 dest = path_1.join(dest);
145 switch (await fileType(dest)) {
146 case 'directory':
147 dest = path.join(dest, path.basename(source));
148 break;
149 case 'file':
150 await rm(dest);
151 }
152 log_1.log('mv', source, dest);
153 return fs.move(source, dest);
154}
155exports.mv = mv;
156async function exists(filepath) {
157 filepath = path_1.join(filepath);
158 const exists = await fs.pathExists(filepath);
159 log_1.log('exists', filepath, exists);
160 return exists;
161}
162exports.exists = exists;
163(function (exists_1) {
164 function sync(filepath) {
165 filepath = path_1.join(filepath);
166 const exists = fs.pathExistsSync(filepath);
167 log_1.log('exists.sync', filepath, exists);
168 return exists;
169 }
170 exists_1.sync = sync;
171})(exists = exports.exists || (exports.exists = {}));
172function chmod(filepath, mode) {
173 filepath = path_1.join(filepath);
174 log_1.log('chmod', filepath, mode.toString(8));
175 return fs.chmod(filepath, mode);
176}
177exports.chmod = chmod;
178function ln(from, to) {
179 from = path_1.join(from);
180 to = path_1.join(to);
181 log_1.log('ln', from, to);
182 return fs.link(from, to);
183}
184exports.ln = ln;
185/**
186 * create a new temporary directory
187 * uses tmp
188 */
189async function tmpDir() {
190 const output = await util_1.promisify(deps.tmp.dir)();
191 log_1.log('tmpDir', output.name);
192 return output.name;
193}
194exports.tmpDir = tmpDir;
195async function emptyDir(filepath) {
196 filepath = path_1.join(filepath);
197 log_1.log('emptyDir', filepath);
198 await fs.mkdirp(path.dirname(filepath));
199 return fs.emptyDir(filepath);
200}
201exports.emptyDir = emptyDir;
202async function hash(algo, fp) {
203 const f = path_1.join(fp);
204 log_1.log('hash', algo, f);
205 return new Promise((resolve, reject) => {
206 const hash = crypto.createHash(algo);
207 const stream = fs.createReadStream(f);
208 stream.on('error', err => reject(err));
209 stream.on('data', chunk => hash.update(chunk));
210 stream.on('end', () => resolve(hash.digest('hex')));
211 });
212}
213exports.hash = hash;