UNPKG

3.46 kBJavaScriptView Raw
1import path from 'path';
2import fs from 'fs';
3import { promisify } from 'util';
4import rimraf from 'rimraf';
5import { isErrnoException } from '@stryker-mutator/util';
6export const fileUtils = {
7 deleteDir: promisify(rimraf),
8 async cleanFolder(folderName) {
9 try {
10 await fs.promises.lstat(folderName);
11 await this.deleteDir(folderName);
12 return fs.promises.mkdir(folderName, { recursive: true });
13 }
14 catch (e) {
15 return fs.promises.mkdir(folderName, { recursive: true });
16 }
17 },
18 async exists(fileName) {
19 try {
20 await fs.promises.stat(fileName);
21 return true;
22 }
23 catch (err) {
24 if (isErrnoException(err) && err.code === 'ENOENT') {
25 return false;
26 }
27 else {
28 // Oops, didn't mean to catch this one ⚾
29 throw err;
30 }
31 }
32 },
33 /**
34 * Wrapper around the 'import' expression (for testability)
35 */
36 importModule(moduleName) {
37 return import(moduleName);
38 },
39 /**
40 * Recursively walks the from directory and copy the content to the target directory synchronously
41 * @param from The source directory to move from
42 * @param to The target directory to move to
43 */
44 moveDirectoryRecursiveSync(from, to) {
45 if (!fs.existsSync(from)) {
46 return;
47 }
48 if (!fs.existsSync(to)) {
49 fs.mkdirSync(to);
50 }
51 const files = fs.readdirSync(from);
52 for (const file of files) {
53 const fromFileName = path.join(from, file);
54 const toFileName = path.join(to, file);
55 const stats = fs.lstatSync(fromFileName);
56 if (stats.isFile()) {
57 fs.renameSync(fromFileName, toFileName);
58 }
59 else {
60 this.moveDirectoryRecursiveSync(fromFileName, toFileName);
61 }
62 }
63 fs.rmdirSync(from);
64 },
65 /**
66 * Creates a symlink at `from` that points to `to`
67 * @param to The thing you want to point to
68 * @param from The thing you want to point from
69 */
70 async symlinkJunction(to, from) {
71 await fs.promises.mkdir(path.dirname(from), { recursive: true });
72 return fs.promises.symlink(to, from, 'junction');
73 },
74 /**
75 * Looks for the node_modules folder from basePath up to root.
76 * returns the first occurrence of the node_modules, or null of none could be found.
77 * @param basePath starting point
78 */
79 async findNodeModulesList(basePath, tempDirName) {
80 var _a;
81 const nodeModulesList = [];
82 const dirBfsQueue = (_a = ['.']) !== null && _a !== void 0 ? _a : [];
83 let dir;
84 while ((dir = dirBfsQueue.pop())) {
85 if (path.basename(dir) === tempDirName) {
86 continue;
87 }
88 if (path.basename(dir) === 'node_modules') {
89 nodeModulesList.push(dir);
90 continue;
91 }
92 const parentDir = dir;
93 const filesWithType = await fs.promises.readdir(path.join(basePath, dir), { withFileTypes: true });
94 const dirs = filesWithType.filter((file) => file.isDirectory()).map((childDir) => path.join(parentDir, childDir.name));
95 dirBfsQueue.push(...dirs);
96 }
97 return nodeModulesList;
98 },
99};
100//# sourceMappingURL=file-utils.js.map
\No newline at end of file