@bemoje/fs 0.0.12 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -34
- package/package.json +9 -9
- package/src/index.d.ts +35 -0
- package/src/index.js +39 -0
- package/src/index.js.map +1 -0
- package/src/lib/AppData.d.ts +10 -0
- package/src/lib/AppData.js +24 -0
- package/src/lib/AppData.js.map +1 -0
- package/src/lib/JsonFile.d.ts +78 -0
- package/src/lib/JsonFile.js +146 -0
- package/src/lib/JsonFile.js.map +1 -0
- package/src/lib/LogFile.d.ts +50 -0
- package/src/lib/LogFile.js +158 -0
- package/src/lib/LogFile.js.map +1 -0
- package/src/lib/{absoluteToRelativePath.ts → absoluteToRelativePath.d.ts} +13 -20
- package/src/lib/absoluteToRelativePath.js +25 -0
- package/src/lib/absoluteToRelativePath.js.map +1 -0
- package/src/lib/appendLineToFile.d.ts +11 -0
- package/src/lib/appendLineToFile.js +28 -0
- package/src/lib/appendLineToFile.js.map +1 -0
- package/src/lib/{cleanDirectorySync.ts → cleanDirectorySync.d.ts} +16 -24
- package/src/lib/cleanDirectorySync.js +29 -0
- package/src/lib/cleanDirectorySync.js.map +1 -0
- package/src/lib/{createDirectory.ts → createDirectory.d.ts} +1 -5
- package/src/lib/createDirectory.js +25 -0
- package/src/lib/createDirectory.js.map +1 -0
- package/src/lib/{createDirectorySync.ts → createDirectorySync.d.ts} +1 -6
- package/src/lib/createDirectorySync.js +20 -0
- package/src/lib/createDirectorySync.js.map +1 -0
- package/src/lib/{createFileExtensionFilter.ts → createFileExtensionFilter.d.ts} +1 -14
- package/src/lib/createFileExtensionFilter.js +32 -0
- package/src/lib/createFileExtensionFilter.js.map +1 -0
- package/src/lib/{cwdpath.ts → cwdpath.d.ts} +12 -16
- package/src/lib/cwdpath.js +21 -0
- package/src/lib/cwdpath.js.map +1 -0
- package/src/lib/{deleteDirectory.ts → deleteDirectory.d.ts} +1 -5
- package/src/lib/deleteDirectory.js +23 -0
- package/src/lib/deleteDirectory.js.map +1 -0
- package/src/lib/{deleteDirectorySafe.ts → deleteDirectorySafe.d.ts} +1 -5
- package/src/lib/deleteDirectorySafe.js +25 -0
- package/src/lib/deleteDirectorySafe.js.map +1 -0
- package/src/lib/{deleteDirectorySafeSync.ts → deleteDirectorySafeSync.d.ts} +1 -7
- package/src/lib/deleteDirectorySafeSync.js +23 -0
- package/src/lib/deleteDirectorySafeSync.js.map +1 -0
- package/src/lib/{deleteDirectorySync.ts → deleteDirectorySync.d.ts} +1 -6
- package/src/lib/deleteDirectorySync.js +22 -0
- package/src/lib/deleteDirectorySync.js.map +1 -0
- package/src/lib/{ensureValidWindowsPath.ts → ensureValidWindowsPath.d.ts} +22 -57
- package/src/lib/ensureValidWindowsPath.js +54 -0
- package/src/lib/ensureValidWindowsPath.js.map +1 -0
- package/src/lib/getDiskDrivesWindows.d.ts +1 -0
- package/src/lib/getDiskDrivesWindows.js +19 -0
- package/src/lib/getDiskDrivesWindows.js.map +1 -0
- package/src/lib/getDownloadsDirectory.d.ts +1 -0
- package/src/lib/getDownloadsDirectory.js +11 -0
- package/src/lib/getDownloadsDirectory.js.map +1 -0
- package/src/lib/getHomeDirectory.d.ts +1 -0
- package/src/lib/getHomeDirectory.js +13 -0
- package/src/lib/getHomeDirectory.js.map +1 -0
- package/src/lib/getLastMofifiedFileIn.d.ts +1 -0
- package/src/lib/getLastMofifiedFileIn.js +22 -0
- package/src/lib/getLastMofifiedFileIn.js.map +1 -0
- package/src/lib/getRootDir.d.ts +1 -0
- package/src/lib/getRootDir.js +10 -0
- package/src/lib/getRootDir.js.map +1 -0
- package/src/lib/isWindows.d.ts +1 -0
- package/src/lib/isWindows.js +8 -0
- package/src/lib/isWindows.js.map +1 -0
- package/src/lib/{normalizeFileExtension.ts → normalizeFileExtension.d.ts} +22 -29
- package/src/lib/normalizeFileExtension.js +36 -0
- package/src/lib/normalizeFileExtension.js.map +1 -0
- package/src/lib/readFileLastLines.d.ts +7 -0
- package/src/lib/readFileLastLines.js +18 -0
- package/src/lib/readFileLastLines.js.map +1 -0
- package/src/lib/{readJsonFile.ts → readJsonFile.d.ts} +12 -16
- package/src/lib/readJsonFile.js +23 -0
- package/src/lib/readJsonFile.js.map +1 -0
- package/src/lib/{readJsonFileSync.ts → readJsonFileSync.d.ts} +12 -16
- package/src/lib/readJsonFileSync.js +21 -0
- package/src/lib/readJsonFileSync.js.map +1 -0
- package/src/lib/readPackageJson.d.ts +1 -0
- package/src/lib/readPackageJson.js +15 -0
- package/src/lib/readPackageJson.js.map +1 -0
- package/src/lib/removeIllegalFilenameCharacters.d.ts +1 -0
- package/src/lib/removeIllegalFilenameCharacters.js +8 -0
- package/src/lib/removeIllegalFilenameCharacters.js.map +1 -0
- package/src/lib/tempFile.d.ts +6 -0
- package/src/lib/tempFile.js +26 -0
- package/src/lib/tempFile.js.map +1 -0
- package/src/lib/types/IEnsureValidWindowsPathOptions.d.ts +10 -0
- package/src/lib/types/IEnsureValidWindowsPathOptions.js +3 -0
- package/src/lib/types/IEnsureValidWindowsPathOptions.js.map +1 -0
- package/src/lib/types/ILogFileData.d.ts +5 -0
- package/src/lib/types/ILogFileData.js +3 -0
- package/src/lib/types/ILogFileData.js.map +1 -0
- package/src/lib/unzipFile.d.ts +1 -0
- package/src/lib/unzipFile.js +12 -0
- package/src/lib/unzipFile.js.map +1 -0
- package/src/lib/unzipMergeFiles.d.ts +4 -0
- package/src/lib/unzipMergeFiles.js +29 -0
- package/src/lib/unzipMergeFiles.js.map +1 -0
- package/src/lib/wipeDirectorySafe.d.ts +1 -0
- package/src/lib/wipeDirectorySafe.js +14 -0
- package/src/lib/wipeDirectorySafe.js.map +1 -0
- package/src/lib/{writeJsonFile.ts → writeJsonFile.d.ts} +1 -5
- package/src/lib/writeJsonFile.js +25 -0
- package/src/lib/writeJsonFile.js.map +1 -0
- package/src/lib/{writeJsonFileSync.ts → writeJsonFileSync.d.ts} +1 -5
- package/src/lib/writeJsonFileSync.js +22 -0
- package/src/lib/writeJsonFileSync.js.map +1 -0
- package/LICENSE +0 -21
- package/jest.config.ts +0 -13
- package/project.json +0 -30
- package/src/index.ts +0 -35
- package/src/lib/AppData.ts +0 -25
- package/src/lib/JsonFile.ts +0 -158
- package/src/lib/LogFile.ts +0 -117
- package/src/lib/absoluteToRelativePath.test.ts +0 -11
- package/src/lib/appendLineToFile.test.ts +0 -49
- package/src/lib/appendLineToFile.ts +0 -21
- package/src/lib/cleanDirectorySync.test.ts +0 -57
- package/src/lib/createDirectory.test.ts +0 -14
- package/src/lib/createDirectorySync.test.ts +0 -14
- package/src/lib/createFileExtensionFilter.test.ts +0 -17
- package/src/lib/cwdpath.test.ts +0 -48
- package/src/lib/deleteDirectory.test.ts +0 -24
- package/src/lib/deleteDirectorySafe.test.ts +0 -40
- package/src/lib/deleteDirectorySafeSync.test.ts +0 -30
- package/src/lib/deleteDirectorySync.test.ts +0 -23
- package/src/lib/ensureValidWindowsPath.test.ts +0 -80
- package/src/lib/getDiskDrivesWindows.ts +0 -13
- package/src/lib/getDownloadsDirectory.ts +0 -6
- package/src/lib/getHomeDirectory.ts +0 -7
- package/src/lib/getLastMofifiedFileIn.ts +0 -15
- package/src/lib/getRootDir.ts +0 -5
- package/src/lib/isWindows.ts +0 -3
- package/src/lib/normalizeFileExtension.test.ts +0 -15
- package/src/lib/readFileLastLines.ts +0 -12
- package/src/lib/readJsonFile.test.ts +0 -34
- package/src/lib/readJsonFileSync.test.ts +0 -36
- package/src/lib/readPackageJson.ts +0 -9
- package/src/lib/removeIllegalFilenameCharacters.ts +0 -3
- package/src/lib/tempFile.ts +0 -20
- package/src/lib/types/IEnsureValidWindowsPathOptions.ts +0 -10
- package/src/lib/types/ILogFileData.ts +0 -5
- package/src/lib/unzipFile.ts +0 -5
- package/src/lib/unzipMergeFiles.ts +0 -20
- package/src/lib/wipeDirectorySafe.ts +0 -7
- package/src/lib/writeJsonFile.test.ts +0 -26
- package/src/lib/writeJsonFileSync.test.ts +0 -28
- package/tsconfig.spec.json +0 -9
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { createDirectory } from './createDirectory'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Append a line to a file. Creates the file if it does not exist.
|
|
7
|
-
* @param filepath The path of the file.
|
|
8
|
-
* @param line The string data to append.
|
|
9
|
-
* @returns A Promise that resolves when the line has been appended to the file.
|
|
10
|
-
* @throws Will throw an error if the operation fails.
|
|
11
|
-
* @example ```ts
|
|
12
|
-
* await appendLineToFile('/path/to/file', 'This is a new line')
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export async function appendLineToFile(filepath: string, line: string, linebreakBefore = false): Promise<void> {
|
|
16
|
-
if (!fs.existsSync(filepath)) {
|
|
17
|
-
await createDirectory(path.dirname(filepath))
|
|
18
|
-
await fs.promises.writeFile(filepath, '')
|
|
19
|
-
}
|
|
20
|
-
await fs.promises.appendFile(filepath, linebreakBefore ? '\n' + line : line + '\n')
|
|
21
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { cleanDirectorySync } from './cleanDirectorySync'
|
|
4
|
-
|
|
5
|
-
// Mock fs.readdirSync
|
|
6
|
-
jest.mock('fs', () => ({
|
|
7
|
-
readdirSync: jest.fn(),
|
|
8
|
-
statSync: jest.fn(),
|
|
9
|
-
unlinkSync: jest.fn(),
|
|
10
|
-
}))
|
|
11
|
-
|
|
12
|
-
describe(cleanDirectorySync.name, () => {
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
// Clear mock function calls and return values before each test
|
|
15
|
-
jest.clearAllMocks()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('should remove files that satisfy the predicate', () => {
|
|
19
|
-
// Mock fs.readdirSync to return an array of filenames
|
|
20
|
-
const mockFilenames = ['file1.txt', 'file2.txt', 'file3.txt']
|
|
21
|
-
;(fs.readdirSync as jest.Mock).mockReturnValue(mockFilenames)
|
|
22
|
-
|
|
23
|
-
// Mock fs.statSync to return a mock stat object
|
|
24
|
-
const mockStat = { isFile: jest.fn().mockReturnValue(true) }
|
|
25
|
-
;(fs.statSync as jest.Mock).mockReturnValue(mockStat)
|
|
26
|
-
|
|
27
|
-
// Mock the predicate function to return true for file2.txt
|
|
28
|
-
const mockPredicate = jest.fn().mockImplementation((filepath, stat) => {
|
|
29
|
-
return filepath === path.join('path/to/directory', 'file2.txt')
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
// Call the function
|
|
33
|
-
cleanDirectorySync('path/to/directory', mockPredicate)
|
|
34
|
-
|
|
35
|
-
// Verify that fs.unlinkSync was called only for file2.txt
|
|
36
|
-
expect((fs.unlinkSync as jest.Mock).mock.calls).toEqual([[path.join('path/to/directory', 'file2.txt')]])
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('should not remove any files if the predicate returns false for all files', () => {
|
|
40
|
-
// Mock fs.readdirSync to return an array of filenames
|
|
41
|
-
const mockFilenames = ['file1.txt', 'file2.txt', 'file3.txt']
|
|
42
|
-
;(fs.readdirSync as jest.Mock).mockReturnValue(mockFilenames)
|
|
43
|
-
|
|
44
|
-
// Mock fs.statSync to return a mock stat object
|
|
45
|
-
const mockStat = { isFile: jest.fn().mockReturnValue(true) }
|
|
46
|
-
;(fs.statSync as jest.Mock).mockReturnValue(mockStat)
|
|
47
|
-
|
|
48
|
-
// Mock the predicate function to always return false
|
|
49
|
-
const mockPredicate = jest.fn().mockReturnValue(false)
|
|
50
|
-
|
|
51
|
-
// Call the function
|
|
52
|
-
cleanDirectorySync('path/to/directory', mockPredicate)
|
|
53
|
-
|
|
54
|
-
// Verify that fs.unlinkSync was not called
|
|
55
|
-
expect((fs.unlinkSync as jest.Mock).mock.calls).toEqual([])
|
|
56
|
-
})
|
|
57
|
-
})
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { createDirectory } from './createDirectory'
|
|
4
|
-
|
|
5
|
-
describe(createDirectory.name, () => {
|
|
6
|
-
it('should create directory successfully', async () => {
|
|
7
|
-
const dirpath = path.join(process.env.TEMP as string, Date.now().toString())
|
|
8
|
-
expect(fs.existsSync(dirpath)).toBe(false)
|
|
9
|
-
await createDirectory(dirpath)
|
|
10
|
-
expect(fs.existsSync(dirpath)).toBe(true)
|
|
11
|
-
fs.rmSync(dirpath, { recursive: true, force: true })
|
|
12
|
-
expect(fs.existsSync(dirpath)).toBe(false)
|
|
13
|
-
})
|
|
14
|
-
})
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { createDirectorySync } from './createDirectorySync'
|
|
4
|
-
|
|
5
|
-
describe(createDirectorySync.name, () => {
|
|
6
|
-
it('should create directory successfully', () => {
|
|
7
|
-
const dirpath = path.join(process.env.TEMP as string, Date.now().toString())
|
|
8
|
-
expect(fs.existsSync(dirpath)).toBe(false)
|
|
9
|
-
createDirectorySync(dirpath)
|
|
10
|
-
expect(fs.existsSync(dirpath)).toBe(true)
|
|
11
|
-
fs.rmSync(dirpath, { recursive: true, force: true })
|
|
12
|
-
expect(fs.existsSync(dirpath)).toBe(false)
|
|
13
|
-
})
|
|
14
|
-
})
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { createFileExtensionFilter } from './createFileExtensionFilter'
|
|
2
|
-
|
|
3
|
-
describe('createFileExtensionFilter', () => {
|
|
4
|
-
const paths = ['./src/file', './src/file.js', './src/file.ts', './src/file.md', './src/file.json']
|
|
5
|
-
it('creates filter that accepts any extension', () => {
|
|
6
|
-
const filter = createFileExtensionFilter()
|
|
7
|
-
expect(paths.filter(filter)).toStrictEqual(paths)
|
|
8
|
-
})
|
|
9
|
-
it('creates filter that accepts one extension', () => {
|
|
10
|
-
const filter = createFileExtensionFilter('.js')
|
|
11
|
-
expect(paths.filter(filter)).toStrictEqual(['./src/file.js'])
|
|
12
|
-
})
|
|
13
|
-
it('creates filter that accepts multiple extensions', () => {
|
|
14
|
-
const filter = createFileExtensionFilter('.js', 'md')
|
|
15
|
-
expect(paths.filter(filter)).toStrictEqual(['./src/file.js', './src/file.md'])
|
|
16
|
-
})
|
|
17
|
-
})
|
package/src/lib/cwdpath.test.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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,24 +0,0 @@
|
|
|
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,40 +0,0 @@
|
|
|
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,30 +0,0 @@
|
|
|
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,23 +0,0 @@
|
|
|
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,80 +0,0 @@
|
|
|
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,13 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
}
|
package/src/lib/getRootDir.ts
DELETED
package/src/lib/isWindows.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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,12 +0,0 @@
|
|
|
1
|
-
import readLastLines from 'read-last-lines'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Read the last n lines of a file.
|
|
5
|
-
* @param filepath The path to the file.
|
|
6
|
-
* @param n The number of lines to read.
|
|
7
|
-
* @param encoding The encoding of the file.
|
|
8
|
-
* @example await readLastLines.read('path/to/file.log', 50)
|
|
9
|
-
*/
|
|
10
|
-
export async function readFileLastLines(filepath: string, n: number, encoding: NodeJS.BufferEncoding = 'utf8') {
|
|
11
|
-
return await readLastLines.read(filepath, n, encoding)
|
|
12
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import { readJsonFileSync } from './readJsonFileSync'
|
|
3
|
-
|
|
4
|
-
describe(readJsonFileSync.name, () => {
|
|
5
|
-
afterEach(() => {
|
|
6
|
-
jest.restoreAllMocks()
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
it('should read and parse JSON file successfully', () => {
|
|
10
|
-
const jsonData = { key: 'value' }
|
|
11
|
-
const jsonString = JSON.stringify(jsonData)
|
|
12
|
-
const readFileSyncMock = jest.spyOn(fs, 'readFileSync')
|
|
13
|
-
readFileSyncMock.mockReturnValue(Buffer.from(jsonString))
|
|
14
|
-
const result = readJsonFileSync('test.json')
|
|
15
|
-
expect(result).toEqual(jsonData)
|
|
16
|
-
expect(readFileSyncMock).toHaveBeenCalledWith('test.json', 'utf8')
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it('should throw an error if the file cannot be read', () => {
|
|
20
|
-
const readFileSyncMock = jest.spyOn(fs, 'readFileSync')
|
|
21
|
-
const error = new Error('Failed to read file')
|
|
22
|
-
readFileSyncMock.mockImplementation(() => {
|
|
23
|
-
throw error
|
|
24
|
-
})
|
|
25
|
-
expect(() => readJsonFileSync('test.json')).toThrow(error)
|
|
26
|
-
expect(readFileSyncMock).toHaveBeenCalledWith('test.json', 'utf8')
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
it('should throw an error if the file has invalid JSON', () => {
|
|
30
|
-
const readFileSyncMock = jest.spyOn(fs, 'readFileSync')
|
|
31
|
-
const invalidJsonString = 'invalid json'
|
|
32
|
-
readFileSyncMock.mockReturnValue(Buffer.from(invalidJsonString))
|
|
33
|
-
expect(() => readJsonFileSync('test.json')).toThrow(SyntaxError)
|
|
34
|
-
expect(readFileSyncMock).toHaveBeenCalledWith('test.json', 'utf8')
|
|
35
|
-
})
|
|
36
|
-
})
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { readJsonFileSync } from './readJsonFileSync'
|
|
4
|
-
|
|
5
|
-
export function readPackageJson(projectRoot = process.cwd()): Record<string, any> | undefined {
|
|
6
|
-
const filepath = path.join(projectRoot, 'package.json')
|
|
7
|
-
if (!fs.existsSync(filepath)) return
|
|
8
|
-
return readJsonFileSync(filepath)
|
|
9
|
-
}
|
package/src/lib/tempFile.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a temporary file and deletes it after the callback has finished.
|
|
6
|
-
* @param fileExtension - The file extension to use for the temporary file.
|
|
7
|
-
* @param callback - The callback to execute with the temporary file path. The callback can return a promise and the temporary file will not be deleted until the promise has resolved or rejected.
|
|
8
|
-
*/
|
|
9
|
-
export function tempFile(fileExtension: string, callback: (fpath: string) => void | Promise<void>): void {
|
|
10
|
-
const dir = path.join(process.env['TEMP']!, 'bemoje', 'temp')
|
|
11
|
-
const fpath = path.join(dir, Date.now() + fileExtension)
|
|
12
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
13
|
-
const prom = callback(fpath)
|
|
14
|
-
if (prom instanceof Promise) {
|
|
15
|
-
const rm = () => fs.rmSync(fpath)
|
|
16
|
-
prom.then(rm).catch(rm)
|
|
17
|
-
} else {
|
|
18
|
-
fs.rmSync(fpath)
|
|
19
|
-
}
|
|
20
|
-
}
|
package/src/lib/unzipFile.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import extract from 'extract-zip'
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param source Either a directory containing zip files or an array of zip file paths.
|
|
7
|
-
*/
|
|
8
|
-
export async function unzipMergeFiles(source: string | string[], target: string) {
|
|
9
|
-
if (typeof source === 'string') {
|
|
10
|
-
source = fs.readdirSync(source).map((filename) => {
|
|
11
|
-
return path.join(source as string, filename)
|
|
12
|
-
})
|
|
13
|
-
}
|
|
14
|
-
for (const filepath of source) {
|
|
15
|
-
const stat = await fs.promises.stat(filepath)
|
|
16
|
-
if (stat.isDirectory()) continue
|
|
17
|
-
if (!filepath.endsWith('.zip')) continue
|
|
18
|
-
await extract(filepath, { dir: target })
|
|
19
|
-
}
|
|
20
|
-
}
|