UNPKG

3.43 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.copyDir = copyDir;
7exports.copyFile = copyFile;
8exports.getFilesInDir = getFilesInDir;
9exports.isExcludedFile = isExcludedFile;
10exports.mkdirp = mkdirp;
11exports.recursiveRmdir = recursiveRmdir;
12exports.searchUpDirPath = searchUpDirPath;
13
14var _mkdirp = _interopRequireDefault(require("mkdirp"));
15
16var _node = require("./node.js");
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20const P = Promise;
21
22function copyDir(srcPath, destPath) {
23 return new Promise((res, rej) => {
24 _node.fs.copy(srcPath, destPath, err => {
25 if (err) {
26 rej(err);
27 } else {
28 res();
29 }
30 });
31 });
32}
33
34function copyFile(srcPath, destPath, preProcessor) {
35 return new Promise((res, rej) => {
36 if (preProcessor) {
37 const content = _node.fs.readFileSync(srcPath, 'utf-8');
38
39 _node.fs.writeFile(destPath, preProcessor(content), err => {
40 if (err) {
41 rej(err);
42 } else {
43 res();
44 }
45 });
46 } else {
47 _node.fs.copyFile(srcPath, destPath, err => {
48 if (err) {
49 rej(err);
50 } else {
51 res();
52 }
53 });
54 }
55 });
56}
57
58async function getFilesInDir(dirPath, recursive = false) {
59 let dirItems = await _node.fs.readdir(dirPath);
60 let dirItemStats = await P.all(dirItems.map(item => _node.fs.stat(_node.path.join(dirPath, item))));
61 const installedLibDefs = new Set();
62 await P.all(dirItems.map(async (itemName, idx) => {
63 const itemStat = dirItemStats[idx];
64
65 if (itemStat.isFile()) {
66 installedLibDefs.add(itemName);
67 } else if (recursive && itemStat.isDirectory()) {
68 const itemPath = _node.path.join(dirPath, itemName);
69
70 const subDirFiles = await getFilesInDir(itemPath, recursive);
71 subDirFiles.forEach(subItemName => installedLibDefs.add(_node.path.join(itemName, subItemName)));
72 }
73 }));
74 return installedLibDefs;
75}
76
77function mkdirp(path) {
78 return (0, _mkdirp.default)(path);
79}
80
81async function recursiveRmdir(dirPath) {
82 let dirItems = await _node.fs.readdir(dirPath);
83 let dirItemStats = await P.all(dirItems.map(item => _node.fs.stat(_node.path.join(dirPath, item))));
84 await P.all(dirItems.map(async (itemName, idx) => {
85 const itemStat = dirItemStats[idx];
86
87 const itemPath = _node.path.join(dirPath, itemName);
88
89 if (itemStat.isFile()) {
90 await _node.fs.unlink(itemPath);
91 } else {
92 await recursiveRmdir(itemPath);
93 await _node.fs.rmdir(itemPath).catch(err => {
94 if (err.code === 'ENOENT') {
95 // Ignore ENOENT error
96 // it's okay if the files are already removed
97 return;
98 }
99
100 throw err;
101 });
102 }
103 }));
104 return _node.fs.rmdir(dirPath);
105}
106
107async function searchUpDirPath(startDir, testFn) {
108 let currDir = startDir;
109 let lastDir = null;
110
111 while (currDir !== lastDir) {
112 if (await testFn(currDir)) {
113 return currDir;
114 }
115
116 lastDir = currDir;
117 currDir = _node.path.resolve(currDir, '..');
118 }
119
120 return null;
121}
122/**
123 * Returns whether a file matches one of the excluded file names
124 * which would be used by an external system and shouldn't be parsed
125 * by flow-typed cli.
126 */
127
128
129function isExcludedFile(item) {
130 // If a user opens definitions dir in finder it will create `.DS_Store`
131 return ['.DS_Store', 'CODEOWNERS'].includes(item);
132}
\No newline at end of file