@bemoje/fs 0.0.11 → 0.0.12
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/package.json +3 -3
- package/src/index.ts +35 -0
- package/src/lib/AppData.ts +25 -0
- package/src/lib/JsonFile.ts +158 -0
- package/src/lib/LogFile.ts +117 -0
- package/src/lib/absoluteToRelativePath.test.ts +11 -0
- package/{dist/types/lib/absoluteToRelativePath.d.ts → src/lib/absoluteToRelativePath.ts} +20 -14
- package/src/lib/appendLineToFile.test.ts +49 -0
- package/src/lib/appendLineToFile.ts +21 -0
- package/src/lib/cleanDirectorySync.test.ts +57 -0
- package/{dist/types/lib/cleanDirectorySync.d.ts → src/lib/cleanDirectorySync.ts} +24 -17
- package/src/lib/createDirectory.test.ts +14 -0
- package/{dist/types/lib/createDirectory.d.ts → src/lib/createDirectory.ts} +5 -2
- package/src/lib/createDirectorySync.test.ts +14 -0
- package/{dist/types/lib/createDirectorySync.d.ts → src/lib/createDirectorySync.ts} +6 -2
- package/src/lib/createFileExtensionFilter.test.ts +17 -0
- package/{dist/types/lib/createFileExtensionFilter.d.ts → src/lib/createFileExtensionFilter.ts} +14 -2
- package/src/lib/cwdpath.test.ts +48 -0
- package/{dist/types/lib/cwdpath.d.ts → src/lib/cwdpath.ts} +16 -13
- package/src/lib/deleteDirectory.test.ts +24 -0
- package/{dist/types/lib/deleteDirectory.d.ts → src/lib/deleteDirectory.ts} +5 -2
- package/src/lib/deleteDirectorySafe.test.ts +40 -0
- package/{dist/types/lib/deleteDirectorySafe.d.ts → src/lib/deleteDirectorySafe.ts} +5 -2
- package/src/lib/deleteDirectorySafeSync.test.ts +30 -0
- package/{dist/types/lib/deleteDirectorySafeSync.d.ts → src/lib/deleteDirectorySafeSync.ts} +7 -2
- package/src/lib/deleteDirectorySync.test.ts +23 -0
- package/{dist/types/lib/deleteDirectorySync.d.ts → src/lib/deleteDirectorySync.ts} +6 -2
- package/src/lib/ensureValidWindowsPath.test.ts +80 -0
- package/{dist/types/lib/ensureValidWindowsPath.d.ts → src/lib/ensureValidWindowsPath.ts} +57 -23
- package/src/lib/getDiskDrivesWindows.ts +13 -0
- package/src/lib/getDownloadsDirectory.ts +6 -0
- package/src/lib/getHomeDirectory.ts +7 -0
- package/src/lib/getLastMofifiedFileIn.ts +15 -0
- package/src/lib/getRootDir.ts +5 -0
- package/src/lib/isWindows.ts +3 -0
- package/src/lib/normalizeFileExtension.test.ts +15 -0
- package/{dist/types/lib/normalizeFileExtension.d.ts → src/lib/normalizeFileExtension.ts} +29 -23
- package/{dist/types/lib/readFileLastLines.d.ts → src/lib/readFileLastLines.ts} +5 -3
- package/src/lib/readJsonFile.test.ts +34 -0
- package/{dist/types/lib/readJsonFile.d.ts → src/lib/readJsonFile.ts} +16 -13
- package/src/lib/readJsonFileSync.test.ts +36 -0
- package/{dist/types/lib/readJsonFileSync.d.ts → src/lib/readJsonFileSync.ts} +16 -13
- package/src/lib/readPackageJson.ts +9 -0
- package/src/lib/removeIllegalFilenameCharacters.ts +3 -0
- package/src/lib/tempFile.ts +20 -0
- package/src/lib/types/IEnsureValidWindowsPathOptions.ts +10 -0
- package/src/lib/types/ILogFileData.ts +5 -0
- package/src/lib/unzipFile.ts +5 -0
- package/src/lib/unzipMergeFiles.ts +20 -0
- package/src/lib/wipeDirectorySafe.ts +7 -0
- package/src/lib/writeJsonFile.test.ts +26 -0
- package/{dist/types/lib/writeJsonFile.d.ts → src/lib/writeJsonFile.ts} +5 -2
- package/src/lib/writeJsonFileSync.test.ts +28 -0
- package/{dist/types/lib/writeJsonFileSync.d.ts → src/lib/writeJsonFileSync.ts} +5 -2
- package/dist/index.cjs.js +0 -6
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -6
- package/dist/index.esm.js.map +0 -1
- package/dist/types/index.d.ts +0 -36
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/lib/AppData.d.ts +0 -11
- package/dist/types/lib/AppData.d.ts.map +0 -1
- package/dist/types/lib/JsonFile.d.ts +0 -79
- package/dist/types/lib/JsonFile.d.ts.map +0 -1
- package/dist/types/lib/LogFile.d.ts +0 -51
- package/dist/types/lib/LogFile.d.ts.map +0 -1
- package/dist/types/lib/absoluteToRelativePath.d.ts.map +0 -1
- package/dist/types/lib/appendLineToFile.d.ts +0 -12
- package/dist/types/lib/appendLineToFile.d.ts.map +0 -1
- package/dist/types/lib/cleanDirectorySync.d.ts.map +0 -1
- package/dist/types/lib/createDirectory.d.ts.map +0 -1
- package/dist/types/lib/createDirectorySync.d.ts.map +0 -1
- package/dist/types/lib/createFileExtensionFilter.d.ts.map +0 -1
- package/dist/types/lib/cwdpath.d.ts.map +0 -1
- package/dist/types/lib/deleteDirectory.d.ts.map +0 -1
- package/dist/types/lib/deleteDirectorySafe.d.ts.map +0 -1
- package/dist/types/lib/deleteDirectorySafeSync.d.ts.map +0 -1
- package/dist/types/lib/deleteDirectorySync.d.ts.map +0 -1
- package/dist/types/lib/ensureValidWindowsPath.d.ts.map +0 -1
- package/dist/types/lib/getDiskDrivesWindows.d.ts +0 -2
- package/dist/types/lib/getDiskDrivesWindows.d.ts.map +0 -1
- package/dist/types/lib/getDownloadsDirectory.d.ts +0 -2
- package/dist/types/lib/getDownloadsDirectory.d.ts.map +0 -1
- package/dist/types/lib/getHomeDirectory.d.ts +0 -2
- package/dist/types/lib/getHomeDirectory.d.ts.map +0 -1
- package/dist/types/lib/getLastMofifiedFileIn.d.ts +0 -2
- package/dist/types/lib/getLastMofifiedFileIn.d.ts.map +0 -1
- package/dist/types/lib/getRootDir.d.ts +0 -2
- package/dist/types/lib/getRootDir.d.ts.map +0 -1
- package/dist/types/lib/isWindows.d.ts +0 -2
- package/dist/types/lib/isWindows.d.ts.map +0 -1
- package/dist/types/lib/normalizeFileExtension.d.ts.map +0 -1
- package/dist/types/lib/readFileLastLines.d.ts.map +0 -1
- package/dist/types/lib/readJsonFile.d.ts.map +0 -1
- package/dist/types/lib/readJsonFileSync.d.ts.map +0 -1
- package/dist/types/lib/readPackageJson.d.ts +0 -2
- package/dist/types/lib/readPackageJson.d.ts.map +0 -1
- package/dist/types/lib/removeIllegalFilenameCharacters.d.ts +0 -2
- package/dist/types/lib/removeIllegalFilenameCharacters.d.ts.map +0 -1
- package/dist/types/lib/tempFile.d.ts +0 -7
- package/dist/types/lib/tempFile.d.ts.map +0 -1
- package/dist/types/lib/types/IEnsureValidWindowsPathOptions.d.ts +0 -11
- package/dist/types/lib/types/IEnsureValidWindowsPathOptions.d.ts.map +0 -1
- package/dist/types/lib/types/ILogFileData.d.ts +0 -6
- package/dist/types/lib/types/ILogFileData.d.ts.map +0 -1
- package/dist/types/lib/unzipFile.d.ts +0 -2
- package/dist/types/lib/unzipFile.d.ts.map +0 -1
- package/dist/types/lib/unzipMergeFiles.d.ts +0 -5
- package/dist/types/lib/unzipMergeFiles.d.ts.map +0 -1
- package/dist/types/lib/wipeDirectorySafe.d.ts +0 -2
- package/dist/types/lib/wipeDirectorySafe.d.ts.map +0 -1
- package/dist/types/lib/writeJsonFile.d.ts.map +0 -1
- package/dist/types/lib/writeJsonFileSync.d.ts.map +0 -1
package/{dist/types/lib/createFileExtensionFilter.d.ts → src/lib/createFileExtensionFilter.ts}
RENAMED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import { normalizeFileExtension } from './normalizeFileExtension'
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Takes a list of file extensions and returns a filter function that returns true if a filepath/filename passed to it contains one of the given file extensions.
|
|
3
6
|
* @remarks If no file extensions are provided, the filter will always return true.
|
|
@@ -10,5 +13,14 @@
|
|
|
10
13
|
* //=> false
|
|
11
14
|
* ```
|
|
12
15
|
*/
|
|
13
|
-
export
|
|
14
|
-
|
|
16
|
+
export function createFileExtensionFilter(...fileExtensions: string[]): (filepath: string) => boolean {
|
|
17
|
+
if (!fileExtensions.length) return () => true
|
|
18
|
+
return (filepath: string) => {
|
|
19
|
+
for (const ext of fileExtensions.map(normalizeFileExtension)) {
|
|
20
|
+
if (path.extname(filepath) === ext) {
|
|
21
|
+
return true
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import { cwdpath } from './cwdpath'
|
|
3
|
+
|
|
4
|
+
describe(cwdpath.name, () => {
|
|
5
|
+
const sep = path.sep
|
|
6
|
+
|
|
7
|
+
describe('when the current working directory is "/home/user"', () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
jest.spyOn(process, 'cwd').mockReturnValue('/home/user')
|
|
10
|
+
})
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
jest.restoreAllMocks()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('should join the paths correctly', () => {
|
|
16
|
+
const result = cwdpath('dir1', 'dir2', 'file.txt')
|
|
17
|
+
expect(result).toBe(`${sep}home${sep}user${sep}dir1${sep}dir2${sep}file.txt`)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('when the current working directory is "/home/user" and no paths are given', () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
jest.spyOn(process, 'cwd').mockReturnValue('/home/user')
|
|
24
|
+
})
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
jest.restoreAllMocks()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should return the current working directory', () => {
|
|
30
|
+
const result = cwdpath()
|
|
31
|
+
expect(result).toBe(`${sep}home${sep}user`)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('when the current working directory is "/" and the given paths are ["dir1", "dir2"]', () => {
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
jest.spyOn(process, 'cwd').mockReturnValue('/')
|
|
38
|
+
})
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
jest.restoreAllMocks()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should join the paths correctly', () => {
|
|
44
|
+
const result = cwdpath('dir1', 'dir2')
|
|
45
|
+
expect(result).toBe(`${sep}dir1${sep}dir2`)
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
* @
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import path from 'path'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Joins the given path segments to the current working directory path, and normalizes the resulting path.
|
|
5
|
+
* @remarks
|
|
6
|
+
* The function uses the `path.join` method from the Node.js `path` module, and the `process.cwd` method from the Node.js `process` module.
|
|
7
|
+
* @param paths - The path segments to join to the current working directory path.
|
|
8
|
+
* @returns The resulting path.
|
|
9
|
+
* @example ```ts
|
|
10
|
+
* console.log(cwdpath('src', 'index.ts'));
|
|
11
|
+
* //=> '{working_directory}/src/index.ts'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function cwdpath(...paths: string[]): string {
|
|
15
|
+
return path.join(process.cwd(), ...paths)
|
|
16
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { deleteDirectory } from './deleteDirectory'
|
|
3
|
+
|
|
4
|
+
describe(deleteDirectory.name, () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
jest.restoreAllMocks()
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('should delete directory successfully', async () => {
|
|
10
|
+
const dirpath = 'test'
|
|
11
|
+
const rmMock = jest.spyOn(fs.promises, 'rm')
|
|
12
|
+
await deleteDirectory(dirpath)
|
|
13
|
+
expect(rmMock).toHaveBeenCalledWith(dirpath, { recursive: true, force: true })
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('should throw an error if the directory cannot be deleted', async () => {
|
|
17
|
+
const dirpath = 'test'
|
|
18
|
+
const rmMock = jest.spyOn(fs.promises, 'rm')
|
|
19
|
+
const error = new Error('Failed to delete directory')
|
|
20
|
+
rmMock.mockRejectedValue(error)
|
|
21
|
+
await expect(deleteDirectory(dirpath)).rejects.toThrow(error)
|
|
22
|
+
expect(rmMock).toHaveBeenCalledWith(dirpath, { recursive: true, force: true })
|
|
23
|
+
})
|
|
24
|
+
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Deletes a directory and all of its contents.
|
|
3
5
|
* @remarks This function uses Node.js's `fs.promises.rm` method with the `recursive` and `force` options set to `true`.
|
|
@@ -9,5 +11,6 @@
|
|
|
9
11
|
* deleteDirectory('/path/to/directory');
|
|
10
12
|
* ```
|
|
11
13
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
+
export async function deleteDirectory(dirpath: string): Promise<void> {
|
|
15
|
+
await fs.promises.rm(dirpath, { recursive: true, force: true })
|
|
16
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { deleteDirectorySafe } from './deleteDirectorySafe'
|
|
3
|
+
|
|
4
|
+
describe(deleteDirectorySafe.name, () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
jest.restoreAllMocks()
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('should delete directory successfully if it exists', async () => {
|
|
10
|
+
const dirpath = 'test'
|
|
11
|
+
const existsSyncMock = jest.spyOn(fs, 'existsSync')
|
|
12
|
+
existsSyncMock.mockReturnValue(true)
|
|
13
|
+
const rmMock = jest.spyOn(fs.promises, 'rm')
|
|
14
|
+
await deleteDirectorySafe(dirpath)
|
|
15
|
+
expect(existsSyncMock).toHaveBeenCalledWith(dirpath)
|
|
16
|
+
expect(rmMock).toHaveBeenCalledWith(dirpath, { recursive: true, force: true })
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should not delete directory if it does not exist', async () => {
|
|
20
|
+
const dirpath = 'test'
|
|
21
|
+
const existsSyncMock = jest.spyOn(fs, 'existsSync')
|
|
22
|
+
existsSyncMock.mockReturnValue(false)
|
|
23
|
+
const rmMock = jest.spyOn(fs.promises, 'rm')
|
|
24
|
+
await deleteDirectorySafe(dirpath)
|
|
25
|
+
expect(existsSyncMock).toHaveBeenCalledWith(dirpath)
|
|
26
|
+
expect(rmMock).not.toHaveBeenCalled()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should throw an error if the directory cannot be deleted', async () => {
|
|
30
|
+
const dirpath = 'test'
|
|
31
|
+
const existsSyncMock = jest.spyOn(fs, 'existsSync')
|
|
32
|
+
existsSyncMock.mockReturnValue(true)
|
|
33
|
+
const rmMock = jest.spyOn(fs.promises, 'rm')
|
|
34
|
+
const error = new Error('Failed to delete directory')
|
|
35
|
+
rmMock.mockRejectedValue(error)
|
|
36
|
+
await expect(deleteDirectorySafe(dirpath)).rejects.toThrow(error)
|
|
37
|
+
expect(existsSyncMock).toHaveBeenCalledWith(dirpath)
|
|
38
|
+
expect(rmMock).toHaveBeenCalledWith(dirpath, { recursive: true, force: true })
|
|
39
|
+
})
|
|
40
|
+
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Deletes a directory safely if it exists.
|
|
3
5
|
* @remarks This function uses Node.js' `fs.promises.rm` method with the `recursive` and `force` options set to `true`.
|
|
@@ -10,5 +12,6 @@
|
|
|
10
12
|
* //=> undefined
|
|
11
13
|
* ```
|
|
12
14
|
*/
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
+
export async function deleteDirectorySafe(dirpath: string): Promise<void> {
|
|
16
|
+
if (fs.existsSync(dirpath)) await fs.promises.rm(dirpath, { recursive: true, force: true })
|
|
17
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { deleteDirectorySafeSync } from './deleteDirectorySafeSync'
|
|
3
|
+
|
|
4
|
+
describe(deleteDirectorySafeSync.name, () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
jest.restoreAllMocks()
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('should delete directory successfully if it exists', () => {
|
|
10
|
+
const dirpath = 'test'
|
|
11
|
+
const existsSyncMock = jest.spyOn(fs, 'existsSync')
|
|
12
|
+
existsSyncMock.mockReturnValue(true)
|
|
13
|
+
const rmSyncMock = jest.spyOn(fs, 'rmSync')
|
|
14
|
+
const result = deleteDirectorySafeSync(dirpath)
|
|
15
|
+
expect(existsSyncMock).toHaveBeenCalledWith(dirpath)
|
|
16
|
+
expect(rmSyncMock).toHaveBeenCalledWith(dirpath, { recursive: true, force: true })
|
|
17
|
+
expect(result).toEqual(dirpath)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should return the directory path if it does not exist', () => {
|
|
21
|
+
const dirpath = 'test'
|
|
22
|
+
const existsSyncMock = jest.spyOn(fs, 'existsSync')
|
|
23
|
+
existsSyncMock.mockReturnValue(false)
|
|
24
|
+
const rmSyncMock = jest.spyOn(fs, 'rmSync')
|
|
25
|
+
const result = deleteDirectorySafeSync(dirpath)
|
|
26
|
+
expect(existsSyncMock).toHaveBeenCalledWith(dirpath)
|
|
27
|
+
expect(rmSyncMock).not.toHaveBeenCalled()
|
|
28
|
+
expect(result).toEqual(dirpath)
|
|
29
|
+
})
|
|
30
|
+
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Deletes a directory synchronously in a safe manner. If the directory does not exist, it will simply return the directory path without throwing an error.
|
|
3
5
|
* @param dirpath The path of the directory to be deleted.
|
|
@@ -8,5 +10,8 @@
|
|
|
8
10
|
* //=> undefined
|
|
9
11
|
* ```
|
|
10
12
|
*/
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
+
export function deleteDirectorySafeSync(dirpath: string): string {
|
|
14
|
+
if (!fs.existsSync(dirpath)) return dirpath
|
|
15
|
+
fs.rmSync(dirpath, { recursive: true, force: true })
|
|
16
|
+
return dirpath
|
|
17
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { deleteDirectorySync } from './deleteDirectorySync'
|
|
3
|
+
|
|
4
|
+
describe(deleteDirectorySync.name, () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
jest.restoreAllMocks()
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('should delete directory successfully', () => {
|
|
10
|
+
const dirpath = 'test'
|
|
11
|
+
const rmSyncMock = jest.spyOn(fs, 'rmSync')
|
|
12
|
+
deleteDirectorySync(dirpath)
|
|
13
|
+
expect(rmSyncMock).toHaveBeenCalledWith(dirpath, { recursive: true, force: true })
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('should return the directory path', () => {
|
|
17
|
+
const dirpath = 'test'
|
|
18
|
+
const rmSyncMock = jest.spyOn(fs, 'rmSync')
|
|
19
|
+
const result = deleteDirectorySync(dirpath)
|
|
20
|
+
expect(result).toEqual(dirpath)
|
|
21
|
+
expect(rmSyncMock).toHaveBeenCalledWith(dirpath, { recursive: true, force: true })
|
|
22
|
+
})
|
|
23
|
+
})
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Deletes a directory synchronously.
|
|
3
5
|
* @remarks This function uses Node.js's `fs.rmSync` method with the options `{ recursive: true, force: true }`.
|
|
@@ -9,5 +11,7 @@
|
|
|
9
11
|
* //=> undefined
|
|
10
12
|
* ```
|
|
11
13
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
+
export function deleteDirectorySync(dirpath: string): string {
|
|
15
|
+
fs.rmSync(dirpath, { recursive: true, force: true })
|
|
16
|
+
return dirpath
|
|
17
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ensureValidWindowsPath } from './ensureValidWindowsPath'
|
|
2
|
+
|
|
3
|
+
describe('ensureValidWindowsPath', () => {
|
|
4
|
+
it('example', () => {
|
|
5
|
+
ensureValidWindowsPath('C:\\Users\\John')
|
|
6
|
+
// => true
|
|
7
|
+
})
|
|
8
|
+
it('accepts backslash', () => {
|
|
9
|
+
expect(ensureValidWindowsPath('C:\\Users\\Benjamin\\Desktop')).toBe(true)
|
|
10
|
+
})
|
|
11
|
+
it('accepts forward slash', () => {
|
|
12
|
+
expect(ensureValidWindowsPath('C:/Users/John/Desktop')).toBe(true)
|
|
13
|
+
})
|
|
14
|
+
it('rejects on both forward and backward slash in same path', () => {
|
|
15
|
+
expect(ensureValidWindowsPath('C:/Users\\John')).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
it('rejects when exceeding max length unless extendedMaxLength is enabled', () => {
|
|
18
|
+
const longPath =
|
|
19
|
+
'C:/Users/John/Desktop/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
|
|
20
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
|
|
21
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' +
|
|
22
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/index.js'
|
|
23
|
+
expect(ensureValidWindowsPath(longPath)).toBe(false)
|
|
24
|
+
expect(ensureValidWindowsPath(longPath, { extendedMaxLength: true })).toBe(true)
|
|
25
|
+
})
|
|
26
|
+
it('accepts network path', () => {
|
|
27
|
+
expect(ensureValidWindowsPath('\\\\192.168.1.2')).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
it('rejects on empty string', () => {
|
|
30
|
+
expect(ensureValidWindowsPath('')).toBe(false)
|
|
31
|
+
})
|
|
32
|
+
it('rejects on illegal characters in windows file names', () => {
|
|
33
|
+
expect(ensureValidWindowsPath('C:\\cool\\wow<wow.js')).toBe(false)
|
|
34
|
+
expect(ensureValidWindowsPath('C:\\cool\\wow>wow.js')).toBe(false)
|
|
35
|
+
expect(ensureValidWindowsPath('C:\\cool\\wow:wow.js')).toBe(false)
|
|
36
|
+
expect(ensureValidWindowsPath('C:\\cool\\wow"wow.js')).toBe(false)
|
|
37
|
+
expect(ensureValidWindowsPath('C:\\cool\\wow|wow.js')).toBe(false)
|
|
38
|
+
expect(ensureValidWindowsPath('C:\\cool\\wow?wow.js')).toBe(false)
|
|
39
|
+
expect(ensureValidWindowsPath('C:\\cool\\wow*wow.js')).toBe(false)
|
|
40
|
+
})
|
|
41
|
+
it('rejects on illegal windows filenames', () => {
|
|
42
|
+
expect(ensureValidWindowsPath('C:\\cool\\CON')).toBe(false)
|
|
43
|
+
expect(ensureValidWindowsPath('C:\\cool\\PRN')).toBe(false)
|
|
44
|
+
expect(ensureValidWindowsPath('C:\\cool\\AUX')).toBe(false)
|
|
45
|
+
expect(ensureValidWindowsPath('C:\\cool\\NUL')).toBe(false)
|
|
46
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM1')).toBe(false)
|
|
47
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM2')).toBe(false)
|
|
48
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM3')).toBe(false)
|
|
49
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM4')).toBe(false)
|
|
50
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM5')).toBe(false)
|
|
51
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM6')).toBe(false)
|
|
52
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM7')).toBe(false)
|
|
53
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM8')).toBe(false)
|
|
54
|
+
expect(ensureValidWindowsPath('C:\\cool\\COM9')).toBe(false)
|
|
55
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT1')).toBe(false)
|
|
56
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT2')).toBe(false)
|
|
57
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT3')).toBe(false)
|
|
58
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT4')).toBe(false)
|
|
59
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT5')).toBe(false)
|
|
60
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT6')).toBe(false)
|
|
61
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT7')).toBe(false)
|
|
62
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT8')).toBe(false)
|
|
63
|
+
expect(ensureValidWindowsPath('C:\\cool\\LPT9')).toBe(false)
|
|
64
|
+
})
|
|
65
|
+
it('accepts on illegal windows filename strings as part of filename', () => {
|
|
66
|
+
expect(ensureValidWindowsPath('C:\\cool\\CON.txt')).toBe(true)
|
|
67
|
+
})
|
|
68
|
+
it('accets on illegal windows filename strings as part of directory name', () => {
|
|
69
|
+
expect(ensureValidWindowsPath('C:\\CON4\\file.txt')).toBe(true)
|
|
70
|
+
})
|
|
71
|
+
it('rejects on illegal windows filename string as directory name', () => {
|
|
72
|
+
expect(ensureValidWindowsPath('C:\\CON\\file.txt')).toBe(false)
|
|
73
|
+
})
|
|
74
|
+
it('rejects on invalid drive letter', () => {
|
|
75
|
+
expect(ensureValidWindowsPath('CC:\\CON\\file.txt')).toBe(false)
|
|
76
|
+
})
|
|
77
|
+
it('throws on instead of returning false when enabled', () => {
|
|
78
|
+
expect(() => ensureValidWindowsPath('', { assert: true })).toThrowError()
|
|
79
|
+
})
|
|
80
|
+
})
|
|
@@ -1,23 +1,57 @@
|
|
|
1
|
-
import { IEnsureValidWindowsPathOptions } from './types/IEnsureValidWindowsPathOptions'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
* - `
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import { IEnsureValidWindowsPathOptions } from './types/IEnsureValidWindowsPathOptions'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check whether a provided windows filesystem path string is valid according to:
|
|
5
|
+
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
|
|
6
|
+
* @param path The path to validate.
|
|
7
|
+
* @returns A boolean indicating whether the path is valid. If the `assert` option is true, the function will throw an error instead of returning false.
|
|
8
|
+
* @throws Will throw an error if the `assert` option is true and the path is invalid.
|
|
9
|
+
* @param options An optional object that may contain the following properties:
|
|
10
|
+
* - `assert`: If true, the function will throw an error if the path is invalid. Default is false.
|
|
11
|
+
* - `extendedMaxLength`: If true, the function will allow paths up to 32767 characters long. Default is false.
|
|
12
|
+
* @example ```ts
|
|
13
|
+
* ensureValidWindowsPath('C:\\Users\\User\\Documents\\file.txt');;
|
|
14
|
+
* //=> true
|
|
15
|
+
* ensureValidWindowsPath('C:/Users/User/Documents/file.txt');;
|
|
16
|
+
* //=> false
|
|
17
|
+
* ensureValidWindowsPath('C:\\Users\\User\\Documents\\file.txt', { assert: true });;
|
|
18
|
+
* //=> throws an error
|
|
19
|
+
* ensureValidWindowsPath('C:\\Users\\User\\Documents\\file.txt', { extendedMaxLength: true });;
|
|
20
|
+
* //=> true
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function ensureValidWindowsPath(path: string, options?: IEnsureValidWindowsPathOptions): boolean {
|
|
24
|
+
const throwOrFalse = (msg: string) => {
|
|
25
|
+
if (options && options.assert === true) {
|
|
26
|
+
throw new Error(`Invalid windows path. ${msg} | input received: ${path}`)
|
|
27
|
+
}
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (path.length === 0) {
|
|
32
|
+
return throwOrFalse('Path string is length 0.')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (path.indexOf('/') !== -1 && path.indexOf('\\') !== -1) {
|
|
36
|
+
return throwOrFalse('Path contains both backslash and forward slash.')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const maxLength = (options && options.extendedMaxLength ? 32767 : 260) - 12
|
|
40
|
+
if (path.length > maxLength) {
|
|
41
|
+
return throwOrFalse(`Maximum length of ${maxLength} exceeded`)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let noDriveLetter = path
|
|
45
|
+
if (/^\w:(\\|\/)/g.test(path)) {
|
|
46
|
+
noDriveLetter = path.substring(2)
|
|
47
|
+
}
|
|
48
|
+
if (/[<>"|?*:]/g.test(noDriveLetter)) {
|
|
49
|
+
return throwOrFalse(`Illegal characters in: ${path} | Illegal characters are: <>"|?:*`)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (/(\\|\/)(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])((\\|\/)|$)/g.test(path)) {
|
|
53
|
+
return throwOrFalse('Illegal name in path string')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return true
|
|
57
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { execSync } from 'child_process'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import { isWindows } from './isWindows'
|
|
4
|
+
|
|
5
|
+
export function getDiskDrivesWindows(): string[] {
|
|
6
|
+
if (!isWindows()) throw new Error('This function is only available on Windows.')
|
|
7
|
+
return execSync('wmic logicaldisk get name')
|
|
8
|
+
.toString()
|
|
9
|
+
.split('\n')
|
|
10
|
+
.map((s) => s.trim())
|
|
11
|
+
.filter((s) => /^[a-z]:/i.test(s))
|
|
12
|
+
.map((s) => s.substring(0, 2) + path.sep)
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
|
|
4
|
+
export async function getLastMofifiedFileIn(dirpath: string): Promise<string> {
|
|
5
|
+
const children = await fs.promises.readdir(dirpath)
|
|
6
|
+
const filtered = children
|
|
7
|
+
.map((fname: string) => {
|
|
8
|
+
const fpath = path.join(dirpath, fname)
|
|
9
|
+
const stat = fs.statSync(fpath)
|
|
10
|
+
return [fpath, stat.mtimeMs, stat.isFile()]
|
|
11
|
+
})
|
|
12
|
+
.filter((a) => a[2]) as [string, number, boolean][]
|
|
13
|
+
const sorted = filtered.sort((a, b) => b[1] - a[1])
|
|
14
|
+
return sorted[0][0]
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { normalizeFileExtension } from './normalizeFileExtension'
|
|
2
|
+
|
|
3
|
+
describe('normalizeFileExtension', () => {
|
|
4
|
+
it('example', () => {
|
|
5
|
+
expect(normalizeFileExtension('.js')).toBe('.js')
|
|
6
|
+
expect(normalizeFileExtension('js')).toBe('.js')
|
|
7
|
+
expect(normalizeFileExtension('test.js')).toBe('.js')
|
|
8
|
+
expect(normalizeFileExtension('.test.js')).toBe('.js')
|
|
9
|
+
expect(normalizeFileExtension('.test.js')).toBe('.js')
|
|
10
|
+
expect(normalizeFileExtension('..js')).toBe('.js')
|
|
11
|
+
expect(normalizeFileExtension('')).toBe('')
|
|
12
|
+
expect(normalizeFileExtension('.')).toBe('')
|
|
13
|
+
expect(() => normalizeFileExtension('.>s')).toThrowError()
|
|
14
|
+
})
|
|
15
|
+
})
|
|
@@ -1,23 +1,29 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Normalize a file extension to the form: .[ext]
|
|
3
|
-
* Anything before the last "." is not returned.
|
|
4
|
-
* @returns The normalized file extension.
|
|
5
|
-
* @throws If the extension contains illegal characters.
|
|
6
|
-
* @param ext file extension
|
|
7
|
-
* @example ```ts
|
|
8
|
-
* normalizeFileExtension('jpg');;
|
|
9
|
-
* //=> '.jpg'
|
|
10
|
-
* normalizeFileExtension('.jpg');;
|
|
11
|
-
* //=> '.jpg'
|
|
12
|
-
* normalizeFileExtension('..jpg');;
|
|
13
|
-
* //=> '.jpg'
|
|
14
|
-
* normalizeFileExtension('');;
|
|
15
|
-
* //=> ''
|
|
16
|
-
* normalizeFileExtension('.');;
|
|
17
|
-
* //=> ''
|
|
18
|
-
* normalizeFileExtension('jpg|png');;
|
|
19
|
-
* //=> Error
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Normalize a file extension to the form: .[ext]
|
|
3
|
+
* Anything before the last "." is not returned.
|
|
4
|
+
* @returns The normalized file extension.
|
|
5
|
+
* @throws If the extension contains illegal characters.
|
|
6
|
+
* @param ext file extension
|
|
7
|
+
* @example ```ts
|
|
8
|
+
* normalizeFileExtension('jpg');;
|
|
9
|
+
* //=> '.jpg'
|
|
10
|
+
* normalizeFileExtension('.jpg');;
|
|
11
|
+
* //=> '.jpg'
|
|
12
|
+
* normalizeFileExtension('..jpg');;
|
|
13
|
+
* //=> '.jpg'
|
|
14
|
+
* normalizeFileExtension('');;
|
|
15
|
+
* //=> ''
|
|
16
|
+
* normalizeFileExtension('.');;
|
|
17
|
+
* //=> ''
|
|
18
|
+
* normalizeFileExtension('jpg|png');;
|
|
19
|
+
* //=> Error
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function normalizeFileExtension(ext: string): string {
|
|
23
|
+
if (ext === '' || ext === '.') return ''
|
|
24
|
+
if (/[<>"|?*:]/g.test(ext)) {
|
|
25
|
+
throw new Error(`Illegal characters in file extension: ${ext} | Illegal characters are: <>"|?:*`)
|
|
26
|
+
}
|
|
27
|
+
if (!ext.includes('.')) return '.' + ext
|
|
28
|
+
return ext.substring(ext.lastIndexOf('.'))
|
|
29
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import readLastLines from 'read-last-lines'
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Read the last n lines of a file.
|
|
4
5
|
* @param filepath The path to the file.
|
|
@@ -6,5 +7,6 @@
|
|
|
6
7
|
* @param encoding The encoding of the file.
|
|
7
8
|
* @example await readLastLines.read('path/to/file.log', 50)
|
|
8
9
|
*/
|
|
9
|
-
export
|
|
10
|
-
|
|
10
|
+
export async function readFileLastLines(filepath: string, n: number, encoding: NodeJS.BufferEncoding = 'utf8') {
|
|
11
|
+
return await readLastLines.read(filepath, n, encoding)
|
|
12
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { readJsonFile } from './readJsonFile'
|
|
3
|
+
|
|
4
|
+
describe(readJsonFile.name, () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
jest.resetAllMocks()
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('should read and parse JSON file successfully', async () => {
|
|
10
|
+
const jsonData = { key: 'value' }
|
|
11
|
+
const jsonString = JSON.stringify(jsonData)
|
|
12
|
+
const readFileMock = jest.spyOn(fs.promises, 'readFile')
|
|
13
|
+
readFileMock.mockResolvedValue(Buffer.from(jsonString))
|
|
14
|
+
const result = await readJsonFile('test.json')
|
|
15
|
+
expect(result).toEqual(jsonData)
|
|
16
|
+
expect(readFileMock).toHaveBeenCalledWith('test.json', 'utf8')
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should throw an error if the file cannot be read', async () => {
|
|
20
|
+
const readFileMock = jest.spyOn(fs.promises, 'readFile')
|
|
21
|
+
const error = new Error('Failed to read file')
|
|
22
|
+
readFileMock.mockRejectedValue(error)
|
|
23
|
+
await expect(readJsonFile('test.json')).rejects.toThrow(error)
|
|
24
|
+
expect(readFileMock).toHaveBeenCalledWith('test.json', 'utf8')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should throw an error if the file has invalid JSON', async () => {
|
|
28
|
+
const readFileMock = jest.spyOn(fs.promises, 'readFile')
|
|
29
|
+
const invalidJsonString = 'invalid json'
|
|
30
|
+
readFileMock.mockResolvedValue(Buffer.from(invalidJsonString))
|
|
31
|
+
await expect(readJsonFile('test.json')).rejects.toThrow(SyntaxError)
|
|
32
|
+
expect(readFileMock).toHaveBeenCalledWith('test.json', 'utf8')
|
|
33
|
+
})
|
|
34
|
+
})
|