git ssb

0+

Rômulo Alves / sync



Tree: ce27463164a9c7dab3164e59e793ace39b8ac23d

Files: ce27463164a9c7dab3164e59e793ace39b8ac23d / utils.js

1903 bytesRaw
1const { stat, access } = require('fs');
2const fs = require('fs-extra');
3const { join } = require('path');
4
5/**
6 * @function getStats
7 * @description Returns the directory/file stat
8 * @param {String} path Path to verify
9 * @return {Promies<Object>} Null if an error happens or the object of stat
10 */
11const getStats = path =>
12 new Promise(resolve => {
13 return stat(path, (err, stat) => {
14 if (err) {
15 // File does not exist
16 return resolve(null);
17 }
18
19 // Return stat
20 return resolve(stat);
21 });
22 });
23
24module.exports.getStats = getStats;
25
26/**
27 * @function exists
28 * @description Returns if a path exists
29 * @param {String} path Path to verify
30 * @return {Promise<Boolean>} Returns a boolean to indicate if path exists
31 */
32module.exports.exists = path =>
33 new Promise(resolve => {
34 return access(path, err => {
35 if (err) {
36 return resolve(false);
37 }
38
39 return resolve(true);
40 });
41 });
42
43/**
44 * @function delete
45 * @description Deletes the path
46 * @param {String} path Path to delete
47 * @return {Promise} Resolves after deleted
48 */
49module.exports.delete = path =>
50 new Promise(resolve => {
51 return fs.remove(path, () => resolve());
52 });
53
54/**
55 * @function readRecursive
56 * @description Reads recursivelly a path
57 * @param {String} source Path to copy
58 * @param {String} target Target path
59 * @return {Promise<Array<Array<String>>>} Resolves with the paths if was OK or rejects with the error object
60 */
61module.exports.readRecursive = (source, target) =>
62 new Promise((resolve, reject) => {
63
64 // Read things inside dir
65 return fs.readdir(source, (err, files) => {
66 if (err) {
67 return reject(err);
68 }
69
70 // Return an array with new source and target
71 const pathsParsed = files.map(f => [
72 join(source, f),
73 join(target, f)
74 ]);
75
76 return resolve(pathsParsed);
77 });
78 });

Built with git-ssb-web