1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.copyDir = copyDir;
|
7 | exports.copyFile = copyFile;
|
8 | exports.getFilesInDir = getFilesInDir;
|
9 | exports.isExcludedFile = isExcludedFile;
|
10 | exports.mkdirp = mkdirp;
|
11 | exports.recursiveRmdir = recursiveRmdir;
|
12 | exports.searchUpDirPath = searchUpDirPath;
|
13 |
|
14 | var _mkdirp = _interopRequireDefault(require("mkdirp"));
|
15 |
|
16 | var _node = require("./node.js");
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | const P = Promise;
|
21 |
|
22 | function 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 |
|
34 | function 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 |
|
58 | async 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 |
|
77 | function mkdirp(path) {
|
78 | return (0, _mkdirp.default)(path);
|
79 | }
|
80 |
|
81 | async 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 |
|
96 |
|
97 | return;
|
98 | }
|
99 |
|
100 | throw err;
|
101 | });
|
102 | }
|
103 | }));
|
104 | return _node.fs.rmdir(dirPath);
|
105 | }
|
106 |
|
107 | async 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 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function isExcludedFile(item) {
|
130 |
|
131 | return ['.DS_Store', 'CODEOWNERS'].includes(item);
|
132 | } |
\ | No newline at end of file |