-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (125 loc) · 5.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { time } from '@dr-js/core/module/common/format'
import { clock } from '@dr-js/core/module/common/time'
import {
listFile, uploadFile, downloadFile,
uploadDirectory, downloadDirectory,
loadPackageList, listPackage, uploadPackage, downloadPackage, trimLocalPackage
} from 'source'
import { createMarkReplacer, generateMarkMap, configureAuthFile, pingRaceUrlList, pingStatUrlList } from './function'
import { FILE_PACK_INFO, FILE_PACK_TRIM_GZ, MODE_NAME_LIST, parseOption, formatUsage } from './option'
import { name as packageName, version as packageVersion } from '../package.json'
const runMode = async (modeName, { tryGet, tryGetFirst, get, getFirst }) => {
if (tryGet('ping-host')) return console.log(await pingRaceUrlList(tryGet('url-host-list')))
if (tryGet('ping-host-stat')) return console.log(JSON.stringify(await pingStatUrlList(tryGet('url-host-list')), null, 2))
const timeStart = clock()
const log = tryGet('quiet')
? () => {}
: (...args) => console.log(...args, `(${time(clock() - timeStart)})`)
const getPackageListList = () => {
const pathPackageJSONList = get('package-json')
const packageNameFilterList = get('package-name-filter')
const packagePathPrefix = tryGetFirst('package-path-prefix') || ''
return pathPackageJSONList.map((pathPackageJSON) => loadPackageList({ pathPackageJSON, packageNameFilterList, packagePathPrefix, log }))
}
if (modeName === 'package-trim-local') return trimLocalPackage({ trimLocalPath: getFirst('package-trim-local'), packageList: [].concat(...getPackageListList()), log }) // trim all at once to prevent all file get trimmed
const isPackageMode = tryGet('package-json')
const isDirectoryMode = tryGet('directory')
const timeout = tryGetFirst('timeout') || 0
const { authFetch } = await configureAuthFile({
authFile: getFirst('auth-file'),
authKey: tryGetFirst('auth-key')
})
let urlHost
let markReplacer = (string) => string
if (!tryGet('keep-mark')) {
const urlHostList = tryGet('url-host-list')
if (urlHostList) urlHost = await pingRaceUrlList(urlHostList)
markReplacer = createMarkReplacer(generateMarkMap({ 'url-host': urlHost }))
}
const commonOption = { timeout, authFetch, log, filePackInfo: FILE_PACK_INFO, filePackTrimGz: FILE_PACK_TRIM_GZ }
log(`[${packageName}@${packageVersion}]`, [
`mode: ${modeName}`,
`timeout: ${timeout}`,
urlHost && `url-host: ${urlHost}`
].filter(Boolean).join(', '))
if (isPackageMode) {
for (const packageList of getPackageListList()) {
switch (modeName) {
case 'list':
await listPackage({
urlPathAction: markReplacer(getFirst('url-path-action')),
packageList, ...commonOption
})
break
case 'upload':
await uploadPackage({
urlFileUpload: markReplacer(getFirst('url-file-upload')),
urlPathAction: markReplacer(getFirst('url-path-action')),
packageList, ...commonOption
})
break
case 'download':
await downloadPackage({
urlFileDownload: markReplacer(getFirst('url-file-download')),
packageList, ...commonOption
})
break
}
}
return
}
if (isDirectoryMode) {
commonOption.isTrimGz = tryGet('trim-gz')
commonOption.isUse7z = tryGet('use-7z')
}
switch (modeName) {
case 'list':
return listFile({
urlPathAction: markReplacer(getFirst('url-path-action')),
listKeyPrefix: markReplacer(tryGetFirst('list-key-prefix') || ''),
...commonOption
})
case 'upload': {
commonOption.urlFileUpload = markReplacer(getFirst('url-file-upload'))
commonOption.key = markReplacer(getFirst('upload-key'))
return isDirectoryMode
? uploadDirectory({
directoryInputPath: getFirst('upload-directory'),
infoString: markReplacer((tryGet('directory-pack-info') || [ '{time-iso}' ]).join('\n')),
...commonOption
})
: uploadFile({
fileInputPath: getFirst('upload-file'),
...commonOption
})
}
case 'download': {
commonOption.urlFileDownload = markReplacer(getFirst('url-file-download'))
commonOption.key = markReplacer(getFirst('download-key'))
return isDirectoryMode
? downloadDirectory({
directoryOutputPath: getFirst('download-directory'),
...commonOption
})
: downloadFile({
fileOutputPath: getFirst('download-file'),
...commonOption
})
}
}
}
const main = async () => {
const optionData = await parseOption()
if (optionData.tryGet('version')) return console.log(JSON.stringify({ packageName, packageVersion }, null, 2))
if (optionData.tryGet('help')) return console.log(formatUsage())
const modeName = MODE_NAME_LIST.find((name) => optionData.tryGet(name))
if (!modeName) throw new Error('no mode specified')
await runMode(modeName, optionData).catch((error) => {
console.warn(`[Error] in mode: ${modeName}:`, error.stack || error)
process.exit(2)
})
}
main().catch((error) => {
console.warn(formatUsage(error.stack || error, 'simple'))
process.exit(1)
})