PicList/src/main/utils/handleArgv.ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

import path from 'path'
import fs from 'fs-extra'
2022-01-08 02:44:09 -05:00
import { Logger } from 'picgo'
interface IResultFileObject {
2019-12-19 06:17:21 -05:00
path: string
}
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!.map(item => {
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
}
}
}).filter(item => item !== null) as Result
return result
}
}
return []
}
export {
getUploadFiles
}