2019-04-14 03:41:48 -04:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs-extra'
|
2023-02-15 10:36:47 -05:00
|
|
|
import { Logger } from 'piclist'
|
2022-08-25 23:04:44 -04:00
|
|
|
import { isUrl } from '~/universal/utils/common'
|
2021-04-23 12:44:49 -04:00
|
|
|
interface IResultFileObject {
|
2019-12-19 06:17:21 -05:00
|
|
|
path: string
|
|
|
|
}
|
2021-04-23 12:44:49 -04:00
|
|
|
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) {
|
2019-04-14 22:28:11 -04:00
|
|
|
return null // for uploading images in clipboard
|
2021-04-23 12:44:49 -04:00
|
|
|
} else if ((fileList?.length || 0) > 0) {
|
|
|
|
const result = fileList!.map(item => {
|
2022-08-25 23:04:44 -04:00
|
|
|
if (isUrl(item)) {
|
|
|
|
return {
|
|
|
|
path: item
|
|
|
|
}
|
|
|
|
}
|
2021-04-23 12:44:49 -04:00
|
|
|
if (path.isAbsolute(item)) {
|
|
|
|
return {
|
|
|
|
path: item
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-04 10:40:28 -05:00
|
|
|
const tempPath = path.join(cwd, item)
|
2021-04-23 12:44:49 -04:00
|
|
|
if (fs.existsSync(tempPath)) {
|
2019-04-14 22:28:11 -04:00
|
|
|
return {
|
2021-04-23 12:44:49 -04:00
|
|
|
path: tempPath
|
2019-04-14 22:28:11 -04:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-23 12:44:49 -04:00
|
|
|
logger.warn(`cli -> can't get file: ${tempPath}, invalid path`)
|
|
|
|
return null
|
2019-04-14 03:41:48 -04:00
|
|
|
}
|
2021-04-23 12:44:49 -04:00
|
|
|
}
|
|
|
|
}).filter(item => item !== null) as Result
|
2019-04-14 22:28:11 -04:00
|
|
|
return result
|
|
|
|
}
|
2019-04-14 03:41:48 -04:00
|
|
|
}
|
2019-04-14 22:28:11 -04:00
|
|
|
return []
|
2019-04-14 03:41:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
getUploadFiles
|
|
|
|
}
|