🔨 Refactor(custom): refactored cli upload

This commit is contained in:
Kuingsmile 2024-11-13 15:13:00 +08:00
parent 7d5eaf17c4
commit 876ec16a48
2 changed files with 30 additions and 64 deletions

View File

@ -41,18 +41,20 @@ const isDevelopment = process.env.NODE_ENV !== 'production'
const handleStartUpFiles = (argv: string[], cwd: string) => { const handleStartUpFiles = (argv: string[], cwd: string) => {
const files = getUploadFiles(argv, cwd, logger) const files = getUploadFiles(argv, cwd, logger)
if (files === null || files.length > 0) {
// 如果有文件列表作为参数,说明是命令行启动
if (files === null) { if (files === null) {
logger.info('cli -> uploading file from clipboard') logger.info('cli -> uploading file from clipboard')
uploadClipboardFiles() uploadClipboardFiles()
} else {
logger.info('cli -> uploading files from cli', ...files.map(item => item.path))
const win = windowManager.getAvailableWindow()
uploadChoosedFiles(win.webContents, files)
}
return true return true
} }
if (files.length > 0) {
logger.info('cli -> uploading files from cli', ...files.map(file => file.path))
const win = windowManager.getAvailableWindow()
uploadChoosedFiles(win.webContents, files)
return true
}
return false return false
} }

View File

@ -9,61 +9,25 @@ interface IResultFileObject {
} }
type Result = IResultFileObject[] type Result = IResultFileObject[]
interface IHandleArgvResult {
success: boolean
fileList?: string[]
}
const handleArgv = (argv: string[]): IHandleArgvResult => {
const uploadIndex = argv.indexOf('upload')
if (uploadIndex !== -1) {
const fileList = argv.slice(1 + uploadIndex)
return {
success: true,
fileList
}
}
return {
success: false
}
}
const getUploadFiles = (argv = process.argv, cwd = process.cwd(), logger: Logger) => { const getUploadFiles = (argv = process.argv, cwd = process.cwd(), logger: Logger) => {
const { success, fileList } = handleArgv(argv) const uploadIndex = argv.indexOf('upload')
if (!success) { if (uploadIndex === -1) return []
return [] const fileList = argv.slice(uploadIndex + 1)
} else {
if (fileList?.length === 0) { if (fileList.length === 0) return null // for uploading images in clipboard
return null // for uploading images in clipboard
} else if ((fileList?.length || 0) > 0) { return fileList
const result = fileList!
.map(item => { .map(item => {
if (isUrl(item)) { if (isUrl(item) || path.isAbsolute(item)) return { path: item }
return {
path: item const resolvedPath = path.join(cwd, item)
if (fs.existsSync(resolvedPath)) {
return { path: resolvedPath }
} }
} logger.warn(`cli -> can't get file: ${resolvedPath}, invalid path`)
if (path.isAbsolute(item)) {
return {
path: item
}
} else {
const tempPath = path.join(cwd, item)
if (fs.existsSync(tempPath)) {
return {
path: tempPath
}
} else {
logger.warn(`cli -> can't get file: ${tempPath}, invalid path`)
return null return null
}
}
}) })
.filter(item => item !== null) as Result .filter(item => item !== null) as Result
return result
}
}
return []
} }
export { getUploadFiles } export { getUploadFiles }