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

+2 -2
View File
@@ -2,8 +2,8 @@ import { Schema } from 'mongoose'
export interface IFileCondition {
path: string
type?: string
exists?: boolean
type: string
exists: boolean
}
export interface ICondition {
+1
View File
@@ -5,6 +5,7 @@ export { Quest, IQuest } from './quest.js'
export { Session, ISession } from './session.js'
export { Command, ICommand } from './command.js'
export { UserProfile, IUserProfile } from './userProfile.js'
export { IFileCondition, ICondition } from './condition.js'
export default async function connectDB(uri: string) {
const db = await mongoose.connect(uri)
+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)]
}