@ark/fs 0.57.0-beta.1 → 0.57.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/fs.js +30 -22
- package/package.json +1 -1
package/out/fs.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { dirname, join, parse } from "node:path";
|
|
3
|
+
import { dirname, join, parse, resolve } from "node:path";
|
|
4
4
|
import * as process from "node:process";
|
|
5
5
|
import { URL, fileURLToPath } from "node:url";
|
|
6
6
|
import { caller } from "./caller.js";
|
|
@@ -26,22 +26,27 @@ export const writeJson = (path, data) => writeFileSync(path, `${JSON.stringify(d
|
|
|
26
26
|
export const rewriteJson = (path, transform) => writeJson(path, transform(readJson(path)));
|
|
27
27
|
export const rmRf = (target) => rmSync(target, { recursive: true, force: true });
|
|
28
28
|
export const cpR = (from, to) => cpSync(from, to, { recursive: true, force: true });
|
|
29
|
-
export const walkPaths = (dir, options = {}) =>
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
export const walkPaths = (dir, options = {}) => {
|
|
30
|
+
const walk = (currentDir, relPrefix) => readdirSync(currentDir).reduce((paths, item) => {
|
|
31
|
+
const path = join(currentDir, item);
|
|
32
|
+
const relPath = relPrefix ? join(relPrefix, item) : item;
|
|
33
|
+
const stat = lstatSync(path);
|
|
34
|
+
const isDir = stat.isDirectory();
|
|
35
|
+
const isFile = stat.isFile();
|
|
36
|
+
if (isDir && options.ignoreDirsMatching?.test(relPath))
|
|
37
|
+
return paths;
|
|
38
|
+
if (isFile && options.ignoreFilesMatching?.test(path))
|
|
39
|
+
return paths;
|
|
40
|
+
const excludeCurrent = (options.excludeDirs && isDir) ||
|
|
41
|
+
(options.excludeFiles && !isDir) ||
|
|
42
|
+
options.exclude?.(path) ||
|
|
43
|
+
(options.include && !options.include(path));
|
|
44
|
+
const nestedPaths = isDir ? walk(path, relPath) : [];
|
|
45
|
+
paths.push(...(excludeCurrent ? [] : [path]), ...nestedPaths);
|
|
34
46
|
return paths;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
(options.excludeFiles && !isDir) ||
|
|
39
|
-
options.exclude?.(path) ||
|
|
40
|
-
(options.include && !options.include(path));
|
|
41
|
-
const nestedPaths = isDir ? walkPaths(path, options) : [];
|
|
42
|
-
paths.push(...(excludeCurrent ? [] : [path]), ...nestedPaths);
|
|
43
|
-
return paths;
|
|
44
|
-
}, []);
|
|
47
|
+
}, []);
|
|
48
|
+
return walk(dir, "");
|
|
49
|
+
};
|
|
45
50
|
/** Fetch the file and directory paths from a path, uri, or `import.meta.url` */
|
|
46
51
|
export const filePath = (path) => {
|
|
47
52
|
let file;
|
|
@@ -66,8 +71,9 @@ export const fromHome = (...joinWith) => join(homedir(), ...joinWith);
|
|
|
66
71
|
export const fsRoot = parse(process.cwd()).root;
|
|
67
72
|
export const findPackageRoot = (fromDir) => {
|
|
68
73
|
const startDir = fromDir ?? dirOfCaller();
|
|
69
|
-
let dirToCheck = startDir;
|
|
70
|
-
|
|
74
|
+
let dirToCheck = resolve(startDir);
|
|
75
|
+
let previousDir;
|
|
76
|
+
while (dirToCheck !== previousDir) {
|
|
71
77
|
try {
|
|
72
78
|
const contents = readJson(join(dirToCheck, "package.json"));
|
|
73
79
|
/*
|
|
@@ -80,13 +86,14 @@ export const findPackageRoot = (fromDir) => {
|
|
|
80
86
|
catch {
|
|
81
87
|
// If the file doesn't exist, go up another level
|
|
82
88
|
}
|
|
83
|
-
|
|
89
|
+
previousDir = dirToCheck;
|
|
90
|
+
dirToCheck = dirname(dirToCheck);
|
|
84
91
|
}
|
|
85
92
|
return null;
|
|
86
93
|
};
|
|
87
94
|
export const assertPackageRoot = (fromDir) => {
|
|
88
95
|
const startDir = fromDir ?? dirOfCaller();
|
|
89
|
-
const result = findPackageRoot(
|
|
96
|
+
const result = findPackageRoot(startDir);
|
|
90
97
|
if (result)
|
|
91
98
|
return result;
|
|
92
99
|
throw new Error(`${startDir} is not part of a node package.`);
|
|
@@ -98,7 +105,8 @@ export const findPackageAncestors = (fromDir) => {
|
|
|
98
105
|
dir = findPackageRoot(dir);
|
|
99
106
|
if (dir) {
|
|
100
107
|
dirs.push(dir);
|
|
101
|
-
|
|
108
|
+
const parent = dirname(dir);
|
|
109
|
+
dir = parent === dir ? null : parent;
|
|
102
110
|
}
|
|
103
111
|
}
|
|
104
112
|
return dirs;
|
|
@@ -114,7 +122,7 @@ getShellOutput("git ls-files --exclude-standard --cached --others")
|
|
|
114
122
|
export const tsFileMatcher = /^.*\.(c|m)?tsx?$/;
|
|
115
123
|
const inFileFilter = {
|
|
116
124
|
include: path => tsFileMatcher.test(path),
|
|
117
|
-
ignoreDirsMatching: /node_modules|out|dist|docgen/
|
|
125
|
+
ignoreDirsMatching: /(?:^|[/\\])(?:node_modules|out|dist|docgen)(?:[/\\]|$)/
|
|
118
126
|
};
|
|
119
127
|
export const getSourceFilePaths = (dir = ".") => walkPaths(dir, inFileFilter);
|
|
120
128
|
export const getSourceFileEntries = (dir = ".") => getSourceFilePaths(dir).map(path => [path, readFile(path)]);
|