Files
cli/assistant/src/module/upload/mv.ts

35 lines
1.1 KiB
TypeScript

import type { AssistantConfig } from '@/module/assistant/index.ts';
import fs from 'node:fs';
import path from 'node:path';
export class UploadManager {
config: AssistantConfig;
constructor(config: AssistantConfig) {
this.config = config;
}
mvFile(opts: {
temppath: string;
type: 'file' | 's3' | 'app-file';
targetPath: string;
}) {
const { type = 'file', temppath, targetPath } = opts;
const pageDir = this.config.configPath?.pagesDir!;
const appsDir = this.config.configPath?.appsDir!;
let dir = type === 'app-file' ? appsDir : pageDir;
if (type === 'file' || type === 'app-file') {
const fullTargetPath = targetPath.startsWith('/')
? targetPath
: path.join(dir, targetPath);
const targetDir = fullTargetPath.substring(0, fullTargetPath.lastIndexOf('/'));
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
fs.renameSync(temppath, fullTargetPath);
return fullTargetPath;
}
}
}
export const uploadManager = (config: AssistantConfig) => {
return new UploadManager(config);
}