-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.js
206 lines (179 loc) · 6.91 KB
/
package.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { join, resolve, relative, normalize, dirname, basename } from 'path'
import { readFileSync } from 'fs'
import { padTable } from '@dr-js/core/module/common/format'
import { indentLine } from '@dr-js/core/module/common/string'
import { compareSemVer } from '@dr-js/core/module/common/module/SemVer'
import { STAT_ERROR, getPathLstat, toPosixPath } from '@dr-js/core/module/node/file/Path'
import { getFileList } from '@dr-js/core/module/node/file/Directory'
import { modifyDelete, modifyRename } from '@dr-js/core/module/node/file/Modify'
import { PATH_ACTION_TYPE } from '@dr-js/node/module/module/PathAction/base'
import { pathAction, fileUpload, fileDownload } from '@dr-js/node/module/server/feature/Explorer/client'
const loadPackageList = ({
pathPackageJSON,
packageNameFilterList = [], // array of RegExp or String(startsWith)
packagePathPrefix = '',
log
}) => {
__DEV__ && console.log({ packageNameFilterList })
const filterList = packageNameFilterList.map( // normalize to `filter.test(string)`
(packageNameFilter) => (packageNameFilter instanceof RegExp)
? packageNameFilter
: { test: (string) => string.startsWith(packageNameFilter) }
)
const {
dependencies,
devDependencies,
// peerDependencies, // NOTE: ignore peerDependencies, since this is not intended to be installed
optionalDependencies // TODO: support allow failed install pass?
} = JSON.parse(String(readFileSync(pathPackageJSON)))
__DEV__ && console.log({
dependencies,
devDependencies,
// peerDependencies,
optionalDependencies
})
const packageList = Object
.entries({
...dependencies,
...devDependencies,
// ...peerDependencies,
...optionalDependencies
})
.filter(([ packageName ]) => filterList.find((filter) => filter.test(packageName)))
.map(([ packageName, packagePath ]) => {
if (!packagePath.startsWith('./')) throw new Error(`[loadPackageList] invalid packagePath: ${packagePath} (${packageName})`)
const serverPath = toPosixPath(normalize(join(packagePathPrefix, packagePath)))
const localPath = resolve(dirname(pathPackageJSON), packagePath)
return { packageName, packagePath, serverPath, localPath }
})
__DEV__ && console.log('[loadPackageList] get filtered packageList:', packageList)
log(`found ${packageList.length} package in ${pathPackageJSON}`)
return packageList
}
const REGEXP_PACKAGE_VERSION = /-(\d+\.\d+\.\d+.*)\.tgz$/ // pick semver version from: `some/path/<name>-<version>.tgz`
const unique = (array) => [ ...new Set(array) ]
const listPackage = async ({
packageList,
urlPathAction,
timeout,
authFetch,
log
}) => {
// first get server file
const serverPathList = packageList.map(({ serverPath }) => toPosixPath(dirname(serverPath)))
__DEV__ && console.log({ serverPathList, nameList: unique(serverPathList) })
const { resultList: contentList } = await pathAction({
actionType: PATH_ACTION_TYPE.DIRECTORY_CONTENT,
key: '',
nameList: unique(serverPathList),
urlPathAction,
timeout,
authFetch
})
__DEV__ && console.log({ contentList })
__DEV__ && console.log(JSON.stringify(contentList, null, 2))
const serverPackageList = unique(contentList.reduce((o, { key, fileList }) => {
fileList.forEach(([ name ]) => {
const result = REGEXP_PACKAGE_VERSION.exec(name) || []
const packageVersion = result[ 1 ]
const packageFileName = name.slice(0, result.index)
const serverPath = toPosixPath(join(key, name))
o.push({ packageVersion, packageFileName, serverPath })
})
return o
}, []))
__DEV__ && console.log({ serverPackageList })
const statusList = []
for (let index = 0, indexMax = packageList.length; index < indexMax; index++) {
const { packageName, packagePath } = packageList[ index ]
const [ , packageVersion = '0.0.0' ] = REGEXP_PACKAGE_VERSION.exec(packagePath) || []
const packageFileName = basename(packagePath)
const relatedServerPackageList = serverPackageList
.filter((serverPackage) => packageFileName.startsWith(serverPackage.packageFileName))
.sort((a, b) => compareSemVer(b.packageVersion, a.packageVersion)) // big version first
const serverPackageVersion = (relatedServerPackageList[ 0 ] || {}).packageVersion
statusList.push([
packageName,
packageVersion,
serverPackageVersion,
relatedServerPackageList.length,
packageVersion === serverPackageVersion
])
}
log(`[PackageList]\n${indentLine(padTable({
table: [ [ 'name', 'local', 'server', '#', 'match' ], ...statusList ],
padFuncList: [ 'L', 'R', 'L', 'R', 'R' ]
}))}`)
}
const uploadPackage = async ({
packageList,
urlFileUpload,
urlPathAction, // TODO: strange url dependency
timeout,
authFetch,
log
}) => {
// first check if server file exist
const { resultList: visibleList } = await pathAction({
actionType: PATH_ACTION_TYPE.PATH_VISIBLE,
key: '',
nameList: packageList.map(({ serverPath }) => serverPath),
urlPathAction,
timeout,
authFetch
})
for (let index = 0, indexMax = packageList.length; index < indexMax; index++) {
const tag = `[PackageUpload|${index + 1}/${indexMax}]`
const { packageName, packagePath, serverPath, localPath } = packageList[ index ]
const { isVisible } = visibleList[ index ]
if (isVisible) log(tag, `exist: ${packagePath}`)
else {
await fileUpload({ fileInputPath: localPath, key: serverPath, urlFileUpload, timeout, authFetch, log })
log(tag, `done ${packageName}: ${packagePath}`)
}
}
}
const downloadPackage = async ({
packageList,
urlFileDownload,
timeout,
authFetch,
log
}) => {
for (let index = 0, indexMax = packageList.length; index < indexMax; index++) {
const tag = `[PackageDownload|${index + 1}/${indexMax}]`
const { packageName, packagePath, serverPath, localPath } = packageList[ index ]
if (STAT_ERROR !== await getPathLstat(localPath)) log(tag, `exist: ${packagePath}`)
else {
const fileTempPath = `${localPath}_temp_${Date.now().toString(36)}`
await fileDownload({ fileOutputPath: fileTempPath, key: serverPath, urlFileDownload, timeout, authFetch, log })
await modifyRename(fileTempPath, localPath)
log(tag, `done ${packageName}: ${packagePath}`)
}
}
}
const trimLocalPackage = async ({
packageList,
trimLocalPath,
log
}) => {
const keepPathSet = new Set(packageList.map(({ localPath }) => resolve(localPath)))
const fileList = await getFileList(trimLocalPath)
const tag = `[PackageTrimLocal|${keepPathSet.size}-${fileList.length}]`
let trimCount = 0
for (const file of fileList) {
const path = resolve(file) // resolve so path is comparable
if (keepPathSet.has(path)) continue
await modifyDelete(path)
trimCount++
log(tag, `trim: ${relative(trimLocalPath, file)}`)
}
log(tag, `done trim ${trimCount} of ${fileList.length}`)
}
export {
loadPackageList,
listPackage,
uploadPackage,
downloadPackage,
trimLocalPackage
}