Storage class is now OOP
This commit is contained in:
@ -3,7 +3,7 @@ import * as path from 'path'
|
||||
import { pathToFileURL } from 'url'
|
||||
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
import { getAppPath } from '#application/storage'
|
||||
import Storage from '#application/storage'
|
||||
import { Command } from '#application/types'
|
||||
|
||||
export class CommandRegistry {
|
||||
@ -15,7 +15,7 @@ export class CommandRegistry {
|
||||
}
|
||||
|
||||
public async loadCommands(): Promise<void> {
|
||||
const directory = getAppPath('commands')
|
||||
const directory = Storage.getAppPath('commands')
|
||||
this.logger.info(`Loading commands from: ${directory}`)
|
||||
|
||||
try {
|
||||
@ -32,7 +32,7 @@ export class CommandRegistry {
|
||||
|
||||
private async loadCommandFile(file: fs.Dirent): Promise<void> {
|
||||
try {
|
||||
const filePath = getAppPath('commands', file.name)
|
||||
const filePath = Storage.getAppPath('commands', file.name)
|
||||
const commandName = path.basename(file.name, path.extname(file.name))
|
||||
|
||||
const module = await import(pathToFileURL(filePath).href)
|
||||
|
@ -1,34 +1,71 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import config from './config'
|
||||
import config from '#application/config'
|
||||
|
||||
export function getRootPath(folder: string, ...additionalSegments: string[]) {
|
||||
return path.join(process.cwd(), folder, ...additionalSegments)
|
||||
}
|
||||
class Storage {
|
||||
private readonly baseDir: string
|
||||
private readonly rootDir: string
|
||||
|
||||
export function getAppPath(folder: string, ...additionalSegments: string[]) {
|
||||
const baseDir = config.ENV === 'development' ? 'src' : 'dist'
|
||||
return path.join(process.cwd(), baseDir, folder, ...additionalSegments)
|
||||
}
|
||||
constructor() {
|
||||
this.rootDir = process.cwd()
|
||||
this.baseDir = config.ENV === 'development' ? 'src' : 'dist'
|
||||
}
|
||||
|
||||
export function getPublicPath(folder: string, ...additionalSegments: string[]) {
|
||||
return path.join(process.cwd(), 'public', folder, ...additionalSegments)
|
||||
}
|
||||
/**
|
||||
* Gets path relative to project root
|
||||
*/
|
||||
public getRootPath(folder: string, ...additionalSegments: string[]): string {
|
||||
return path.join(this.rootDir, folder, ...additionalSegments)
|
||||
}
|
||||
|
||||
export function doesPathExist(path: string) {
|
||||
try {
|
||||
fs.accessSync(path, fs.constants.F_OK)
|
||||
return true
|
||||
} catch (e) {
|
||||
return false
|
||||
/**
|
||||
* Gets path relative to app directory (src/dist)
|
||||
*/
|
||||
public getAppPath(folder: string, ...additionalSegments: string[]): string {
|
||||
return path.join(this.rootDir, this.baseDir, folder, ...additionalSegments)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets path relative to public directory
|
||||
*/
|
||||
public getPublicPath(folder: string, ...additionalSegments: string[]): string {
|
||||
return path.join(this.rootDir, 'public', folder, ...additionalSegments)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a path exists
|
||||
* @throws Error if path is empty or invalid
|
||||
*/
|
||||
public doesPathExist(pathToCheck: string): boolean {
|
||||
if (!pathToCheck) {
|
||||
throw new Error('Path cannot be empty')
|
||||
}
|
||||
|
||||
try {
|
||||
fs.accessSync(pathToCheck, fs.constants.F_OK)
|
||||
return true
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a directory and any necessary parent directories
|
||||
* @throws Error if directory creation fails
|
||||
*/
|
||||
public createDir(dirPath: string): void {
|
||||
if (!dirPath) {
|
||||
throw new Error('Directory path cannot be empty')
|
||||
}
|
||||
|
||||
try {
|
||||
fs.mkdirSync(dirPath, { recursive: true })
|
||||
} catch (error) {
|
||||
const typedError = error as Error
|
||||
throw new Error(`Failed to create directory: ${typedError.message}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createDir(path: string) {
|
||||
try {
|
||||
fs.mkdirSync(path, { recursive: true })
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
export default new Storage()
|
||||
|
Reference in New Issue
Block a user