@fanwang/pack-util 3.1.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +1 -0
- package/README.md +20 -20
- package/bin/index.js +92 -57
- package/eslint.config.js +3 -0
- package/package.json +2 -1
package/.vscode/settings.json
CHANGED
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
# pack-util
|
|
1
|
+
# pack-util
|
|
2
2
|
|
|
3
|
-
**中文** | [English](./README.en-US.md)
|
|
3
|
+
**中文** | [English](./README.en-US.md)
|
|
4
4
|
|
|
5
5
|
一个打包工具,可以在互联网网机器上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,并批量上传到 nexus 搭建的 npm 私服上。支持 `pnpm`/`yarn`/`npm` 打包工具。
|
|
6
6
|
|
|
7
|
-
## 安装
|
|
7
|
+
## 安装
|
|
8
8
|
|
|
9
9
|
全局安装即可。
|
|
10
10
|
```sh
|
|
11
11
|
npm install -g @fanwang/pack-util
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
## 使用
|
|
14
|
+
## 使用
|
|
15
15
|
|
|
16
|
-
**使用说明**
|
|
16
|
+
**使用说明**
|
|
17
17
|
|
|
18
18
|
如果使用控制台命令的时候提示电脑禁止运行脚本,用管理员身份运行 cmd 或者 vscode 等然后在命令行输入:
|
|
19
19
|
|
|
@@ -23,11 +23,11 @@ Set-ExecutionPolicy RemoteSigned
|
|
|
23
23
|
|
|
24
24
|
建议先将源仓库设为阿里镜像仓库,这样打包速度更快(yarn 和 npm 需要同时设置):
|
|
25
25
|
```sh
|
|
26
|
-
yarn config set registry https://registry.npmmirror.com
|
|
27
|
-
npm config set registry https://registry.npmmirror.com
|
|
26
|
+
yarn config set registry https://registry.npmmirror.com
|
|
27
|
+
npm config set registry https://registry.npmmirror.com
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
### 第一步. 将项目所有依赖包下载至 ./node_modules_pack 文件夹
|
|
30
|
+
### 第一步. 将项目所有依赖包下载至 ./node_modules_pack 文件夹
|
|
31
31
|
```sh
|
|
32
32
|
cd your_project
|
|
33
33
|
pack-util pack
|
|
@@ -35,13 +35,14 @@ pack-util pack
|
|
|
35
35
|
```
|
|
36
36
|
**解释:**
|
|
37
37
|
工具是根据你使用的包管理工具来检索项目的所有依赖包。
|
|
38
|
-
- pnpm: 根据 pnpm-lock.yaml 文件来检索
|
|
39
|
-
- yarn: 根据 yarn.lock 文件来检索
|
|
40
|
-
- npm: 根据 npm list --all 的输出来检索
|
|
38
|
+
- pnpm: 根据 `pnpm-lock.yaml` 文件来检索
|
|
39
|
+
- yarn: 根据 `yarn.lock` 文件来检索
|
|
40
|
+
- npm: 根据 `npm list --all` 的输出来检索
|
|
41
|
+
|
|
41
42
|
因此,如果单纯的想从互联网下载一些包,可以仅凭一个 package.json 文件,利用不同的包管理工具执行 install 得到相应的检索文件后,就可以执行 pack 操作了。甚至建立一个空文件夹,然后通过 `yarn add` | `pnpm add` | `npm install` 添加自己需要的包来直接得到检索文件,也是可以的。
|
|
42
43
|
|
|
43
|
-
### 第二步. 将依赖包批量上传到 nexus 搭建的 npm 私服上
|
|
44
|
-
1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网机器上,然后执行上传。
|
|
44
|
+
### 第二步. 将依赖包批量上传到 nexus 搭建的 npm 私服上
|
|
45
|
+
1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网机器上,然后执行上传。
|
|
45
46
|
```sh
|
|
46
47
|
cd your_project
|
|
47
48
|
pack-util upload
|
|
@@ -49,7 +50,7 @@ pack-util upload
|
|
|
49
50
|
**解释:**
|
|
50
51
|
工具是在命令执行目录下查找 ./node_modules_pack 文件夹,然后把文件夹中的包批量上传,所以只需要把 ./node_modules_pack 文件夹拷贝到命令执行目录即可。
|
|
51
52
|
|
|
52
|
-
2. 输入参数
|
|
53
|
+
2. 输入参数
|
|
53
54
|
```
|
|
54
55
|
? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
|
|
55
56
|
? Please input your Nexus npm repository (eg: localNpm): localNpm
|
|
@@ -57,26 +58,25 @@ pack-util upload
|
|
|
57
58
|
? password: [hidden]
|
|
58
59
|
```
|
|
59
60
|
|
|
60
|
-
3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传(也可以直接重试上传)。
|
|
61
|
+
3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传(也可以直接重试上传)。
|
|
61
62
|
|
|
62
|
-
### 第三步. 在内网机器上执行包依赖安装
|
|
63
|
+
### 第三步. 在内网机器上执行包依赖安装
|
|
63
64
|
先删除 `yarn.lock` / `package-lock.json` / `pnpm-lock.yaml` 文件。
|
|
64
65
|
|
|
65
|
-
**PNPM**
|
|
66
|
+
**PNPM**
|
|
66
67
|
```
|
|
67
68
|
pnpm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
68
69
|
pnpm install
|
|
69
70
|
```
|
|
70
71
|
|
|
71
|
-
**YARN**
|
|
72
|
+
**YARN**
|
|
72
73
|
```
|
|
73
74
|
yarn config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
74
75
|
yarn install
|
|
75
76
|
```
|
|
76
77
|
|
|
77
|
-
**NPM**
|
|
78
|
+
**NPM**
|
|
78
79
|
```
|
|
79
80
|
npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
80
81
|
npm install --save --legacy-peer-deps # 注意:在私网上安装 npm 包需要忽略对 peer 依赖
|
|
81
82
|
```
|
|
82
|
-
|
package/bin/index.js
CHANGED
|
@@ -1,49 +1,68 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import { createReadStream, createWriteStream, mkdirSync, readdirSync, readFileSync } from 'node:fs'
|
|
4
|
+
import process from 'node:process'
|
|
5
|
+
import url from 'node:url'
|
|
5
6
|
import { Command } from 'commander'
|
|
6
|
-
import
|
|
7
|
+
import { execa } from 'execa'
|
|
7
8
|
import FormData from 'form-data'
|
|
9
|
+
import inquirer from 'inquirer'
|
|
8
10
|
import fetch from 'node-fetch'
|
|
9
|
-
import url from 'url'
|
|
10
|
-
import yaml from 'yaml'
|
|
11
11
|
import pLimit from 'p-limit'
|
|
12
|
+
import yaml from 'yaml'
|
|
12
13
|
|
|
13
14
|
const program = new Command()
|
|
14
15
|
|
|
15
16
|
program
|
|
16
17
|
.name('pack-util')
|
|
17
18
|
.description('Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.')
|
|
18
|
-
.version('3.1
|
|
19
|
-
|
|
19
|
+
.version('3.2.1')
|
|
20
20
|
|
|
21
21
|
const limit = pLimit(10)
|
|
22
|
-
|
|
22
|
+
async function getPackageInfos(packageManager) {
|
|
23
23
|
// 使用 yarn
|
|
24
24
|
if (packageManager === 'yarn') {
|
|
25
25
|
const fileData = readFileSync('./yarn.lock')
|
|
26
|
-
return fileData.toString().split(/\r?\n/)
|
|
27
|
-
.filter(line => line.startsWith(' resolved'))
|
|
28
|
-
.map(line => line.substring(12, line.length - 1))
|
|
26
|
+
return fileData.toString().split(/\r?\n/).filter(line => line.startsWith(' resolved')).map(line => line.substring(12, line.length - 1))
|
|
29
27
|
}
|
|
28
|
+
// 使用 pnpm
|
|
30
29
|
else if (packageManager === 'pnpm') {
|
|
31
30
|
const fileData = readFileSync('./pnpm-lock.yaml', 'utf8')
|
|
32
31
|
const pnpmLock = yaml.parse(fileData)
|
|
33
|
-
// 直接使用 `packages`
|
|
34
|
-
const packages = Object.keys(pnpmLock.packages)
|
|
35
|
-
|
|
32
|
+
// 直接使用 `packages` 下的键作为包名和版本,同时处理其下的 `peerDependencies`
|
|
33
|
+
const packages = Object.keys(pnpmLock.packages).reduce((acc, key) => {
|
|
34
|
+
const pkg = pnpmLock.packages[key]
|
|
35
|
+
// 将当前包加入列表
|
|
36
|
+
acc.add(key)
|
|
37
|
+
if (pkg.peerDependencies) {
|
|
38
|
+
Object.keys(pkg.peerDependencies).forEach((peer) => {
|
|
39
|
+
let peerVersion = pkg.peerDependencies[peer]
|
|
40
|
+
if (peerVersion.includes('>='))
|
|
41
|
+
peerVersion = '*'
|
|
42
|
+
// 只匹配版本号
|
|
43
|
+
const versionRegex = /(\d+\.\d+\.\d+)/
|
|
44
|
+
const versionMatch = peerVersion.match(versionRegex)
|
|
45
|
+
if (versionMatch && versionMatch[1])
|
|
46
|
+
acc.add(`${peer}@${versionMatch[1]}`)
|
|
47
|
+
else
|
|
48
|
+
acc.add(`${peer}@*`)
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
return acc
|
|
52
|
+
}, new Set())
|
|
53
|
+
return Array.from(packages) // 返回去重后的包
|
|
36
54
|
}
|
|
37
55
|
// 使用 npm
|
|
38
56
|
else {
|
|
39
57
|
const { stdout } = await execa('npm', ['list', '--all'])
|
|
40
58
|
let packages = stdout.split('\n')
|
|
41
|
-
packages.splice(0, 1)
|
|
59
|
+
packages.splice(0, 1) // 去掉首行路径字符串
|
|
42
60
|
packages = packages.filter(item => !item.includes('deduped')) // 去掉标记重复的包
|
|
43
|
-
.map(name => {
|
|
61
|
+
.map((name) => {
|
|
44
62
|
const nameRegex = /(\S+@.+)/g
|
|
45
63
|
const nameParse = name.match(nameRegex)
|
|
46
|
-
if (nameParse && nameParse.length > 0)
|
|
64
|
+
if (nameParse && nameParse.length > 0)
|
|
65
|
+
return nameParse[0]
|
|
47
66
|
else return ''
|
|
48
67
|
}) // 解析每行的包名称
|
|
49
68
|
.filter(packageName => packageName.length > 0) // 去掉空行
|
|
@@ -51,35 +70,42 @@ const getPackageInfos = async (packageManager) => {
|
|
|
51
70
|
}
|
|
52
71
|
}
|
|
53
72
|
|
|
54
|
-
|
|
55
|
-
var index = 0
|
|
73
|
+
let index = 0
|
|
56
74
|
// 利用 npm pack 在外网上对 dependencies 中的所有包打包。
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
75
|
+
async function packPackage(packageName) {
|
|
76
|
+
try {
|
|
77
|
+
const { stdout } = await execa('npm', ['pack', packageName, '--pack-destination=./node_modules_pack'])
|
|
78
|
+
index++
|
|
79
|
+
// console.log(`${index}: ${stdout}`)
|
|
80
|
+
process.stdout.clearLine()
|
|
81
|
+
process.stdout.cursorTo(0)
|
|
82
|
+
process.stdout.write(`${index}: ${stdout}`) // 在同一行打印处理进度
|
|
83
|
+
return stdout
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
console.error(`\nWARN: Failed to pack package ${packageName}: ${error.message}`)
|
|
87
|
+
return packageName
|
|
88
|
+
}
|
|
89
|
+
}
|
|
66
90
|
|
|
67
91
|
// 直接利用依赖包的下载地址进行下载
|
|
68
|
-
|
|
69
|
-
const urlObj = url.
|
|
92
|
+
async function downloadPackage(packageUrl) {
|
|
93
|
+
const urlObj = new url.URL(packageUrl)
|
|
70
94
|
const filename = urlObj.pathname.substring(urlObj.pathname.lastIndexOf('/') + 1)
|
|
71
|
-
const fileStream = createWriteStream(
|
|
95
|
+
const fileStream = createWriteStream(`./node_modules_pack/${filename}`)
|
|
72
96
|
let res
|
|
73
97
|
try {
|
|
74
98
|
res = await fetch(packageUrl)
|
|
75
|
-
}
|
|
76
|
-
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
console.error(`\nWARN: Failed to download package ${packageUrl}: ${error.message}`)
|
|
77
102
|
return filename
|
|
78
103
|
}
|
|
79
104
|
if (!res?.ok) {
|
|
80
|
-
console.error(
|
|
105
|
+
console.error(`\nWARN: Failed to download package ${filename}: ${res.status} ${res.statusText}`)
|
|
81
106
|
return filename
|
|
82
|
-
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
83
109
|
res.body.pipe(fileStream)
|
|
84
110
|
await new Promise((resolve, reject) => {
|
|
85
111
|
fileStream.on('finish', () => {
|
|
@@ -97,17 +123,19 @@ const downloadPackage = async (packageUrl) => {
|
|
|
97
123
|
}
|
|
98
124
|
|
|
99
125
|
// 通过 nexus 的 components upload API 上传 npm 包,有时候会报 statusCode 500,暂不知道原因,重新上传即可。
|
|
100
|
-
|
|
126
|
+
async function uploadPackage(packageName, options) {
|
|
101
127
|
const { host, repository, username, password } = options
|
|
102
128
|
const form = new FormData()
|
|
103
|
-
form.append('npm.asset', createReadStream(
|
|
129
|
+
form.append('npm.asset', createReadStream(`./node_modules_pack/${packageName}`))
|
|
104
130
|
form.submit({
|
|
105
131
|
hostname: new URL(host).hostname,
|
|
106
132
|
port: new URL(host).port,
|
|
107
133
|
path: `/service/rest/v1/components?repository=${repository}`,
|
|
108
|
-
auth: `${username}:${password}
|
|
109
|
-
},
|
|
110
|
-
if (err)
|
|
134
|
+
auth: `${username}:${password}`,
|
|
135
|
+
}, (err, res) => {
|
|
136
|
+
if (err) {
|
|
137
|
+
process.stdout.write(`\n${err.message}`)
|
|
138
|
+
}
|
|
111
139
|
else {
|
|
112
140
|
if (res.statusCode > 200 && res.statusCode < 300) {
|
|
113
141
|
// upload success
|
|
@@ -116,7 +144,9 @@ const uploadPackage = async (packageName, options) => {
|
|
|
116
144
|
process.stdout.cursorTo(0)
|
|
117
145
|
process.stdout.write(`${index}: ${packageName}`) // 在同一行打印处理进度
|
|
118
146
|
}
|
|
119
|
-
else
|
|
147
|
+
else {
|
|
148
|
+
process.stdout.write(`\n${packageName} : UPLOAD ERROR -- code ${res.statusCode} -- ${res.statusMessage}`)
|
|
149
|
+
} // 打印上传失败的包
|
|
120
150
|
}
|
|
121
151
|
})
|
|
122
152
|
}
|
|
@@ -131,37 +161,42 @@ program
|
|
|
131
161
|
name: 'packageManager',
|
|
132
162
|
message: 'Please select your package manager:',
|
|
133
163
|
choices: ['pnpm', 'yarn', 'npm'],
|
|
134
|
-
default: 'pnpm'
|
|
164
|
+
default: 'pnpm',
|
|
135
165
|
},
|
|
136
166
|
]
|
|
137
167
|
inquirer.prompt(questions).then((options) => {
|
|
138
168
|
getPackageInfos(options.packageManager).then(async (packages) => {
|
|
139
|
-
|
|
169
|
+
process.stdout.write(`Total ${packages.length} packages to be packed, please wait...\n`)
|
|
140
170
|
mkdirSync('./node_modules_pack', { recursive: true })
|
|
141
171
|
if (options.packageManager === 'yarn') {
|
|
142
|
-
const downloadTasks = packages.map(
|
|
143
|
-
limit(() => downloadPackage(packageUrl))
|
|
172
|
+
const downloadTasks = packages.map(packageUrl =>
|
|
173
|
+
limit(() => downloadPackage(packageUrl)),
|
|
144
174
|
)
|
|
145
175
|
await Promise.all(downloadTasks)
|
|
146
|
-
}
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
147
178
|
const { stdout } = await execa('npm', ['config', 'get', 'registry'])
|
|
148
|
-
const registryUrl = stdout.endsWith('/') ? stdout : `${stdout}
|
|
179
|
+
const registryUrl = stdout.endsWith('/') ? stdout : `${stdout}/`
|
|
149
180
|
const packTasks = packages.map((packageName) => {
|
|
150
181
|
const atIndex = packageName.lastIndexOf('@')
|
|
151
182
|
const fullname = packageName.substring(0, atIndex)
|
|
152
183
|
const name = fullname.includes('/') ? fullname.split('/')[1] : fullname
|
|
153
184
|
const version = packageName.substring(atIndex + 1)
|
|
154
|
-
|
|
155
|
-
|
|
185
|
+
if (version === '*') {
|
|
186
|
+
return limit(() => packPackage(fullname))
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
const packageUrl = `${registryUrl}${fullname}/-/${name}-${version}.tgz`
|
|
190
|
+
return limit(() => downloadPackage(packageUrl))
|
|
191
|
+
}
|
|
156
192
|
})
|
|
157
193
|
await Promise.all(packTasks)
|
|
158
194
|
}
|
|
159
|
-
|
|
195
|
+
process.stdout.write('\nAll packages are successfully packed in ./node_modules_pack!')
|
|
160
196
|
})
|
|
161
197
|
})
|
|
162
198
|
})
|
|
163
199
|
|
|
164
|
-
|
|
165
200
|
program
|
|
166
201
|
.command('upload')
|
|
167
202
|
.description('Upload all packed packages in ./node_modules_pack to Nexus self-hosted Registry')
|
|
@@ -170,29 +205,29 @@ program
|
|
|
170
205
|
{
|
|
171
206
|
type: 'input',
|
|
172
207
|
name: 'host',
|
|
173
|
-
message: 'Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): '
|
|
208
|
+
message: 'Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): ',
|
|
174
209
|
},
|
|
175
210
|
{
|
|
176
211
|
type: 'input',
|
|
177
212
|
name: 'repository',
|
|
178
213
|
message: 'Please input your Nexus npm repository (eg: localNpm): ',
|
|
179
|
-
default: 'localNpm'
|
|
214
|
+
default: 'localNpm',
|
|
180
215
|
},
|
|
181
216
|
{
|
|
182
217
|
type: 'input',
|
|
183
218
|
name: 'username',
|
|
184
219
|
message: 'login to Nexus by username: ',
|
|
185
|
-
default: 'admin'
|
|
220
|
+
default: 'admin',
|
|
186
221
|
},
|
|
187
222
|
{
|
|
188
223
|
type: 'password',
|
|
189
224
|
name: 'password',
|
|
190
|
-
message: 'password: '
|
|
225
|
+
message: 'password: ',
|
|
191
226
|
},
|
|
192
227
|
]
|
|
193
|
-
inquirer.prompt(questions).then(options => {
|
|
228
|
+
inquirer.prompt(questions).then((options) => {
|
|
194
229
|
const packages = readdirSync('./node_modules_pack')
|
|
195
|
-
|
|
230
|
+
process.stdout.write(`\nTotal ${packages.length} packages to be uploaded, please wait...`)
|
|
196
231
|
for (const packageName of packages) {
|
|
197
232
|
uploadPackage(packageName, options)
|
|
198
233
|
}
|
package/eslint.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fanwang/pack-util",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"pack-util": "./bin/index.js"
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"yaml": "^2.5.1"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
+
"@antfu/eslint-config": "^3.7.3",
|
|
41
42
|
"bumpp": "^9.6.1",
|
|
42
43
|
"czg": "^1.10.1",
|
|
43
44
|
"taze": "^0.17.2"
|