🔨 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 files = getUploadFiles(argv, cwd, logger)
if (files === null || files.length > 0) {
// 如果有文件列表作为参数,说明是命令行启动
if (files === null) {
logger.info('cli -> uploading file from clipboard')
uploadClipboardFiles()
} else {
logger.info('cli -> uploading files from cli', ...files.map(item => item.path))
const win = windowManager.getAvailableWindow()
uploadChoosedFiles(win.webContents, files)
}
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
}

View File

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