@stryke/fs 0.33.32 → 0.33.33
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/CHANGELOG.md +6 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -1
- package/dist/buffer.cjs +60 -1
- package/dist/buffer.mjs +56 -1
- package/dist/buffer.mjs.map +1 -1
- package/dist/chmod-x.cjs +93 -1
- package/dist/chmod-x.d.cts.map +1 -1
- package/dist/chmod-x.mjs +87 -1
- package/dist/chmod-x.mjs.map +1 -1
- package/dist/command-exists.cjs +102 -1
- package/dist/command-exists.mjs +100 -1
- package/dist/command-exists.mjs.map +1 -1
- package/dist/compress.cjs +29 -1
- package/dist/compress.mjs +28 -1
- package/dist/compress.mjs.map +1 -1
- package/dist/constants.mjs +1 -1
- package/dist/convert/src/array-buffer-to-string.cjs +19 -1
- package/dist/convert/src/array-buffer-to-string.mjs +18 -1
- package/dist/convert/src/array-buffer-to-string.mjs.map +1 -1
- package/dist/convert/src/to-array.cjs +15 -1
- package/dist/convert/src/to-array.mjs +14 -1
- package/dist/convert/src/to-array.mjs.map +1 -1
- package/dist/convert/src/uint8-array-to-string.cjs +15 -1
- package/dist/convert/src/uint8-array-to-string.mjs +15 -1
- package/dist/convert/src/uint8-array-to-string.mjs.map +1 -1
- package/dist/copy-file.cjs +87 -1
- package/dist/copy-file.mjs +83 -1
- package/dist/copy-file.mjs.map +1 -1
- package/dist/exists.cjs +27 -1
- package/dist/exists.mjs +25 -1
- package/dist/exists.mjs.map +1 -1
- package/dist/get-parent-path.cjs +35 -1
- package/dist/get-parent-path.mjs +34 -1
- package/dist/get-parent-path.mjs.map +1 -1
- package/dist/get-workspace-root.cjs +158 -1
- package/dist/get-workspace-root.mjs +150 -1
- package/dist/get-workspace-root.mjs.map +1 -1
- package/dist/helpers/src/get-unique.cjs +14 -1
- package/dist/helpers/src/get-unique.mjs +13 -1
- package/dist/helpers/src/get-unique.mjs.map +1 -1
- package/dist/helpers.cjs +83 -1
- package/dist/helpers.mjs +77 -1
- package/dist/helpers.mjs.map +1 -1
- package/dist/index.cjs +122 -1
- package/dist/index.mjs +25 -1
- package/dist/install.cjs +35 -1
- package/dist/install.mjs +33 -1
- package/dist/install.mjs.map +1 -1
- package/dist/is-file.cjs +51 -1
- package/dist/is-file.mjs +47 -1
- package/dist/is-file.mjs.map +1 -1
- package/dist/json/src/storm-json.cjs +117 -1
- package/dist/json/src/storm-json.mjs +115 -1
- package/dist/json/src/storm-json.mjs.map +1 -1
- package/dist/json/src/utils/code-frames.cjs +77 -3
- package/dist/json/src/utils/code-frames.mjs +76 -3
- package/dist/json/src/utils/code-frames.mjs.map +1 -1
- package/dist/json/src/utils/parse-error.cjs +34 -1
- package/dist/json/src/utils/parse-error.mjs +33 -1
- package/dist/json/src/utils/parse-error.mjs.map +1 -1
- package/dist/json/src/utils/parse.cjs +45 -1
- package/dist/json/src/utils/parse.mjs +45 -1
- package/dist/json/src/utils/parse.mjs.map +1 -1
- package/dist/json/src/utils/stringify.cjs +67 -1
- package/dist/json/src/utils/stringify.mjs +67 -1
- package/dist/json/src/utils/stringify.mjs.map +1 -1
- package/dist/json/src/utils/strip-comments.cjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs +86 -4
- package/dist/json/src/utils/strip-comments.mjs.map +1 -1
- package/dist/json.cjs +74 -1
- package/dist/json.mjs +71 -1
- package/dist/json.mjs.map +1 -1
- package/dist/list-files.cjs +86 -1
- package/dist/list-files.mjs +79 -1
- package/dist/list-files.mjs.map +1 -1
- package/dist/package-fns.cjs +148 -1
- package/dist/package-fns.mjs +141 -1
- package/dist/package-fns.mjs.map +1 -1
- package/dist/path/src/append.cjs +57 -1
- package/dist/path/src/append.mjs +56 -1
- package/dist/path/src/append.mjs.map +1 -1
- package/dist/path/src/correct-path.cjs +138 -1
- package/dist/path/src/correct-path.mjs +134 -1
- package/dist/path/src/correct-path.mjs.map +1 -1
- package/dist/path/src/cwd.cjs +17 -1
- package/dist/path/src/cwd.mjs +16 -1
- package/dist/path/src/cwd.mjs.map +1 -1
- package/dist/path/src/file-path-fns.cjs +169 -1
- package/dist/path/src/file-path-fns.mjs +164 -1
- package/dist/path/src/file-path-fns.mjs.map +1 -1
- package/dist/path/src/is-parent-path.cjs +32 -1
- package/dist/path/src/is-parent-path.mjs +32 -1
- package/dist/path/src/is-parent-path.mjs.map +1 -1
- package/dist/path/src/is-root-dir.cjs +14 -1
- package/dist/path/src/is-root-dir.mjs +13 -1
- package/dist/path/src/is-root-dir.mjs.map +1 -1
- package/dist/path/src/is-type.cjs +68 -1
- package/dist/path/src/is-type.mjs +66 -1
- package/dist/path/src/is-type.mjs.map +1 -1
- package/dist/path/src/join-paths.cjs +106 -1
- package/dist/path/src/join-paths.mjs +106 -1
- package/dist/path/src/join-paths.mjs.map +1 -1
- package/dist/path/src/regex.cjs +18 -1
- package/dist/path/src/regex.mjs +11 -1
- package/dist/path/src/regex.mjs.map +1 -1
- package/dist/path/src/replace.cjs +24 -1
- package/dist/path/src/replace.mjs +24 -1
- package/dist/path/src/replace.mjs.map +1 -1
- package/dist/path/src/resolve-parent-path.cjs +18 -1
- package/dist/path/src/resolve-parent-path.mjs +18 -1
- package/dist/path/src/resolve-parent-path.mjs.map +1 -1
- package/dist/path/src/slash.cjs +15 -1
- package/dist/path/src/slash.mjs +14 -1
- package/dist/path/src/slash.mjs.map +1 -1
- package/dist/read-file.cjs +47 -1
- package/dist/read-file.mjs +43 -1
- package/dist/read-file.mjs.map +1 -1
- package/dist/registry.cjs +142 -1
- package/dist/registry.mjs +137 -1
- package/dist/registry.mjs.map +1 -1
- package/dist/remove-file.cjs +27 -1
- package/dist/remove-file.mjs +25 -1
- package/dist/remove-file.mjs.map +1 -1
- package/dist/resolve.cjs +228 -3
- package/dist/resolve.mjs +217 -3
- package/dist/resolve.mjs.map +1 -1
- package/dist/semver-fns.cjs +129 -1
- package/dist/semver-fns.mjs +119 -1
- package/dist/semver-fns.mjs.map +1 -1
- package/dist/string-format/src/package.cjs +92 -1
- package/dist/string-format/src/package.mjs +91 -1
- package/dist/string-format/src/package.mjs.map +1 -1
- package/dist/toml.cjs +66 -3
- package/dist/toml.mjs +61 -3
- package/dist/toml.mjs.map +1 -1
- package/dist/tsconfig.cjs +37 -1
- package/dist/tsconfig.mjs +35 -1
- package/dist/tsconfig.mjs.map +1 -1
- package/dist/type-checks/src/get-object-tag.cjs +15 -1
- package/dist/type-checks/src/get-object-tag.mjs +14 -1
- package/dist/type-checks/src/get-object-tag.mjs.map +1 -1
- package/dist/type-checks/src/index.cjs +14 -1
- package/dist/type-checks/src/index.mjs +16 -1
- package/dist/type-checks/src/is-buffer.cjs +12 -1
- package/dist/type-checks/src/is-buffer.mjs +11 -1
- package/dist/type-checks/src/is-buffer.mjs.map +1 -1
- package/dist/type-checks/src/is-collection.cjs +1 -1
- package/dist/type-checks/src/is-collection.mjs +3 -1
- package/dist/type-checks/src/is-empty.cjs +20 -1
- package/dist/type-checks/src/is-empty.mjs +20 -1
- package/dist/type-checks/src/is-empty.mjs.map +1 -1
- package/dist/type-checks/src/is-error.cjs +29 -1
- package/dist/type-checks/src/is-error.mjs +29 -1
- package/dist/type-checks/src/is-error.mjs.map +1 -1
- package/dist/type-checks/src/is-null.cjs +12 -1
- package/dist/type-checks/src/is-null.mjs +11 -1
- package/dist/type-checks/src/is-null.mjs.map +1 -1
- package/dist/type-checks/src/is-number.cjs +18 -1
- package/dist/type-checks/src/is-number.mjs +17 -1
- package/dist/type-checks/src/is-number.mjs.map +1 -1
- package/dist/type-checks/src/is-object.cjs +19 -1
- package/dist/type-checks/src/is-object.mjs +19 -1
- package/dist/type-checks/src/is-object.mjs.map +1 -1
- package/dist/type-checks/src/is-plain-object.cjs +64 -1
- package/dist/type-checks/src/is-plain-object.mjs +63 -1
- package/dist/type-checks/src/is-plain-object.mjs.map +1 -1
- package/dist/type-checks/src/is-set-string.cjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs +20 -1
- package/dist/type-checks/src/is-set-string.mjs.map +1 -1
- package/dist/type-checks/src/is-set.cjs +19 -1
- package/dist/type-checks/src/is-set.mjs +19 -1
- package/dist/type-checks/src/is-set.mjs.map +1 -1
- package/dist/type-checks/src/is-string.cjs +12 -1
- package/dist/type-checks/src/is-string.mjs +11 -1
- package/dist/type-checks/src/is-string.mjs.map +1 -1
- package/dist/type-checks/src/is-undefined.cjs +8 -1
- package/dist/type-checks/src/is-undefined.mjs +7 -1
- package/dist/type-checks/src/is-undefined.mjs.map +1 -1
- package/dist/type-checks/src/type-detect.cjs +15 -1
- package/dist/type-checks/src/type-detect.mjs +16 -1
- package/dist/type-checks/src/type-detect.mjs.map +1 -1
- package/dist/types/src/base.cjs +6 -1
- package/dist/types/src/base.mjs +5 -1
- package/dist/types/src/base.mjs.map +1 -1
- package/dist/write-file.cjs +40 -1
- package/dist/write-file.mjs +38 -1
- package/dist/write-file.mjs.map +1 -1
- package/dist/yaml.cjs +29 -1
- package/dist/yaml.mjs +27 -1
- package/dist/yaml.mjs.map +1 -1
- package/package.json +6 -6
package/dist/path/src/append.mjs
CHANGED
|
@@ -1,2 +1,57 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { cwd } from "./cwd.mjs";
|
|
2
|
+
import { slash } from "./slash.mjs";
|
|
3
|
+
import { isParentPath } from "./is-parent-path.mjs";
|
|
4
|
+
import { joinPaths } from "./join-paths.mjs";
|
|
5
|
+
|
|
6
|
+
//#region ../path/src/append.ts
|
|
7
|
+
/**
|
|
8
|
+
* If not already a parent path, append the base path from the beginning of the given child path.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* appendPath("src/index.ts", "/home/user/project");
|
|
13
|
+
* // returns "/home/user/project/src/index.ts"
|
|
14
|
+
*
|
|
15
|
+
* appendPath("/user/dev/app.ts", "/user/dev");
|
|
16
|
+
* // returns "/user/dev/app.ts"
|
|
17
|
+
*
|
|
18
|
+
* appendPath("docs/readme.md");
|
|
19
|
+
* // returns "<current_working_directory>/docs/readme.md"
|
|
20
|
+
*
|
|
21
|
+
* appendPath("src/index.ts", "/home/user/project", { skipIfAlreadyParent: false });
|
|
22
|
+
* // returns "/home/user/project/src/index.ts"
|
|
23
|
+
*
|
|
24
|
+
* appendPath("/home/user/project/src/index.ts", "/home/user/project", { skipIfAlreadyParent: false });
|
|
25
|
+
* // returns "/home/user/project/src/index.ts"
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param childPath - The child path to append to the {@link parentPath}
|
|
29
|
+
* @param parentPath - The parent path to add the {@link childPath} to
|
|
30
|
+
* @param options - Options for appending the path
|
|
31
|
+
* @returns The {@link parentPath} with the {@link childPath} appended
|
|
32
|
+
*/
|
|
33
|
+
function appendPath(childPath, parentPath = cwd(), options = {}) {
|
|
34
|
+
return slash(options.skipIfAlreadyParent !== false && isParentPath(childPath, parentPath) ? childPath : joinPaths(parentPath, childPath));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Append the extension to the given path.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* appendExtension("/home/user/project/src/index", ".ts");
|
|
42
|
+
* // returns "/home/user/project/src/index.ts"
|
|
43
|
+
* appendExtension("/home/user/project/src/index.ts", ".js");
|
|
44
|
+
* // returns "/home/user/project/src/index.ts.js"
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @param path - The path to append the extension to.
|
|
48
|
+
* @param extension - The extension to append.
|
|
49
|
+
* @returns The path with the appended extension.
|
|
50
|
+
*/
|
|
51
|
+
function appendExtension(path, extension) {
|
|
52
|
+
return `${path}.${extension.replace(/^\./, "")}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
export { appendExtension, appendPath };
|
|
2
57
|
//# sourceMappingURL=append.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"append.mjs","names":[],"sources":["../../../../path/src/append.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { cwd } from \"./cwd\";\nimport { isParentPath } from \"./is-parent-path\";\nimport { joinPaths } from \"./join-paths\";\nimport { slash } from \"./slash\";\n\nexport interface AppendPathOptions {\n /**\n * If `true`, the function will skip appending if the `childPath` is already a child of the `parentPath`.\n *\n * @defaultValue true\n */\n skipIfAlreadyParent?: boolean;\n}\n\n/**\n * If not already a parent path, append the base path from the beginning of the given child path.\n *\n * @example\n * ```ts\n * appendPath(\"src/index.ts\", \"/home/user/project\");\n * // returns \"/home/user/project/src/index.ts\"\n *\n * appendPath(\"/user/dev/app.ts\", \"/user/dev\");\n * // returns \"/user/dev/app.ts\"\n *\n * appendPath(\"docs/readme.md\");\n * // returns \"<current_working_directory>/docs/readme.md\"\n *\n * appendPath(\"src/index.ts\", \"/home/user/project\", { skipIfAlreadyParent: false });\n * // returns \"/home/user/project/src/index.ts\"\n *\n * appendPath(\"/home/user/project/src/index.ts\", \"/home/user/project\", { skipIfAlreadyParent: false });\n * // returns \"/home/user/project/src/index.ts\"\n * ```\n *\n * @param childPath - The child path to append to the {@link parentPath}\n * @param parentPath - The parent path to add the {@link childPath} to\n * @param options - Options for appending the path\n * @returns The {@link parentPath} with the {@link childPath} appended\n */\nexport function appendPath(\n childPath: string,\n parentPath: string = cwd(),\n options: AppendPathOptions = {}\n): string {\n return slash(\n options.skipIfAlreadyParent !== false && isParentPath(childPath, parentPath)\n ? childPath\n : joinPaths(parentPath, childPath)\n );\n}\n\nexport const append = appendPath;\n\n/**\n * Append the extension to the given path.\n *\n * @example\n * ```ts\n * appendExtension(\"/home/user/project/src/index\", \".ts\");\n * // returns \"/home/user/project/src/index.ts\"\n * appendExtension(\"/home/user/project/src/index.ts\", \".js\");\n * // returns \"/home/user/project/src/index.ts.js\"\n * ```\n *\n * @param path - The path to append the extension to.\n * @param extension - The extension to append.\n * @returns The path with the appended extension.\n */\nexport function appendExtension(path: string, extension: string): string {\n return `${path}.${extension.replace(/^\\./, \"\")}`;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"append.mjs","names":[],"sources":["../../../../path/src/append.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { cwd } from \"./cwd\";\nimport { isParentPath } from \"./is-parent-path\";\nimport { joinPaths } from \"./join-paths\";\nimport { slash } from \"./slash\";\n\nexport interface AppendPathOptions {\n /**\n * If `true`, the function will skip appending if the `childPath` is already a child of the `parentPath`.\n *\n * @defaultValue true\n */\n skipIfAlreadyParent?: boolean;\n}\n\n/**\n * If not already a parent path, append the base path from the beginning of the given child path.\n *\n * @example\n * ```ts\n * appendPath(\"src/index.ts\", \"/home/user/project\");\n * // returns \"/home/user/project/src/index.ts\"\n *\n * appendPath(\"/user/dev/app.ts\", \"/user/dev\");\n * // returns \"/user/dev/app.ts\"\n *\n * appendPath(\"docs/readme.md\");\n * // returns \"<current_working_directory>/docs/readme.md\"\n *\n * appendPath(\"src/index.ts\", \"/home/user/project\", { skipIfAlreadyParent: false });\n * // returns \"/home/user/project/src/index.ts\"\n *\n * appendPath(\"/home/user/project/src/index.ts\", \"/home/user/project\", { skipIfAlreadyParent: false });\n * // returns \"/home/user/project/src/index.ts\"\n * ```\n *\n * @param childPath - The child path to append to the {@link parentPath}\n * @param parentPath - The parent path to add the {@link childPath} to\n * @param options - Options for appending the path\n * @returns The {@link parentPath} with the {@link childPath} appended\n */\nexport function appendPath(\n childPath: string,\n parentPath: string = cwd(),\n options: AppendPathOptions = {}\n): string {\n return slash(\n options.skipIfAlreadyParent !== false && isParentPath(childPath, parentPath)\n ? childPath\n : joinPaths(parentPath, childPath)\n );\n}\n\nexport const append = appendPath;\n\n/**\n * Append the extension to the given path.\n *\n * @example\n * ```ts\n * appendExtension(\"/home/user/project/src/index\", \".ts\");\n * // returns \"/home/user/project/src/index.ts\"\n * appendExtension(\"/home/user/project/src/index.ts\", \".js\");\n * // returns \"/home/user/project/src/index.ts.js\"\n * ```\n *\n * @param path - The path to append the extension to.\n * @param extension - The extension to append.\n * @returns The path with the appended extension.\n */\nexport function appendExtension(path: string, extension: string): string {\n return `${path}.${extension.replace(/^\\./, \"\")}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,SAAgB,WACd,WACA,aAAqB,KAAK,EAC1B,UAA6B,EAAE,EACvB;AACR,QAAO,MACL,QAAQ,wBAAwB,SAAS,aAAa,WAAW,WAAW,GACxE,YACA,UAAU,YAAY,UAAU,CACrC;;;;;;;;;;;;;;;;;AAoBH,SAAgB,gBAAgB,MAAc,WAA2B;AACvE,QAAO,GAAG,KAAK,GAAG,UAAU,QAAQ,OAAO,GAAG"}
|
|
@@ -1 +1,138 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_cwd = require('./cwd.cjs');
|
|
2
|
+
const require_regex = require('./regex.cjs');
|
|
3
|
+
const require_is_type = require('./is-type.cjs');
|
|
4
|
+
const require_slash = require('./slash.cjs');
|
|
5
|
+
const require_append = require('./append.cjs');
|
|
6
|
+
|
|
7
|
+
//#region ../path/src/correct-path.ts
|
|
8
|
+
function normalizeWindowsPath(input = "") {
|
|
9
|
+
if (!input) return input;
|
|
10
|
+
return require_slash.slash(input).replace(require_regex.DRIVE_LETTER_START_REGEX, (r) => r.toUpperCase());
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Corrects/normalized a file path.
|
|
14
|
+
*
|
|
15
|
+
* @param path - The path to correct.
|
|
16
|
+
* @returns The corrected path.
|
|
17
|
+
*/
|
|
18
|
+
function correctPath(path) {
|
|
19
|
+
if (!path || path.length === 0) return ".";
|
|
20
|
+
path = normalizeWindowsPath(path);
|
|
21
|
+
const isUNCPath = path.match(require_regex.UNC_REGEX);
|
|
22
|
+
const isPathAbsolute = require_is_type.isAbsolutePath(path);
|
|
23
|
+
const trailingSeparator = path.endsWith("/");
|
|
24
|
+
path = normalizeString(path, !isPathAbsolute);
|
|
25
|
+
if (path.length === 0) {
|
|
26
|
+
if (isPathAbsolute) return "/";
|
|
27
|
+
return trailingSeparator ? "./" : ".";
|
|
28
|
+
}
|
|
29
|
+
if (trailingSeparator) path += "/";
|
|
30
|
+
if (require_regex.DRIVE_LETTER_REGEX.test(path)) path += "/";
|
|
31
|
+
if (isUNCPath) {
|
|
32
|
+
if (!isPathAbsolute) return `//./${path}`;
|
|
33
|
+
return `//${path}`;
|
|
34
|
+
}
|
|
35
|
+
return !path.startsWith("/") && isPathAbsolute && !require_regex.DRIVE_LETTER_REGEX.test(path) ? `/${path}` : path;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Remove any star tokens (*) from the end of the file path
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* stripStars("src/**") // returns "src"
|
|
42
|
+
* stripStars("src/*") // returns "src"
|
|
43
|
+
* stripStars("src/**\/*") // returns "src"
|
|
44
|
+
* stripStars("src/**\/*.txt") // returns "src"
|
|
45
|
+
* stripStars("src/**\/file.txt") // returns "src"
|
|
46
|
+
* stripStars("src/file.txt") // returns "src/file.txt"
|
|
47
|
+
* stripStars("") // returns "."
|
|
48
|
+
*
|
|
49
|
+
* @param path - The path to correct.
|
|
50
|
+
* @returns The corrected path.
|
|
51
|
+
*/
|
|
52
|
+
function stripStars(path) {
|
|
53
|
+
if (!path || path.length === 0) return ".";
|
|
54
|
+
path = correctPath(path);
|
|
55
|
+
let found = false;
|
|
56
|
+
return `${path.startsWith("/") ? "/" : ""}${path.split("/").reduce((ret, segment) => {
|
|
57
|
+
if (!segment?.trim()) return ret;
|
|
58
|
+
if (found || segment.includes("*")) {
|
|
59
|
+
found = true;
|
|
60
|
+
return ret;
|
|
61
|
+
}
|
|
62
|
+
return ret + (ret ? `/${segment}` : segment);
|
|
63
|
+
}, "")}`;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolves a string path, resolving '.' and '.' segments and allowing paths above the root.
|
|
67
|
+
*
|
|
68
|
+
* @param path - The path to normalize.
|
|
69
|
+
* @param allowAboveRoot - Whether to allow the resulting path to be above the root directory.
|
|
70
|
+
* @returns the normalize path string.
|
|
71
|
+
*/
|
|
72
|
+
function normalizeString(path, allowAboveRoot) {
|
|
73
|
+
let res = "";
|
|
74
|
+
let lastSegmentLength = 0;
|
|
75
|
+
let lastSlash = -1;
|
|
76
|
+
let dots = 0;
|
|
77
|
+
let char = null;
|
|
78
|
+
for (let index = 0; index <= path.length; ++index) {
|
|
79
|
+
if (index < path.length) char = path[index];
|
|
80
|
+
else if (char === "/") break;
|
|
81
|
+
else char = "/";
|
|
82
|
+
if (char === "/") {
|
|
83
|
+
if (lastSlash === index - 1 || dots === 1) {} else if (dots === 2) {
|
|
84
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
85
|
+
if (res.length > 2) {
|
|
86
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
87
|
+
if (lastSlashIndex === -1) {
|
|
88
|
+
res = "";
|
|
89
|
+
lastSegmentLength = 0;
|
|
90
|
+
} else {
|
|
91
|
+
res = res.slice(0, lastSlashIndex);
|
|
92
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
93
|
+
}
|
|
94
|
+
lastSlash = index;
|
|
95
|
+
dots = 0;
|
|
96
|
+
continue;
|
|
97
|
+
} else if (res.length > 0) {
|
|
98
|
+
res = "";
|
|
99
|
+
lastSegmentLength = 0;
|
|
100
|
+
lastSlash = index;
|
|
101
|
+
dots = 0;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (allowAboveRoot) {
|
|
106
|
+
res += res.length > 0 ? "/.." : "..";
|
|
107
|
+
lastSegmentLength = 2;
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
if (res.length > 0) res += `/${path.slice(lastSlash + 1, index)}`;
|
|
111
|
+
else res = path.slice(lastSlash + 1, index);
|
|
112
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
113
|
+
}
|
|
114
|
+
lastSlash = index;
|
|
115
|
+
dots = 0;
|
|
116
|
+
} else if (char === "." && dots !== -1) ++dots;
|
|
117
|
+
else dots = -1;
|
|
118
|
+
}
|
|
119
|
+
return res;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Converts a given path to an absolute path based on the current working directory.
|
|
123
|
+
*
|
|
124
|
+
* @param path - The path to convert to an absolute path.
|
|
125
|
+
* @param cwd - The current working directory to use as the base path if the path is not absolute.
|
|
126
|
+
* @returns The absolute path.
|
|
127
|
+
*/
|
|
128
|
+
function toAbsolutePath(path, cwd$1 = require_cwd.cwd()) {
|
|
129
|
+
if (require_is_type.isAbsolutePath(path)) return path;
|
|
130
|
+
return require_slash.slash(normalizeString(require_append.appendPath(path, cwd$1), true));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
//#endregion
|
|
134
|
+
exports.correctPath = correctPath;
|
|
135
|
+
exports.normalizeString = normalizeString;
|
|
136
|
+
exports.normalizeWindowsPath = normalizeWindowsPath;
|
|
137
|
+
exports.stripStars = stripStars;
|
|
138
|
+
exports.toAbsolutePath = toAbsolutePath;
|
|
@@ -1,2 +1,135 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { cwd } from "./cwd.mjs";
|
|
2
|
+
import { DRIVE_LETTER_REGEX, DRIVE_LETTER_START_REGEX, UNC_REGEX } from "./regex.mjs";
|
|
3
|
+
import { isAbsolutePath } from "./is-type.mjs";
|
|
4
|
+
import { slash } from "./slash.mjs";
|
|
5
|
+
import { appendPath } from "./append.mjs";
|
|
6
|
+
|
|
7
|
+
//#region ../path/src/correct-path.ts
|
|
8
|
+
function normalizeWindowsPath(input = "") {
|
|
9
|
+
if (!input) return input;
|
|
10
|
+
return slash(input).replace(DRIVE_LETTER_START_REGEX, (r) => r.toUpperCase());
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Corrects/normalized a file path.
|
|
14
|
+
*
|
|
15
|
+
* @param path - The path to correct.
|
|
16
|
+
* @returns The corrected path.
|
|
17
|
+
*/
|
|
18
|
+
function correctPath(path) {
|
|
19
|
+
if (!path || path.length === 0) return ".";
|
|
20
|
+
path = normalizeWindowsPath(path);
|
|
21
|
+
const isUNCPath = path.match(UNC_REGEX);
|
|
22
|
+
const isPathAbsolute = isAbsolutePath(path);
|
|
23
|
+
const trailingSeparator = path.endsWith("/");
|
|
24
|
+
path = normalizeString(path, !isPathAbsolute);
|
|
25
|
+
if (path.length === 0) {
|
|
26
|
+
if (isPathAbsolute) return "/";
|
|
27
|
+
return trailingSeparator ? "./" : ".";
|
|
28
|
+
}
|
|
29
|
+
if (trailingSeparator) path += "/";
|
|
30
|
+
if (DRIVE_LETTER_REGEX.test(path)) path += "/";
|
|
31
|
+
if (isUNCPath) {
|
|
32
|
+
if (!isPathAbsolute) return `//./${path}`;
|
|
33
|
+
return `//${path}`;
|
|
34
|
+
}
|
|
35
|
+
return !path.startsWith("/") && isPathAbsolute && !DRIVE_LETTER_REGEX.test(path) ? `/${path}` : path;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Remove any star tokens (*) from the end of the file path
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* stripStars("src/**") // returns "src"
|
|
42
|
+
* stripStars("src/*") // returns "src"
|
|
43
|
+
* stripStars("src/**\/*") // returns "src"
|
|
44
|
+
* stripStars("src/**\/*.txt") // returns "src"
|
|
45
|
+
* stripStars("src/**\/file.txt") // returns "src"
|
|
46
|
+
* stripStars("src/file.txt") // returns "src/file.txt"
|
|
47
|
+
* stripStars("") // returns "."
|
|
48
|
+
*
|
|
49
|
+
* @param path - The path to correct.
|
|
50
|
+
* @returns The corrected path.
|
|
51
|
+
*/
|
|
52
|
+
function stripStars(path) {
|
|
53
|
+
if (!path || path.length === 0) return ".";
|
|
54
|
+
path = correctPath(path);
|
|
55
|
+
let found = false;
|
|
56
|
+
return `${path.startsWith("/") ? "/" : ""}${path.split("/").reduce((ret, segment) => {
|
|
57
|
+
if (!segment?.trim()) return ret;
|
|
58
|
+
if (found || segment.includes("*")) {
|
|
59
|
+
found = true;
|
|
60
|
+
return ret;
|
|
61
|
+
}
|
|
62
|
+
return ret + (ret ? `/${segment}` : segment);
|
|
63
|
+
}, "")}`;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolves a string path, resolving '.' and '.' segments and allowing paths above the root.
|
|
67
|
+
*
|
|
68
|
+
* @param path - The path to normalize.
|
|
69
|
+
* @param allowAboveRoot - Whether to allow the resulting path to be above the root directory.
|
|
70
|
+
* @returns the normalize path string.
|
|
71
|
+
*/
|
|
72
|
+
function normalizeString(path, allowAboveRoot) {
|
|
73
|
+
let res = "";
|
|
74
|
+
let lastSegmentLength = 0;
|
|
75
|
+
let lastSlash = -1;
|
|
76
|
+
let dots = 0;
|
|
77
|
+
let char = null;
|
|
78
|
+
for (let index = 0; index <= path.length; ++index) {
|
|
79
|
+
if (index < path.length) char = path[index];
|
|
80
|
+
else if (char === "/") break;
|
|
81
|
+
else char = "/";
|
|
82
|
+
if (char === "/") {
|
|
83
|
+
if (lastSlash === index - 1 || dots === 1) {} else if (dots === 2) {
|
|
84
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
85
|
+
if (res.length > 2) {
|
|
86
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
87
|
+
if (lastSlashIndex === -1) {
|
|
88
|
+
res = "";
|
|
89
|
+
lastSegmentLength = 0;
|
|
90
|
+
} else {
|
|
91
|
+
res = res.slice(0, lastSlashIndex);
|
|
92
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
93
|
+
}
|
|
94
|
+
lastSlash = index;
|
|
95
|
+
dots = 0;
|
|
96
|
+
continue;
|
|
97
|
+
} else if (res.length > 0) {
|
|
98
|
+
res = "";
|
|
99
|
+
lastSegmentLength = 0;
|
|
100
|
+
lastSlash = index;
|
|
101
|
+
dots = 0;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (allowAboveRoot) {
|
|
106
|
+
res += res.length > 0 ? "/.." : "..";
|
|
107
|
+
lastSegmentLength = 2;
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
if (res.length > 0) res += `/${path.slice(lastSlash + 1, index)}`;
|
|
111
|
+
else res = path.slice(lastSlash + 1, index);
|
|
112
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
113
|
+
}
|
|
114
|
+
lastSlash = index;
|
|
115
|
+
dots = 0;
|
|
116
|
+
} else if (char === "." && dots !== -1) ++dots;
|
|
117
|
+
else dots = -1;
|
|
118
|
+
}
|
|
119
|
+
return res;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Converts a given path to an absolute path based on the current working directory.
|
|
123
|
+
*
|
|
124
|
+
* @param path - The path to convert to an absolute path.
|
|
125
|
+
* @param cwd - The current working directory to use as the base path if the path is not absolute.
|
|
126
|
+
* @returns The absolute path.
|
|
127
|
+
*/
|
|
128
|
+
function toAbsolutePath(path, cwd$1 = cwd()) {
|
|
129
|
+
if (isAbsolutePath(path)) return path;
|
|
130
|
+
return slash(normalizeString(appendPath(path, cwd$1), true));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
//#endregion
|
|
134
|
+
export { correctPath, normalizeString, normalizeWindowsPath, stripStars, toAbsolutePath };
|
|
2
135
|
//# sourceMappingURL=correct-path.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"correct-path.mjs","names":["char: string | null","currentDir","cwd"],"sources":["../../../../path/src/correct-path.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { appendPath } from \"./append\";\nimport { cwd as currentDir } from \"./cwd\";\nimport { isAbsolutePath } from \"./is-type\";\nimport { joinPaths } from \"./join-paths\";\nimport {\n DRIVE_LETTER_REGEX,\n DRIVE_LETTER_START_REGEX,\n UNC_REGEX\n} from \"./regex\";\nimport { slash } from \"./slash\";\n\n// Util to normalize windows paths to posix\nexport function normalizeWindowsPath(input = \"\") {\n if (!input) {\n return input;\n }\n\n return slash(input).replace(DRIVE_LETTER_START_REGEX, r => r.toUpperCase());\n}\n\n/**\n * Corrects/normalized a file path.\n *\n * @param path - The path to correct.\n * @returns The corrected path.\n */\nexport function correctPath(path?: string) {\n if (!path || path.length === 0) {\n return \".\";\n }\n\n // Normalize windows argument\n path = normalizeWindowsPath(path);\n\n const isUNCPath = path.match(UNC_REGEX);\n const isPathAbsolute = isAbsolutePath(path);\n const trailingSeparator = path.endsWith(\"/\");\n\n // Normalize the path\n path = normalizeString(path, !isPathAbsolute);\n\n if (path.length === 0) {\n if (isPathAbsolute) {\n return \"/\";\n }\n return trailingSeparator ? \"./\" : \".\";\n }\n\n if (trailingSeparator) {\n path += \"/\";\n }\n if (DRIVE_LETTER_REGEX.test(path)) {\n path += \"/\";\n }\n\n if (isUNCPath) {\n if (!isPathAbsolute) {\n return `//./${path}`;\n }\n return `//${path}`;\n }\n\n return !path.startsWith(\"/\") &&\n isPathAbsolute &&\n !DRIVE_LETTER_REGEX.test(path)\n ? `/${path}`\n : path;\n}\n\n/**\n * Remove any star tokens (*) from the end of the file path\n *\n * @example\n * stripStars(\"src/**\") // returns \"src\"\n * stripStars(\"src/*\") // returns \"src\"\n * stripStars(\"src/**\\/*\") // returns \"src\"\n * stripStars(\"src/**\\/*.txt\") // returns \"src\"\n * stripStars(\"src/**\\/file.txt\") // returns \"src\"\n * stripStars(\"src/file.txt\") // returns \"src/file.txt\"\n * stripStars(\"\") // returns \".\"\n *\n * @param path - The path to correct.\n * @returns The corrected path.\n */\nexport function stripStars(path?: string) {\n if (!path || path.length === 0) {\n return \".\";\n }\n\n path = correctPath(path);\n\n let found = false;\n\n return `${path.startsWith(\"/\") ? \"/\" : \"\"}${path\n .split(\"/\")\n .reduce((ret, segment) => {\n if (!segment?.trim()) {\n return ret;\n }\n\n if (found || segment.includes(\"*\")) {\n found = true;\n return ret;\n }\n\n return ret + (ret ? `/${segment}` : segment);\n }, \"\")}`;\n}\n\n/**\n * Resolves a string path, resolving '.' and '.' segments and allowing paths above the root.\n *\n * @param path - The path to normalize.\n * @param allowAboveRoot - Whether to allow the resulting path to be above the root directory.\n * @returns the normalize path string.\n */\nexport function normalizeString(path: string, allowAboveRoot: boolean) {\n let res = \"\";\n let lastSegmentLength = 0;\n let lastSlash = -1;\n let dots = 0;\n let char: string | null = null;\n for (let index = 0; index <= path.length; ++index) {\n if (index < path.length) {\n // casted because we know it exists thanks to the length check\n char = path[index] as string;\n } else if (char === \"/\") {\n break;\n } else {\n char = \"/\";\n }\n if (char === \"/\") {\n if (lastSlash === index - 1 || dots === 1) {\n // NOOP\n } else if (dots === 2) {\n if (\n res.length < 2 ||\n lastSegmentLength !== 2 ||\n res[res.length - 1] !== \".\" ||\n res[res.length - 2] !== \".\"\n ) {\n if (res.length > 2) {\n const lastSlashIndex = res.lastIndexOf(\"/\");\n if (lastSlashIndex === -1) {\n res = \"\";\n lastSegmentLength = 0;\n } else {\n res = res.slice(0, lastSlashIndex);\n lastSegmentLength = res.length - 1 - res.lastIndexOf(\"/\");\n }\n lastSlash = index;\n dots = 0;\n continue;\n } else if (res.length > 0) {\n res = \"\";\n lastSegmentLength = 0;\n lastSlash = index;\n dots = 0;\n continue;\n }\n }\n if (allowAboveRoot) {\n res += res.length > 0 ? \"/..\" : \"..\";\n lastSegmentLength = 2;\n }\n } else {\n if (res.length > 0) {\n res += `/${path.slice(lastSlash + 1, index)}`;\n } else {\n res = path.slice(lastSlash + 1, index);\n }\n lastSegmentLength = index - lastSlash - 1;\n }\n lastSlash = index;\n dots = 0;\n } else if (char === \".\" && dots !== -1) {\n ++dots;\n } else {\n dots = -1;\n }\n }\n return res;\n}\n\n/**\n * Converts a given path to an absolute path based on the current working directory.\n *\n * @param path - The path to convert to an absolute path.\n * @param cwd - The current working directory to use as the base path if the path is not absolute.\n * @returns The absolute path.\n */\nexport function toAbsolutePath(path: string, cwd = currentDir()): string {\n if (isAbsolutePath(path)) {\n return path;\n }\n\n return slash(normalizeString(appendPath(path, cwd), true));\n}\n\n/**\n * Converts a given path to a relative path based on the current working directory.\n *\n * @param path - The path to convert to a relative path.\n * @param cwd - The current working directory to use as the base path if the path is not absolute.\n * @returns The relative path.\n */\nexport function toRelativePath(path: string, cwd = currentDir()): string {\n if (!path || path.length === 0) {\n return \".\";\n }\n\n if (isAbsolutePath(path)) {\n path = slash(normalizeString(path, true));\n } else {\n path = slash(normalizeString(joinPaths(cwd, path), true));\n }\n\n if (path.startsWith(\"./\")) {\n return path.slice(2);\n }\n\n return path;\n}\n\n/**\n * Adds a trailing slash to a path if it doesn't already have one.\n *\n * @param path - The path to modify.\n * @returns The modified path with a trailing slash.\n */\nexport function withTrailingSlash(path: string): string {\n const result = correctPath(path);\n\n return result.endsWith(\"/\") ? result : `${result}/`;\n}\n\n/**\n * Removes a trailing slash from a path if it has one.\n *\n * @param path - The path to modify.\n * @returns The modified path without a trailing slash.\n */\nexport function withoutTrailingSlash(path: string): string {\n const result = correctPath(path);\n\n return result.endsWith(\"/\") ? result.slice(0, -1) : result;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"correct-path.mjs","names":["char: string | null","currentDir","cwd"],"sources":["../../../../path/src/correct-path.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { appendPath } from \"./append\";\nimport { cwd as currentDir } from \"./cwd\";\nimport { isAbsolutePath } from \"./is-type\";\nimport { joinPaths } from \"./join-paths\";\nimport {\n DRIVE_LETTER_REGEX,\n DRIVE_LETTER_START_REGEX,\n UNC_REGEX\n} from \"./regex\";\nimport { slash } from \"./slash\";\n\n// Util to normalize windows paths to posix\nexport function normalizeWindowsPath(input = \"\") {\n if (!input) {\n return input;\n }\n\n return slash(input).replace(DRIVE_LETTER_START_REGEX, r => r.toUpperCase());\n}\n\n/**\n * Corrects/normalized a file path.\n *\n * @param path - The path to correct.\n * @returns The corrected path.\n */\nexport function correctPath(path?: string) {\n if (!path || path.length === 0) {\n return \".\";\n }\n\n // Normalize windows argument\n path = normalizeWindowsPath(path);\n\n const isUNCPath = path.match(UNC_REGEX);\n const isPathAbsolute = isAbsolutePath(path);\n const trailingSeparator = path.endsWith(\"/\");\n\n // Normalize the path\n path = normalizeString(path, !isPathAbsolute);\n\n if (path.length === 0) {\n if (isPathAbsolute) {\n return \"/\";\n }\n return trailingSeparator ? \"./\" : \".\";\n }\n\n if (trailingSeparator) {\n path += \"/\";\n }\n if (DRIVE_LETTER_REGEX.test(path)) {\n path += \"/\";\n }\n\n if (isUNCPath) {\n if (!isPathAbsolute) {\n return `//./${path}`;\n }\n return `//${path}`;\n }\n\n return !path.startsWith(\"/\") &&\n isPathAbsolute &&\n !DRIVE_LETTER_REGEX.test(path)\n ? `/${path}`\n : path;\n}\n\n/**\n * Remove any star tokens (*) from the end of the file path\n *\n * @example\n * stripStars(\"src/**\") // returns \"src\"\n * stripStars(\"src/*\") // returns \"src\"\n * stripStars(\"src/**\\/*\") // returns \"src\"\n * stripStars(\"src/**\\/*.txt\") // returns \"src\"\n * stripStars(\"src/**\\/file.txt\") // returns \"src\"\n * stripStars(\"src/file.txt\") // returns \"src/file.txt\"\n * stripStars(\"\") // returns \".\"\n *\n * @param path - The path to correct.\n * @returns The corrected path.\n */\nexport function stripStars(path?: string) {\n if (!path || path.length === 0) {\n return \".\";\n }\n\n path = correctPath(path);\n\n let found = false;\n\n return `${path.startsWith(\"/\") ? \"/\" : \"\"}${path\n .split(\"/\")\n .reduce((ret, segment) => {\n if (!segment?.trim()) {\n return ret;\n }\n\n if (found || segment.includes(\"*\")) {\n found = true;\n return ret;\n }\n\n return ret + (ret ? `/${segment}` : segment);\n }, \"\")}`;\n}\n\n/**\n * Resolves a string path, resolving '.' and '.' segments and allowing paths above the root.\n *\n * @param path - The path to normalize.\n * @param allowAboveRoot - Whether to allow the resulting path to be above the root directory.\n * @returns the normalize path string.\n */\nexport function normalizeString(path: string, allowAboveRoot: boolean) {\n let res = \"\";\n let lastSegmentLength = 0;\n let lastSlash = -1;\n let dots = 0;\n let char: string | null = null;\n for (let index = 0; index <= path.length; ++index) {\n if (index < path.length) {\n // casted because we know it exists thanks to the length check\n char = path[index] as string;\n } else if (char === \"/\") {\n break;\n } else {\n char = \"/\";\n }\n if (char === \"/\") {\n if (lastSlash === index - 1 || dots === 1) {\n // NOOP\n } else if (dots === 2) {\n if (\n res.length < 2 ||\n lastSegmentLength !== 2 ||\n res[res.length - 1] !== \".\" ||\n res[res.length - 2] !== \".\"\n ) {\n if (res.length > 2) {\n const lastSlashIndex = res.lastIndexOf(\"/\");\n if (lastSlashIndex === -1) {\n res = \"\";\n lastSegmentLength = 0;\n } else {\n res = res.slice(0, lastSlashIndex);\n lastSegmentLength = res.length - 1 - res.lastIndexOf(\"/\");\n }\n lastSlash = index;\n dots = 0;\n continue;\n } else if (res.length > 0) {\n res = \"\";\n lastSegmentLength = 0;\n lastSlash = index;\n dots = 0;\n continue;\n }\n }\n if (allowAboveRoot) {\n res += res.length > 0 ? \"/..\" : \"..\";\n lastSegmentLength = 2;\n }\n } else {\n if (res.length > 0) {\n res += `/${path.slice(lastSlash + 1, index)}`;\n } else {\n res = path.slice(lastSlash + 1, index);\n }\n lastSegmentLength = index - lastSlash - 1;\n }\n lastSlash = index;\n dots = 0;\n } else if (char === \".\" && dots !== -1) {\n ++dots;\n } else {\n dots = -1;\n }\n }\n return res;\n}\n\n/**\n * Converts a given path to an absolute path based on the current working directory.\n *\n * @param path - The path to convert to an absolute path.\n * @param cwd - The current working directory to use as the base path if the path is not absolute.\n * @returns The absolute path.\n */\nexport function toAbsolutePath(path: string, cwd = currentDir()): string {\n if (isAbsolutePath(path)) {\n return path;\n }\n\n return slash(normalizeString(appendPath(path, cwd), true));\n}\n\n/**\n * Converts a given path to a relative path based on the current working directory.\n *\n * @param path - The path to convert to a relative path.\n * @param cwd - The current working directory to use as the base path if the path is not absolute.\n * @returns The relative path.\n */\nexport function toRelativePath(path: string, cwd = currentDir()): string {\n if (!path || path.length === 0) {\n return \".\";\n }\n\n if (isAbsolutePath(path)) {\n path = slash(normalizeString(path, true));\n } else {\n path = slash(normalizeString(joinPaths(cwd, path), true));\n }\n\n if (path.startsWith(\"./\")) {\n return path.slice(2);\n }\n\n return path;\n}\n\n/**\n * Adds a trailing slash to a path if it doesn't already have one.\n *\n * @param path - The path to modify.\n * @returns The modified path with a trailing slash.\n */\nexport function withTrailingSlash(path: string): string {\n const result = correctPath(path);\n\n return result.endsWith(\"/\") ? result : `${result}/`;\n}\n\n/**\n * Removes a trailing slash from a path if it has one.\n *\n * @param path - The path to modify.\n * @returns The modified path without a trailing slash.\n */\nexport function withoutTrailingSlash(path: string): string {\n const result = correctPath(path);\n\n return result.endsWith(\"/\") ? result.slice(0, -1) : result;\n}\n"],"mappings":";;;;;;;AA8BA,SAAgB,qBAAqB,QAAQ,IAAI;AAC/C,KAAI,CAAC,MACH,QAAO;AAGT,QAAO,MAAM,MAAM,CAAC,QAAQ,2BAA0B,MAAK,EAAE,aAAa,CAAC;;;;;;;;AAS7E,SAAgB,YAAY,MAAe;AACzC,KAAI,CAAC,QAAQ,KAAK,WAAW,EAC3B,QAAO;AAIT,QAAO,qBAAqB,KAAK;CAEjC,MAAM,YAAY,KAAK,MAAM,UAAU;CACvC,MAAM,iBAAiB,eAAe,KAAK;CAC3C,MAAM,oBAAoB,KAAK,SAAS,IAAI;AAG5C,QAAO,gBAAgB,MAAM,CAAC,eAAe;AAE7C,KAAI,KAAK,WAAW,GAAG;AACrB,MAAI,eACF,QAAO;AAET,SAAO,oBAAoB,OAAO;;AAGpC,KAAI,kBACF,SAAQ;AAEV,KAAI,mBAAmB,KAAK,KAAK,CAC/B,SAAQ;AAGV,KAAI,WAAW;AACb,MAAI,CAAC,eACH,QAAO,OAAO;AAEhB,SAAO,KAAK;;AAGd,QAAO,CAAC,KAAK,WAAW,IAAI,IAC1B,kBACA,CAAC,mBAAmB,KAAK,KAAK,GAC5B,IAAI,SACJ;;;;;;;;;;;;;;;;;AAkBN,SAAgB,WAAW,MAAe;AACxC,KAAI,CAAC,QAAQ,KAAK,WAAW,EAC3B,QAAO;AAGT,QAAO,YAAY,KAAK;CAExB,IAAI,QAAQ;AAEZ,QAAO,GAAG,KAAK,WAAW,IAAI,GAAG,MAAM,KAAK,KACzC,MAAM,IAAI,CACV,QAAQ,KAAK,YAAY;AACxB,MAAI,CAAC,SAAS,MAAM,CAClB,QAAO;AAGT,MAAI,SAAS,QAAQ,SAAS,IAAI,EAAE;AAClC,WAAQ;AACR,UAAO;;AAGT,SAAO,OAAO,MAAM,IAAI,YAAY;IACnC,GAAG;;;;;;;;;AAUV,SAAgB,gBAAgB,MAAc,gBAAyB;CACrE,IAAI,MAAM;CACV,IAAI,oBAAoB;CACxB,IAAI,YAAY;CAChB,IAAI,OAAO;CACX,IAAIA,OAAsB;AAC1B,MAAK,IAAI,QAAQ,GAAG,SAAS,KAAK,QAAQ,EAAE,OAAO;AACjD,MAAI,QAAQ,KAAK,OAEf,QAAO,KAAK;WACH,SAAS,IAClB;MAEA,QAAO;AAET,MAAI,SAAS,KAAK;AAChB,OAAI,cAAc,QAAQ,KAAK,SAAS,GAAG,YAEhC,SAAS,GAAG;AACrB,QACE,IAAI,SAAS,KACb,sBAAsB,KACtB,IAAI,IAAI,SAAS,OAAO,OACxB,IAAI,IAAI,SAAS,OAAO,KAExB;SAAI,IAAI,SAAS,GAAG;MAClB,MAAM,iBAAiB,IAAI,YAAY,IAAI;AAC3C,UAAI,mBAAmB,IAAI;AACzB,aAAM;AACN,2BAAoB;aACf;AACL,aAAM,IAAI,MAAM,GAAG,eAAe;AAClC,2BAAoB,IAAI,SAAS,IAAI,IAAI,YAAY,IAAI;;AAE3D,kBAAY;AACZ,aAAO;AACP;gBACS,IAAI,SAAS,GAAG;AACzB,YAAM;AACN,0BAAoB;AACpB,kBAAY;AACZ,aAAO;AACP;;;AAGJ,QAAI,gBAAgB;AAClB,YAAO,IAAI,SAAS,IAAI,QAAQ;AAChC,yBAAoB;;UAEjB;AACL,QAAI,IAAI,SAAS,EACf,QAAO,IAAI,KAAK,MAAM,YAAY,GAAG,MAAM;QAE3C,OAAM,KAAK,MAAM,YAAY,GAAG,MAAM;AAExC,wBAAoB,QAAQ,YAAY;;AAE1C,eAAY;AACZ,UAAO;aACE,SAAS,OAAO,SAAS,GAClC,GAAE;MAEF,QAAO;;AAGX,QAAO;;;;;;;;;AAUT,SAAgB,eAAe,MAAc,QAAMC,KAAY,EAAU;AACvE,KAAI,eAAe,KAAK,CACtB,QAAO;AAGT,QAAO,MAAM,gBAAgB,WAAW,MAAMC,MAAI,EAAE,KAAK,CAAC"}
|
package/dist/path/src/cwd.cjs
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
//#region ../path/src/cwd.ts
|
|
3
|
+
/**
|
|
4
|
+
* Get the current working directory.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* This function attempts to retrieve the current working directory using `process.cwd()`.
|
|
8
|
+
*
|
|
9
|
+
* @returns The current working directory or '/' if it cannot be determined
|
|
10
|
+
*/
|
|
11
|
+
function cwd() {
|
|
12
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") return process.cwd().replace(/\\/g, "/");
|
|
13
|
+
return "/";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.cwd = cwd;
|
package/dist/path/src/cwd.mjs
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
//#region ../path/src/cwd.ts
|
|
2
|
+
/**
|
|
3
|
+
* Get the current working directory.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* This function attempts to retrieve the current working directory using `process.cwd()`.
|
|
7
|
+
*
|
|
8
|
+
* @returns The current working directory or '/' if it cannot be determined
|
|
9
|
+
*/
|
|
10
|
+
function cwd() {
|
|
11
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") return process.cwd().replace(/\\/g, "/");
|
|
12
|
+
return "/";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { cwd };
|
|
2
17
|
//# sourceMappingURL=cwd.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cwd.mjs","names":[],"sources":["../../../../path/src/cwd.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/**\n * Get the current working directory.\n *\n * @remarks\n * This function attempts to retrieve the current working directory using `process.cwd()`.\n *\n * @returns The current working directory or '/' if it cannot be determined\n */\nexport function cwd() {\n if (typeof process !== \"undefined\" && typeof process.cwd === \"function\") {\n return process.cwd().replace(/\\\\/g, \"/\");\n }\n return \"/\";\n}\n"],"mappings":"AA0BA,SAAgB,
|
|
1
|
+
{"version":3,"file":"cwd.mjs","names":[],"sources":["../../../../path/src/cwd.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/**\n * Get the current working directory.\n *\n * @remarks\n * This function attempts to retrieve the current working directory using `process.cwd()`.\n *\n * @returns The current working directory or '/' if it cannot be determined\n */\nexport function cwd() {\n if (typeof process !== \"undefined\" && typeof process.cwd === \"function\") {\n return process.cwd().replace(/\\\\/g, \"/\");\n }\n return \"/\";\n}\n"],"mappings":";;;;;;;;;AA0BA,SAAgB,MAAM;AACpB,KAAI,OAAO,YAAY,eAAe,OAAO,QAAQ,QAAQ,WAC3D,QAAO,QAAQ,KAAK,CAAC,QAAQ,OAAO,IAAI;AAE1C,QAAO"}
|
|
@@ -1 +1,169 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_cwd = require('./cwd.cjs');
|
|
2
|
+
const require_regex = require('./regex.cjs');
|
|
3
|
+
const require_is_type = require('./is-type.cjs');
|
|
4
|
+
const require_correct_path = require('./correct-path.cjs');
|
|
5
|
+
const require_is_set_string = require('../../type-checks/src/is-set-string.cjs');
|
|
6
|
+
const require_base = require('../../types/src/base.cjs');
|
|
7
|
+
|
|
8
|
+
//#region ../path/src/file-path-fns.ts
|
|
9
|
+
/**
|
|
10
|
+
* Find the file name from a file path.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const fileName = findFileName("C:\\Users\\user\\Documents\\file.txt");
|
|
15
|
+
* // fileName = "file.txt"
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @param filePath - The file path to process
|
|
19
|
+
* @param options - Options to control the file name extraction
|
|
20
|
+
* @returns The file name
|
|
21
|
+
*/
|
|
22
|
+
function findFileName(filePath, options = {}) {
|
|
23
|
+
const { requireExtension = false, withExtension = true } = options;
|
|
24
|
+
const result = require_correct_path.normalizeWindowsPath(filePath)?.split(filePath?.includes("\\") ? "\\" : "/")?.pop() ?? "";
|
|
25
|
+
if (requireExtension === true && !result.includes(".")) return require_base.EMPTY_STRING;
|
|
26
|
+
if (withExtension === false && result.includes(".")) return result.replace(`.${findFileExtension(result) ?? ""}`, "") || require_base.EMPTY_STRING;
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Find the full file path's directories from a file path.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* The functionality of this method is similar to the {@link path.dirname} function in Node's path module.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const folderPath = findFilePath("C:\\Users\\user\\Documents\\file.txt");
|
|
38
|
+
* // folderPath = "C:\\Users\\user\\Documents"
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @param filePath - The file path to process
|
|
42
|
+
* @param options - Options to control the file name extraction
|
|
43
|
+
* @returns The full file path's directories
|
|
44
|
+
*/
|
|
45
|
+
function findFilePath(filePath, options = {}) {
|
|
46
|
+
const normalizedPath = require_correct_path.normalizeWindowsPath(filePath);
|
|
47
|
+
const result = normalizedPath.replace(findFileName(normalizedPath, {
|
|
48
|
+
requireExtension: false,
|
|
49
|
+
...options
|
|
50
|
+
}), "");
|
|
51
|
+
return result === "/" ? result : result.replace(/\/$/, "");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Find the top most folder containing the file from a file path.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* The functionality of this method is similar to the {@link path.basename} function in Node's path module.
|
|
58
|
+
* If you're looking for the full path of the folder (for example: `C:\\Users\\user\\Documents` instead of just `Documents`) containing the file, use {@link findFilePath} instead.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const folderPath = findFolderName("C:\\Users\\user\\Documents\\file.txt");
|
|
62
|
+
* // folderPath = "Documents"
|
|
63
|
+
*
|
|
64
|
+
* @param filePath - The file path to process
|
|
65
|
+
* @param options - Options to control the file name extraction
|
|
66
|
+
* @returns The folder containing the file
|
|
67
|
+
*/
|
|
68
|
+
function findFolderName(filePath, options) {
|
|
69
|
+
const segments = findFilePath(filePath, options).split("/");
|
|
70
|
+
let lastSegment = "";
|
|
71
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
72
|
+
const val = segments[i];
|
|
73
|
+
if (val) {
|
|
74
|
+
lastSegment = val;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return lastSegment ?? require_base.EMPTY_STRING;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Find the file extension from a file path.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* The functionality of this method is similar to the {@link path.extname} function in Node's path module.
|
|
85
|
+
* The file extension is the part of the file name that comes after the last dot (`.`) in the file name. If the file name does not contain a dot, or if it ends with a dot, this function will return `undefined`.
|
|
86
|
+
*
|
|
87
|
+
* The returned extension **will not** include the dot, for example `txt` or `js` instead of `.txt` or `.js`.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* findFileExtension("C:\\Users\\user\\Documents\\file.config.ts");
|
|
92
|
+
* // Returns "ts"
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @param filePath - The file path to process
|
|
96
|
+
* @returns The file extension or undefined if no extension is found
|
|
97
|
+
*/
|
|
98
|
+
function findFileExtension(filePath) {
|
|
99
|
+
if (filePath.endsWith(".") || filePath.endsWith("/")) return;
|
|
100
|
+
const match = require_regex.FILE_EXTENSION_REGEX.exec(require_correct_path.normalizeWindowsPath(filePath));
|
|
101
|
+
return match && match.length > 0 && require_is_set_string.isSetString(match[0]) ? match[0].replace(".", "") : void 0;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Check if a file path has a file extension.
|
|
105
|
+
*
|
|
106
|
+
* @param filePath - The file path to process
|
|
107
|
+
* @returns An indicator specifying if the file path has a file extension
|
|
108
|
+
*/
|
|
109
|
+
function hasFileExtension(filePath) {
|
|
110
|
+
return Boolean(findFileExtension(filePath));
|
|
111
|
+
}
|
|
112
|
+
function resolve(...paths) {
|
|
113
|
+
paths = paths.map((argument) => require_correct_path.normalizeWindowsPath(argument));
|
|
114
|
+
let resolvedPath = "";
|
|
115
|
+
let resolvedAbsolute = false;
|
|
116
|
+
for (let index = paths.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
117
|
+
const path = index >= 0 ? paths[index] : require_cwd.cwd();
|
|
118
|
+
if (!path || path.length === 0) continue;
|
|
119
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
120
|
+
resolvedAbsolute = require_is_type.isAbsolute(path);
|
|
121
|
+
}
|
|
122
|
+
resolvedPath = require_correct_path.normalizeString(resolvedPath, !resolvedAbsolute);
|
|
123
|
+
if (resolvedAbsolute && !require_is_type.isAbsolute(resolvedPath)) return `/${resolvedPath}`;
|
|
124
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get the relative path from one file to another.
|
|
128
|
+
*
|
|
129
|
+
* @remarks
|
|
130
|
+
* This function is similar to the `path.relative` function in Node's path module.
|
|
131
|
+
*
|
|
132
|
+
* @param from - The base path to start from
|
|
133
|
+
* @param to - The target path to resolve relative to the base path
|
|
134
|
+
* @returns The relative path from the base path to the target path
|
|
135
|
+
*/
|
|
136
|
+
function relative(from, to) {
|
|
137
|
+
const _from = resolve(from).replace(require_regex.ROOT_FOLDER_REGEX, "$1").split("/");
|
|
138
|
+
const _to = resolve(to).replace(require_regex.ROOT_FOLDER_REGEX, "$1").split("/");
|
|
139
|
+
if (_to[0][1] === ":" && _from[0][1] === ":" && _from[0] !== _to[0]) return _to.join("/");
|
|
140
|
+
const _fromCopy = [..._from];
|
|
141
|
+
for (const segment of _fromCopy) {
|
|
142
|
+
if (_to[0] !== segment) break;
|
|
143
|
+
_from.shift();
|
|
144
|
+
_to.shift();
|
|
145
|
+
}
|
|
146
|
+
return [..._from.map(() => ".."), ..._to].join("/");
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get the relative path from one file to another.
|
|
150
|
+
*
|
|
151
|
+
* @remarks
|
|
152
|
+
* This function wraps the `path.relative` function in Node's path module.
|
|
153
|
+
*
|
|
154
|
+
* @param from - The base path to start from
|
|
155
|
+
* @param to - The target path to resolve relative to the base path
|
|
156
|
+
* @param withEndSlash - Whether to include a trailing slash at the end of the path
|
|
157
|
+
* @returns The relative path from the base path to the target path
|
|
158
|
+
*/
|
|
159
|
+
function relativePath(from, to, withEndSlash = false) {
|
|
160
|
+
return relative(withEndSlash !== true ? from.replace(/\/$/, "") : from, withEndSlash !== true ? to.replace(/\/$/, "") : to);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
exports.findFileExtension = findFileExtension;
|
|
165
|
+
exports.findFileName = findFileName;
|
|
166
|
+
exports.findFilePath = findFilePath;
|
|
167
|
+
exports.findFolderName = findFolderName;
|
|
168
|
+
exports.hasFileExtension = hasFileExtension;
|
|
169
|
+
exports.relativePath = relativePath;
|