@jakubmazanec/fs-utils 0.1.0-11dd4a3
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/LICENSE.md +840 -0
- package/README.md +38 -0
- package/dist/createTempDirectory.d.ts +7 -0
- package/dist/createTempDirectory.js +13 -0
- package/dist/createTempDirectory.js.map +1 -0
- package/dist/ensureEmptyDirectory.d.ts +6 -0
- package/dist/ensureEmptyDirectory.js +14 -0
- package/dist/ensureEmptyDirectory.js.map +1 -0
- package/dist/findDirectories.d.ts +7 -0
- package/dist/findDirectories.js +13 -0
- package/dist/findDirectories.js.map +1 -0
- package/dist/findExistingDirectory.d.ts +10 -0
- package/dist/findExistingDirectory.js +26 -0
- package/dist/findExistingDirectory.js.map +1 -0
- package/dist/isDirectory.d.ts +7 -0
- package/dist/isDirectory.js +12 -0
- package/dist/isDirectory.js.map +1 -0
- package/dist/isEmptyDirectory.d.ts +7 -0
- package/dist/isEmptyDirectory.js +18 -0
- package/dist/isEmptyDirectory.js.map +1 -0
- package/dist/isRootPath.d.ts +7 -0
- package/dist/isRootPath.js +11 -0
- package/dist/isRootPath.js.map +1 -0
- package/dist/main.d.ts +8 -0
- package/dist/main.js +10 -0
- package/dist/main.js.map +1 -0
- package/dist/pathToPosixPath.d.ts +7 -0
- package/dist/pathToPosixPath.js +11 -0
- package/dist/pathToPosixPath.js.map +1 -0
- package/package.json +39 -0
- package/src/createTempDirectory.ts +13 -0
- package/src/ensureEmptyDirectory.ts +16 -0
- package/src/findDirectories.ts +13 -0
- package/src/findExistingDirectory.ts +31 -0
- package/src/isDirectory.ts +13 -0
- package/src/isEmptyDirectory.ts +20 -0
- package/src/isRootPath.ts +11 -0
- package/src/main.ts +8 -0
- package/src/pathToPosixPath.ts +11 -0
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# @jakubmazanec/fs-utils
|
|
4
|
+
|
|
5
|
+
<!-- description -->Collection of utilities for working with file system.<!-- description -->
|
|
6
|
+
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @jakubmazanec/fs-utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
⚠️ This is an [ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) package!
|
|
16
|
+
It cannot be required from a CommonJS module.
|
|
17
|
+
|
|
18
|
+
#### Prerequisites
|
|
19
|
+
|
|
20
|
+
- Node.js 16 or later
|
|
21
|
+
- TypeScript 5 or later
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
🚧 This section is under development. Please check back later for updates.
|
|
26
|
+
|
|
27
|
+
## Documentation
|
|
28
|
+
|
|
29
|
+
See [API reference](./docs) for auto-generated documentation.
|
|
30
|
+
|
|
31
|
+
## Contributing
|
|
32
|
+
|
|
33
|
+
If you want to contribute, see [CONTRIBUTING](./CONTRIBUTING.md) for details.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
This package is licensed under the GNU Lesser General Public License v3. See [LICENSE](./LICENSE.md)
|
|
38
|
+
for details.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a unique temporary directory in the operating system's default directory for temporary files. The name of the directory is random, but can be prefixed with a custom string.
|
|
3
|
+
*
|
|
4
|
+
* @param prefix The new directory name prefix.
|
|
5
|
+
* @returns The new directory name.
|
|
6
|
+
* */
|
|
7
|
+
export declare function createTempDirectory(prefix?: string): Promise<string>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a unique temporary directory in the operating system's default directory for temporary files. The name of the directory is random, but can be prefixed with a custom string.
|
|
6
|
+
*
|
|
7
|
+
* @param prefix The new directory name prefix.
|
|
8
|
+
* @returns The new directory name.
|
|
9
|
+
* */ export async function createTempDirectory(prefix) {
|
|
10
|
+
return fs.mkdtemp(path.join(os.tmpdir(), prefix ?? '/'));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=createTempDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/createTempDirectory.ts"],"sourcesContent":["import fs from 'fs-extra';\nimport os from 'node:os';\nimport path from 'node:path';\n\n/**\n * Creates a unique temporary directory in the operating system's default directory for temporary files. The name of the directory is random, but can be prefixed with a custom string.\n *\n * @param prefix The new directory name prefix.\n * @returns The new directory name.\n * */\nexport async function createTempDirectory(prefix?: string) {\n return fs.mkdtemp(path.join(os.tmpdir(), prefix ?? '/'));\n}\n"],"names":["fs","os","path","createTempDirectory","prefix","mkdtemp","join","tmpdir"],"mappings":"AAAA,OAAOA,QAAQ,WAAW;AAC1B,OAAOC,QAAQ,UAAU;AACzB,OAAOC,UAAU,YAAY;AAE7B;;;;;GAKG,GACH,OAAO,eAAeC,oBAAoBC,MAAe;IACvD,OAAOJ,GAAGK,QAAQH,KAAKI,KAAKL,GAAGM,UAAUH,UAAU;AACrD"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensures that a path is actually an empty directory. If the path doesn't exist, the directory is created; if the path exists but isn't an ampty directory, error is thrown.
|
|
3
|
+
*
|
|
4
|
+
* @param path The target path.
|
|
5
|
+
*/
|
|
6
|
+
export declare function ensureEmptyDirectory(targetPath: string): Promise<void>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import { isEmptyDirectory } from './isEmptyDirectory.js';
|
|
3
|
+
/**
|
|
4
|
+
* Ensures that a path is actually an empty directory. If the path doesn't exist, the directory is created; if the path exists but isn't an ampty directory, error is thrown.
|
|
5
|
+
*
|
|
6
|
+
* @param path The target path.
|
|
7
|
+
*/ export async function ensureEmptyDirectory(targetPath) {
|
|
8
|
+
await fs.ensureDir(targetPath);
|
|
9
|
+
if (!await isEmptyDirectory(targetPath)) {
|
|
10
|
+
throw new Error(`Directory "${targetPath}" must be empty`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=ensureEmptyDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ensureEmptyDirectory.ts"],"sourcesContent":["import fs from 'fs-extra';\n\nimport {isEmptyDirectory} from './isEmptyDirectory.js';\n\n/**\n * Ensures that a path is actually an empty directory. If the path doesn't exist, the directory is created; if the path exists but isn't an ampty directory, error is thrown.\n *\n * @param path The target path.\n */\nexport async function ensureEmptyDirectory(targetPath: string) {\n await fs.ensureDir(targetPath);\n\n if (!(await isEmptyDirectory(targetPath))) {\n throw new Error(`Directory \"${targetPath}\" must be empty`);\n }\n}\n"],"names":["fs","isEmptyDirectory","ensureEmptyDirectory","targetPath","ensureDir","Error"],"mappings":"AAAA,OAAOA,QAAQ,WAAW;AAE1B,SAAQC,gBAAgB,QAAO,wBAAwB;AAEvD;;;;CAIC,GACD,OAAO,eAAeC,qBAAqBC,UAAkB;IAC3D,MAAMH,GAAGI,UAAUD;IAEnB,IAAI,CAAE,MAAMF,iBAAiBE,aAAc;QACzC,MAAM,IAAIE,MAAM,CAAC,WAAW,EAAEF,WAAW,eAAe,CAAC;IAC3D;AACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
/**
|
|
3
|
+
* Finds all directories that are direct sub-directories of a path.
|
|
4
|
+
*
|
|
5
|
+
* @param searchPath Search path.
|
|
6
|
+
* @returns The list of directory names.
|
|
7
|
+
*/ export async function findDirectories(searchPath) {
|
|
8
|
+
return (await fs.readdir(searchPath, {
|
|
9
|
+
withFileTypes: true
|
|
10
|
+
})).filter((dirent)=>dirent.isDirectory()).map((dirent)=>dirent.name);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=findDirectories.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/findDirectories.ts"],"sourcesContent":["import fs from 'fs-extra';\n\n/**\n * Finds all directories that are direct sub-directories of a path.\n *\n * @param searchPath Search path.\n * @returns The list of directory names.\n */\nexport async function findDirectories(searchPath: string): Promise<string[]> {\n return (await fs.readdir(searchPath, {withFileTypes: true}))\n .filter((dirent) => dirent.isDirectory())\n .map((dirent) => dirent.name);\n}\n"],"names":["fs","findDirectories","searchPath","readdir","withFileTypes","filter","dirent","isDirectory","map","name"],"mappings":"AAAA,OAAOA,QAAQ,WAAW;AAE1B;;;;;CAKC,GACD,OAAO,eAAeC,gBAAgBC,UAAkB;IACtD,OAAO,AAAC,CAAA,MAAMF,GAAGG,QAAQD,YAAY;QAACE,eAAe;IAAI,EAAC,EACvDC,OAAO,CAACC,SAAWA,OAAOC,eAC1BC,IAAI,CAACF,SAAWA,OAAOG;AAC5B"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Travels up the file system tree from a starting path and returns path of the first existing directory.
|
|
3
|
+
*
|
|
4
|
+
* ```TypeScript
|
|
5
|
+
* let result = findExistingDirectory('/foo/bar/baz'); // if there is no `bar` directory, `result` is `'/foo'`
|
|
6
|
+
* ```
|
|
7
|
+
*
|
|
8
|
+
* @param searchPath Search path.
|
|
9
|
+
*/
|
|
10
|
+
export declare function findExistingDirectory(searchPath: string): Promise<string>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isDirectory } from './isDirectory.js';
|
|
4
|
+
/**
|
|
5
|
+
* Travels up the file system tree from a starting path and returns path of the first existing directory.
|
|
6
|
+
*
|
|
7
|
+
* ```TypeScript
|
|
8
|
+
* let result = findExistingDirectory('/foo/bar/baz'); // if there is no `bar` directory, `result` is `'/foo'`
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* @param searchPath Search path.
|
|
12
|
+
*/ export async function findExistingDirectory(searchPath) {
|
|
13
|
+
let result = searchPath;
|
|
14
|
+
let pathExists = await fs.pathExists(result);
|
|
15
|
+
while(!pathExists){
|
|
16
|
+
result = path.dirname(result);
|
|
17
|
+
pathExists = await fs.pathExists(result);
|
|
18
|
+
}
|
|
19
|
+
let isResultDirectory = await isDirectory(result);
|
|
20
|
+
if (!isResultDirectory) {
|
|
21
|
+
result = path.dirname(result);
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=findExistingDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/findExistingDirectory.ts"],"sourcesContent":["import fs from 'fs-extra';\nimport path from 'node:path';\n\nimport {isDirectory} from './isDirectory.js';\n\n/**\n * Travels up the file system tree from a starting path and returns path of the first existing directory.\n *\n * ```TypeScript\n * let result = findExistingDirectory('/foo/bar/baz'); // if there is no `bar` directory, `result` is `'/foo'`\n * ```\n *\n * @param searchPath Search path.\n */\nexport async function findExistingDirectory(searchPath: string) {\n let result = searchPath;\n let pathExists = await fs.pathExists(result);\n\n while (!pathExists) {\n result = path.dirname(result);\n pathExists = await fs.pathExists(result);\n }\n\n let isResultDirectory = await isDirectory(result);\n\n if (!isResultDirectory) {\n result = path.dirname(result);\n }\n\n return result;\n}\n"],"names":["fs","path","isDirectory","findExistingDirectory","searchPath","result","pathExists","dirname","isResultDirectory"],"mappings":"AAAA,OAAOA,QAAQ,WAAW;AAC1B,OAAOC,UAAU,YAAY;AAE7B,SAAQC,WAAW,QAAO,mBAAmB;AAE7C;;;;;;;;CAQC,GACD,OAAO,eAAeC,sBAAsBC,UAAkB;IAC5D,IAAIC,SAASD;IACb,IAAIE,aAAa,MAAMN,GAAGM,WAAWD;IAErC,MAAO,CAACC,WAAY;QAClBD,SAASJ,KAAKM,QAAQF;QACtBC,aAAa,MAAMN,GAAGM,WAAWD;IACnC;IAEA,IAAIG,oBAAoB,MAAMN,YAAYG;IAE1C,IAAI,CAACG,mBAAmB;QACtBH,SAASJ,KAAKM,QAAQF;IACxB;IAEA,OAAOA;AACT"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a path is a directory.
|
|
4
|
+
*
|
|
5
|
+
* @param path The path to check.
|
|
6
|
+
* @returns `true` if path is a directory, `false` otherwise.
|
|
7
|
+
*/ export async function isDirectory(path) {
|
|
8
|
+
let stat = await fs.lstat(path);
|
|
9
|
+
return stat.isDirectory();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=isDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/isDirectory.ts"],"sourcesContent":["import fs from 'fs-extra';\n\n/**\n * Checks if a path is a directory.\n *\n * @param path The path to check.\n * @returns `true` if path is a directory, `false` otherwise.\n */\nexport async function isDirectory(path: string): Promise<boolean> {\n let stat = await fs.lstat(path);\n\n return stat.isDirectory();\n}\n"],"names":["fs","isDirectory","path","stat","lstat"],"mappings":"AAAA,OAAOA,QAAQ,WAAW;AAE1B;;;;;CAKC,GACD,OAAO,eAAeC,YAAYC,IAAY;IAC5C,IAAIC,OAAO,MAAMH,GAAGI,MAAMF;IAE1B,OAAOC,KAAKF;AACd"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a path is an empty directory.
|
|
4
|
+
*
|
|
5
|
+
* @param path The path to check.
|
|
6
|
+
* @returns `true` if path is an empty directory, `false` otherwise.
|
|
7
|
+
*/ export async function isEmptyDirectory(path) {
|
|
8
|
+
try {
|
|
9
|
+
let directory = await fs.opendir(path);
|
|
10
|
+
let entry = await directory.read();
|
|
11
|
+
await directory.close();
|
|
12
|
+
return entry === null;
|
|
13
|
+
} catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=isEmptyDirectory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/isEmptyDirectory.ts"],"sourcesContent":["import fs from 'fs-extra';\n\n/**\n * Checks if a path is an empty directory.\n *\n * @param path The path to check.\n * @returns `true` if path is an empty directory, `false` otherwise.\n */\nexport async function isEmptyDirectory(path: string) {\n try {\n let directory = await fs.opendir(path);\n let entry = await directory.read();\n\n await directory.close();\n\n return entry === null;\n } catch {\n return false;\n }\n}\n"],"names":["fs","isEmptyDirectory","path","directory","opendir","entry","read","close"],"mappings":"AAAA,OAAOA,QAAQ,WAAW;AAE1B;;;;;CAKC,GACD,OAAO,eAAeC,iBAAiBC,IAAY;IACjD,IAAI;QACF,IAAIC,YAAY,MAAMH,GAAGI,QAAQF;QACjC,IAAIG,QAAQ,MAAMF,UAAUG;QAE5B,MAAMH,UAAUI;QAEhB,OAAOF,UAAU;IACnB,EAAE,OAAM;QACN,OAAO;IACT;AACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a path is a file system root path.
|
|
4
|
+
*
|
|
5
|
+
* @param path The path to check.
|
|
6
|
+
* @returns `true` if path is the root, `false` otherwise.
|
|
7
|
+
*/ export function isRootPath(value) {
|
|
8
|
+
return value.toLowerCase() === path.parse(value).root.toLowerCase();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=isRootPath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/isRootPath.ts"],"sourcesContent":["import path from 'node:path';\n\n/**\n * Checks if a path is a file system root path.\n *\n * @param path The path to check.\n * @returns `true` if path is the root, `false` otherwise.\n */\nexport function isRootPath(value: string) {\n return value.toLowerCase() === path.parse(value).root.toLowerCase();\n}\n"],"names":["path","isRootPath","value","toLowerCase","parse","root"],"mappings":"AAAA,OAAOA,UAAU,YAAY;AAE7B;;;;;CAKC,GACD,OAAO,SAASC,WAAWC,KAAa;IACtC,OAAOA,MAAMC,kBAAkBH,KAAKI,MAAMF,OAAOG,KAAKF;AACxD"}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './createTempDirectory.js';
|
|
2
|
+
export * from './ensureEmptyDirectory.js';
|
|
3
|
+
export * from './findDirectories.js';
|
|
4
|
+
export * from './findExistingDirectory.js';
|
|
5
|
+
export * from './isDirectory.js';
|
|
6
|
+
export * from './isEmptyDirectory.js';
|
|
7
|
+
export * from './isRootPath.js';
|
|
8
|
+
export * from './pathToPosixPath.js';
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './createTempDirectory.js';
|
|
2
|
+
export * from './ensureEmptyDirectory.js';
|
|
3
|
+
export * from './findDirectories.js';
|
|
4
|
+
export * from './findExistingDirectory.js';
|
|
5
|
+
export * from './isDirectory.js';
|
|
6
|
+
export * from './isEmptyDirectory.js';
|
|
7
|
+
export * from './isRootPath.js';
|
|
8
|
+
export * from './pathToPosixPath.js';
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './createTempDirectory.js';\nexport * from './ensureEmptyDirectory.js';\nexport * from './findDirectories.js';\nexport * from './findExistingDirectory.js';\nexport * from './isDirectory.js';\nexport * from './isEmptyDirectory.js';\nexport * from './isRootPath.js';\nexport * from './pathToPosixPath.js';\n"],"names":[],"mappings":"AAAA,cAAc,2BAA2B;AACzC,cAAc,4BAA4B;AAC1C,cAAc,uBAAuB;AACrC,cAAc,6BAA6B;AAC3C,cAAc,mBAAmB;AACjC,cAAc,wBAAwB;AACtC,cAAc,kBAAkB;AAChC,cAAc,uBAAuB"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
/**
|
|
3
|
+
* Converts path to POSIX path.
|
|
4
|
+
*
|
|
5
|
+
* @param value The path to convert.
|
|
6
|
+
* @returns The converted path.
|
|
7
|
+
*/ export function pathToPosixPath(value) {
|
|
8
|
+
return value.split(path.win32.sep).join(path.posix.sep);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=pathToPosixPath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/pathToPosixPath.ts"],"sourcesContent":["import path from 'node:path';\n\n/**\n * Converts path to POSIX path.\n *\n * @param value The path to convert.\n * @returns The converted path.\n */\nexport function pathToPosixPath(value: string): string {\n return value.split(path.win32.sep).join(path.posix.sep);\n}\n"],"names":["path","pathToPosixPath","value","split","win32","sep","join","posix"],"mappings":"AAAA,OAAOA,UAAU,YAAY;AAE7B;;;;;CAKC,GACD,OAAO,SAASC,gBAAgBC,KAAa;IAC3C,OAAOA,MAAMC,MAAMH,KAAKI,MAAMC,KAAKC,KAAKN,KAAKO,MAAMF;AACrD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jakubmazanec/fs-utils",
|
|
3
|
+
"version": "0.1.0-11dd4a3",
|
|
4
|
+
"description": "Collection of utilities for working with file system.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/jakubmazanec/js-tools",
|
|
8
|
+
"directory": "packages/fs-utils"
|
|
9
|
+
},
|
|
10
|
+
"license": "LGPL-3.0-only",
|
|
11
|
+
"author": "Jakub Mazanec <jakub@mazanec.dev>",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": "./dist/main.js",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "swc src -d dist && tsc",
|
|
21
|
+
"clean": "del-cli dist *.tsbuildinfo",
|
|
22
|
+
"document": "typedoc --options typedoc.config.cjs",
|
|
23
|
+
"format": "prettier . --write --ignore-unknown --ignore-path ../../.prettierignore",
|
|
24
|
+
"lint": "eslint .",
|
|
25
|
+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests",
|
|
26
|
+
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"fs-extra": "^10.0.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@jest/globals": "^29.5.0",
|
|
33
|
+
"@types/fs-extra": "^9.0.13"
|
|
34
|
+
},
|
|
35
|
+
"packageManager": "npm@8.19.4",
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=16.20.0"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a unique temporary directory in the operating system's default directory for temporary files. The name of the directory is random, but can be prefixed with a custom string.
|
|
7
|
+
*
|
|
8
|
+
* @param prefix The new directory name prefix.
|
|
9
|
+
* @returns The new directory name.
|
|
10
|
+
* */
|
|
11
|
+
export async function createTempDirectory(prefix?: string) {
|
|
12
|
+
return fs.mkdtemp(path.join(os.tmpdir(), prefix ?? '/'));
|
|
13
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
|
|
3
|
+
import {isEmptyDirectory} from './isEmptyDirectory.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Ensures that a path is actually an empty directory. If the path doesn't exist, the directory is created; if the path exists but isn't an ampty directory, error is thrown.
|
|
7
|
+
*
|
|
8
|
+
* @param path The target path.
|
|
9
|
+
*/
|
|
10
|
+
export async function ensureEmptyDirectory(targetPath: string) {
|
|
11
|
+
await fs.ensureDir(targetPath);
|
|
12
|
+
|
|
13
|
+
if (!(await isEmptyDirectory(targetPath))) {
|
|
14
|
+
throw new Error(`Directory "${targetPath}" must be empty`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Finds all directories that are direct sub-directories of a path.
|
|
5
|
+
*
|
|
6
|
+
* @param searchPath Search path.
|
|
7
|
+
* @returns The list of directory names.
|
|
8
|
+
*/
|
|
9
|
+
export async function findDirectories(searchPath: string): Promise<string[]> {
|
|
10
|
+
return (await fs.readdir(searchPath, {withFileTypes: true}))
|
|
11
|
+
.filter((dirent) => dirent.isDirectory())
|
|
12
|
+
.map((dirent) => dirent.name);
|
|
13
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import {isDirectory} from './isDirectory.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Travels up the file system tree from a starting path and returns path of the first existing directory.
|
|
8
|
+
*
|
|
9
|
+
* ```TypeScript
|
|
10
|
+
* let result = findExistingDirectory('/foo/bar/baz'); // if there is no `bar` directory, `result` is `'/foo'`
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @param searchPath Search path.
|
|
14
|
+
*/
|
|
15
|
+
export async function findExistingDirectory(searchPath: string) {
|
|
16
|
+
let result = searchPath;
|
|
17
|
+
let pathExists = await fs.pathExists(result);
|
|
18
|
+
|
|
19
|
+
while (!pathExists) {
|
|
20
|
+
result = path.dirname(result);
|
|
21
|
+
pathExists = await fs.pathExists(result);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let isResultDirectory = await isDirectory(result);
|
|
25
|
+
|
|
26
|
+
if (!isResultDirectory) {
|
|
27
|
+
result = path.dirname(result);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a path is a directory.
|
|
5
|
+
*
|
|
6
|
+
* @param path The path to check.
|
|
7
|
+
* @returns `true` if path is a directory, `false` otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export async function isDirectory(path: string): Promise<boolean> {
|
|
10
|
+
let stat = await fs.lstat(path);
|
|
11
|
+
|
|
12
|
+
return stat.isDirectory();
|
|
13
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a path is an empty directory.
|
|
5
|
+
*
|
|
6
|
+
* @param path The path to check.
|
|
7
|
+
* @returns `true` if path is an empty directory, `false` otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export async function isEmptyDirectory(path: string) {
|
|
10
|
+
try {
|
|
11
|
+
let directory = await fs.opendir(path);
|
|
12
|
+
let entry = await directory.read();
|
|
13
|
+
|
|
14
|
+
await directory.close();
|
|
15
|
+
|
|
16
|
+
return entry === null;
|
|
17
|
+
} catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a path is a file system root path.
|
|
5
|
+
*
|
|
6
|
+
* @param path The path to check.
|
|
7
|
+
* @returns `true` if path is the root, `false` otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export function isRootPath(value: string) {
|
|
10
|
+
return value.toLowerCase() === path.parse(value).root.toLowerCase();
|
|
11
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './createTempDirectory.js';
|
|
2
|
+
export * from './ensureEmptyDirectory.js';
|
|
3
|
+
export * from './findDirectories.js';
|
|
4
|
+
export * from './findExistingDirectory.js';
|
|
5
|
+
export * from './isDirectory.js';
|
|
6
|
+
export * from './isEmptyDirectory.js';
|
|
7
|
+
export * from './isRootPath.js';
|
|
8
|
+
export * from './pathToPosixPath.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts path to POSIX path.
|
|
5
|
+
*
|
|
6
|
+
* @param value The path to convert.
|
|
7
|
+
* @returns The converted path.
|
|
8
|
+
*/
|
|
9
|
+
export function pathToPosixPath(value: string): string {
|
|
10
|
+
return value.split(path.win32.sep).join(path.posix.sep);
|
|
11
|
+
}
|