refactor: fix file condition checks

This commit is contained in:
wancat committed 2024-02-19 18:35:39 +08:00
1 parent 64215d3ef1
commit 4ab3096fa5
5 files changed
+16 -20

No files matched your search

+1 -1
View File
@@ -8,6 +8,6 @@
"license": "MIT",
"scripts": {
"build": "tsc",
"watch": "tsc -w"
"dev": "tsc -w"
}
}
+9 -15
View File
@@ -1,23 +1,17 @@
const fileTypes = {
file: '-f',
folder: '-d',
link: '-L',
socket: '-S',
unknown: '-e',
}
import type { IFileCondition } from '@linux-odyssey/models'
export type FileType = keyof typeof fileTypes
export type File = {
path: string
type: FileType
}
const fileTypes = new Map([
['file', '-f'],
['folder', '-d'],
['link', '-L'],
['socket', '-S'],
])
function sanitizePath(path: string): string {
return path.replace(/[^a-zA-Z0-9._/-]/g, '')
}
export function buildFileCheckCmd(file: File): string[] {
export function buildFileCheckCmd(file: IFileCondition): string[] {
const { path, type } = file
return ['test', fileTypes[type] || '-e', sanitizePath(path)]
return ['test', fileTypes.get(type) || '-e', sanitizePath(path)]
}