feat: add file matcher

This commit is contained in:
wancat committed 2024-03-08 00:13:38 +08:00
1 parent b5e20766a3
commit d612b2d6af
4 files changed
+123

No files matched your search

+2
View File
@@ -66,6 +66,8 @@ module.exports = {
'@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-constructor': 'error',
'no-empty-function': 'off', 'no-empty-function': 'off',
'@typescript-eslint/no-empty-function': 'error', '@typescript-eslint/no-empty-function': 'error',
'no-shadow': 'off',
'@typescript-eslint/no-shadow': 'error',
}, },
overrides: [ overrides: [
{ {
@@ -0,0 +1,15 @@
import { FileInput, IFileExistenceChecker } from '../types'
interface FileExistenceInput extends FileInput {
exists: boolean
}
export async function checkFiles(
checker: IFileExistenceChecker,
files: FileExistenceInput[]
) {
const existences = await Promise.all(
files.map((file) => checker.exists(file))
)
return existences.every((exists, index) => exists === files[index].exists)
}
+14
View File
@@ -13,3 +13,17 @@ export interface ICondition {
or?: ICondition[] or?: ICondition[]
not?: ICondition not?: ICondition
} }
export enum FileType {
FILE = 'file',
FOLDER = 'folder',
}
export interface FileInput {
path: string
type: FileType
}
export interface IFileExistenceChecker {
exists(file: FileInput): Promise<boolean>
}
+92
View File
@@ -0,0 +1,92 @@
import { test, expect } from '@jest/globals'
import { IFileExistenceChecker, FileType, FileInput } from '../src/types.js'
import { checkFiles } from '../src/condition/FileMatcher.js'
class MockFileChecker implements IFileExistenceChecker {
private files: FileInput[] = [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
},
{
path: '/home/user/world.txt',
type: FileType.FILE,
},
{
path: '/home/user/Downloads',
type: FileType.FOLDER,
},
]
async exists(file: FileInput): Promise<boolean> {
return this.files.some((f) => f.path === file.path && f.type === file.type)
}
}
test('file matcher', async () => {
const checker = new MockFileChecker()
expect(
await checkFiles(checker, [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
exists: true,
},
])
).toBe(true)
expect(
await checkFiles(checker, [
{
path: '/home/user/not-exists.txt',
type: FileType.FILE,
exists: true,
},
])
).toBe(false)
expect(
await checkFiles(checker, [
{
path: '/home/user/not-exists.txt',
type: FileType.FILE,
exists: false,
},
])
).toBe(true)
expect(
await checkFiles(checker, [
{
path: '/home/user/Downloads',
type: FileType.FOLDER,
exists: true,
},
])
).toBe(true)
expect(
await checkFiles(checker, [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
exists: true,
},
{
path: '/home/user/Downloads',
type: FileType.FOLDER,
exists: true,
},
])
).toBe(true)
expect(
await checkFiles(checker, [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
exists: true,
},
{
path: '/home/user/not-exist.txt',
type: FileType.FILE,
exists: true,
},
])
).toBe(false)
})