diff --git a/packages/models/src/condition.ts b/packages/models/src/condition.ts index d878348..0b4daa4 100644 --- a/packages/models/src/condition.ts +++ b/packages/models/src/condition.ts @@ -2,8 +2,8 @@ import { Schema } from 'mongoose' export interface IFileCondition { path: string - type?: string - exists?: boolean + type: string + exists: boolean } export interface ICondition { diff --git a/packages/models/src/index.ts b/packages/models/src/index.ts index a043ec3..f52f223 100644 --- a/packages/models/src/index.ts +++ b/packages/models/src/index.ts @@ -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) diff --git a/packages/utils/package.json b/packages/utils/package.json index c33d17f..633f7b0 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -8,6 +8,6 @@ "license": "MIT", "scripts": { "build": "tsc", - "watch": "tsc -w" + "dev": "tsc -w" } } diff --git a/packages/utils/src/commands.ts b/packages/utils/src/commands.ts index b40ea2e..92f0533 100644 --- a/packages/utils/src/commands.ts +++ b/packages/utils/src/commands.ts @@ -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)] } diff --git a/server/src/containers/cli.js b/server/src/containers/cli.ts similarity index 80% rename from server/src/containers/cli.js rename to server/src/containers/cli.ts index dc4640a..c13316e 100644 --- a/server/src/containers/cli.js +++ b/server/src/containers/cli.ts @@ -1,13 +1,14 @@ import { execFile } from 'child_process' import { buildFileCheckCmd } from '@linux-odyssey/utils' +import type { IFileCondition } from '@linux-odyssey/models' -function sanitizeId(id) { +function sanitizeId(id: string) { return id.replace(/[^a-zA-Z0-9]/g, '') } // eslint-disable-next-line import/prefer-default-export -export function checkFile(id, file) { +export function checkFile(id: string, file: IFileCondition) { return new Promise((resolve, reject) => { const cmd = buildFileCheckCmd(file) // WARNING: This is vulnerable to command injection