@fanwang/pack-util 3.2.3 → 3.2.4
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/README.en-US.md +125 -34
- package/README.md +112 -29
- package/bin/index.js +277 -88
- package/package.json +1 -1
- package/fanwang-pack-util-3.2.3.tgz +0 -0
package/README.en-US.md
CHANGED
|
@@ -1,73 +1,164 @@
|
|
|
1
1
|
# pack-util
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@fanwang/pack-util)
|
|
4
|
+
[](https://github.com/vampirefan/pack-util/blob/main/LICENSE)
|
|
5
|
+
[](https://nodejs.org)
|
|
4
6
|
|
|
5
|
-
**English** | [中文](./README.md)
|
|
7
|
+
**English** | [中文](./README.md)
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
A CLI tool that packs all npm dependencies from your project on an internet-connected machine, then batch-uploads them to a self-hosted Nexus npm registry for offline/air-gapped installation. Supports `pnpm` / `yarn` / `npm`.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Dependency resolution for `pnpm`, `yarn`, and `npm`
|
|
14
|
+
- Concurrent download/pack for fast operation
|
|
15
|
+
- Concurrent upload to Nexus with progress display
|
|
16
|
+
- Interactive CLI, easy to use
|
|
17
|
+
- Supports scoped packages (e.g., `@vue/xxx`)
|
|
18
|
+
|
|
19
|
+
## Workflow
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
Internet Machine Air-gapped LAN
|
|
23
|
+
┌──────────────┐ ┌──────────────────────┐
|
|
24
|
+
│ your_project │ │ Nexus npm Registry │
|
|
25
|
+
│ │ pack-util │ │
|
|
26
|
+
│ node_modules │ ──────────► │ ┌──────────────────┐ │
|
|
27
|
+
│ lockfile │ pack │ │ localNpm repo │ │
|
|
28
|
+
└──────────────┘ │ └──────────────────┘ │
|
|
29
|
+
│ └──────────┬───────────┘
|
|
30
|
+
▼ │
|
|
31
|
+
┌──────────────────┐ Copy to LAN │
|
|
32
|
+
│ node_modules_pack│ ──────────► upload ──►│
|
|
33
|
+
│ (*.tgz files) │ │
|
|
34
|
+
└──────────────────┘ ▼
|
|
35
|
+
┌──────────────────────┐
|
|
36
|
+
│ npm/yarn/pnpm install │
|
|
37
|
+
│ (pointing to registry)│
|
|
38
|
+
└──────────────────────┘
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Install
|
|
8
42
|
|
|
9
43
|
```sh
|
|
10
44
|
npm install -g @fanwang/pack-util
|
|
11
45
|
```
|
|
12
46
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
47
|
+
> Requires Node.js >= 16.17.0
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
### Prerequisites
|
|
52
|
+
|
|
53
|
+
If PowerShell blocks script execution, run as administrator:
|
|
54
|
+
|
|
55
|
+
```powershell
|
|
16
56
|
Set-ExecutionPolicy RemoteSigned
|
|
17
57
|
```
|
|
18
58
|
|
|
19
|
-
|
|
59
|
+
It's recommended to set a faster registry mirror for downloads:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
yarn config set registry https://registry.npmmirror.com
|
|
63
|
+
npm config set registry https://registry.npmmirror.com
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Step 1: Pack dependencies
|
|
67
|
+
|
|
68
|
+
Download all project dependencies to `./node_modules_pack`:
|
|
20
69
|
|
|
21
70
|
```sh
|
|
22
71
|
cd your_project
|
|
23
72
|
pack-util pack
|
|
24
|
-
#
|
|
73
|
+
# Select your package manager ( pnpm | yarn | npm )
|
|
25
74
|
```
|
|
26
|
-
**Explain:**
|
|
27
|
-
`pack-util` retrieves all project dependencies based on the package manager you are using:
|
|
28
|
-
- **pnpm**: Retrieves dependencies from the `pnpm-lock.yaml` file.
|
|
29
|
-
- **yarn**: Retrieves dependencies from the `yarn.lock` file.
|
|
30
|
-
- **npm**: Retrieves dependencies from the output of `npm list --all`.
|
|
31
75
|
|
|
32
|
-
|
|
76
|
+
**How it works:**
|
|
77
|
+
|
|
78
|
+
The tool reads dependency information based on the package manager:
|
|
79
|
+
|
|
80
|
+
| Package Manager | Dependency Source |
|
|
81
|
+
|----------------|------------------|
|
|
82
|
+
| pnpm | `pnpm-lock.yaml` |
|
|
83
|
+
| yarn | `yarn.lock` |
|
|
84
|
+
| npm | `npm list --all` output |
|
|
85
|
+
|
|
86
|
+
> **Tip:** If you just want to download specific packages, create an empty directory, add packages via `pnpm add` / `yarn add` / `npm install`, then run `pack-util pack`.
|
|
87
|
+
|
|
88
|
+
### Step 2: Upload to Nexus
|
|
89
|
+
|
|
90
|
+
1. Copy `./node_modules_pack` folder to the air-gapped machine, then run:
|
|
33
91
|
|
|
34
|
-
### Step 2. Upload all packed packages in ./node_modules_pack
|
|
35
|
-
1. Copy ./node_modules_pack along with your project.
|
|
36
92
|
```sh
|
|
37
|
-
cd your_project
|
|
38
|
-
pack-util upload
|
|
93
|
+
cd your_project
|
|
94
|
+
pack-util upload
|
|
39
95
|
```
|
|
40
|
-
**Explain:**
|
|
41
|
-
`pack-util` looks for the ./node_modules_pack folder in the command execution directory and then uploads the packages in the folder in batches. Therefore, you only need to copy the ./node_modules_pack folder to the command execution directory.
|
|
42
96
|
|
|
43
|
-
2.
|
|
97
|
+
2. Enter Nexus connection details when prompted:
|
|
98
|
+
|
|
44
99
|
```
|
|
45
100
|
? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
|
|
46
|
-
? Please input your Nexus npm repository (eg: localNpm):
|
|
47
|
-
? login to Nexus by username:
|
|
48
|
-
? password:
|
|
101
|
+
? Please input your Nexus npm repository (eg: localNpm): localNpm
|
|
102
|
+
? login to Nexus by username: admin
|
|
103
|
+
? password: [hidden]
|
|
49
104
|
```
|
|
50
105
|
|
|
51
|
-
3. If
|
|
106
|
+
3. If any uploads fail, simply re-run `pack-util upload` (already uploaded packages will be skipped by Nexus).
|
|
52
107
|
|
|
53
|
-
### Step 3
|
|
54
|
-
Remove `yarn.lock` / `package-lock.json` / `pnpm-lock.yaml` file.
|
|
108
|
+
### Step 3: Install in LAN
|
|
55
109
|
|
|
56
|
-
|
|
57
|
-
|
|
110
|
+
Remove old lockfiles (`yarn.lock` / `package-lock.json` / `pnpm-lock.yaml`), then:
|
|
111
|
+
|
|
112
|
+
**PNPM**
|
|
113
|
+
```sh
|
|
58
114
|
pnpm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
59
115
|
pnpm install
|
|
60
116
|
```
|
|
61
117
|
|
|
62
|
-
**YARN**
|
|
63
|
-
```
|
|
118
|
+
**YARN**
|
|
119
|
+
```sh
|
|
64
120
|
yarn config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
65
121
|
yarn install
|
|
66
122
|
```
|
|
67
123
|
|
|
68
|
-
**NPM**
|
|
69
|
-
```
|
|
124
|
+
**NPM**
|
|
125
|
+
```sh
|
|
70
126
|
npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
71
|
-
npm install --
|
|
127
|
+
npm install --legacy-peer-deps
|
|
72
128
|
```
|
|
73
129
|
|
|
130
|
+
> Note: Use `--legacy-peer-deps` with npm to avoid peer dependency resolution issues in private registry environments.
|
|
131
|
+
|
|
132
|
+
## FAQ
|
|
133
|
+
|
|
134
|
+
### Q: Can the packed files be used for other projects?
|
|
135
|
+
|
|
136
|
+
A: Yes. The `./node_modules_pack` folder is a standalone collection of `.tgz` packages. Once uploaded to Nexus, any project pointing to that registry can install them.
|
|
137
|
+
|
|
138
|
+
### Q: What if I get 500 errors during upload?
|
|
139
|
+
|
|
140
|
+
A: Nexus occasionally returns 500 errors. Simply retry `pack-util upload` — already uploaded packages won't be duplicated.
|
|
141
|
+
|
|
142
|
+
### Q: Which Nexus versions are supported?
|
|
143
|
+
|
|
144
|
+
A: Nexus Repository Manager 3.x with npm hosted repositories.
|
|
145
|
+
|
|
146
|
+
## Development
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
# Install dependencies
|
|
150
|
+
npm install
|
|
151
|
+
|
|
152
|
+
# Run test (executes pack command)
|
|
153
|
+
npm test
|
|
154
|
+
|
|
155
|
+
# Commit (conventional commits via czg)
|
|
156
|
+
npm run commit
|
|
157
|
+
|
|
158
|
+
# Release new version
|
|
159
|
+
npm run release
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
[MIT](./LICENSE) - Copyright (c) 2022 vampirefan
|
package/README.md
CHANGED
|
@@ -1,82 +1,165 @@
|
|
|
1
1
|
# pack-util
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@fanwang/pack-util)
|
|
4
|
+
[](https://github.com/vampirefan/pack-util/blob/main/LICENSE)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
|
|
3
7
|
**中文** | [English](./README.en-US.md)
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
一个 CLI 工具,用于在联网环境打包项目的所有 npm 依赖,并批量上传到内网 Nexus 私服,实现离线/内网环境的 npm 包安装。支持 `pnpm` / `yarn` / `npm` 三种包管理器。
|
|
10
|
+
|
|
11
|
+
## 特性
|
|
12
|
+
|
|
13
|
+
- 支持 `pnpm`、`yarn`、`npm` 三种包管理器的依赖解析
|
|
14
|
+
- 并发下载/打包,速度快
|
|
15
|
+
- 并发上传至 Nexus,带进度显示
|
|
16
|
+
- 交互式 CLI,使用简单
|
|
17
|
+
- 支持 scoped packages(如 `@vue/xxx`)
|
|
18
|
+
|
|
19
|
+
## 工作流程
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
联网环境 内网环境
|
|
23
|
+
┌──────────────┐ ┌──────────────────────┐
|
|
24
|
+
│ your_project │ │ Nexus npm 私服 │
|
|
25
|
+
│ │ pack-util │ │
|
|
26
|
+
│ node_modules │ ──────────► │ ┌──────────────────┐ │
|
|
27
|
+
│ lockfile │ pack │ │ localNpm repo │ │
|
|
28
|
+
└──────────────┘ │ └──────────────────┘ │
|
|
29
|
+
│ └──────────┬───────────┘
|
|
30
|
+
▼ │
|
|
31
|
+
┌──────────────────┐ 拷贝至内网 │
|
|
32
|
+
│ node_modules_pack│ ──────────► upload ──►│
|
|
33
|
+
│ (*.tgz files) │ │
|
|
34
|
+
└──────────────────┘ ▼
|
|
35
|
+
┌──────────────────────┐
|
|
36
|
+
│ npm/yarn/pnpm install │
|
|
37
|
+
│ (指向私服 registry) │
|
|
38
|
+
└──────────────────────┘
|
|
39
|
+
```
|
|
6
40
|
|
|
7
41
|
## 安装
|
|
8
42
|
|
|
9
|
-
全局安装即可。
|
|
10
43
|
```sh
|
|
11
44
|
npm install -g @fanwang/pack-util
|
|
12
45
|
```
|
|
13
46
|
|
|
47
|
+
> 要求 Node.js >= 16.17.0
|
|
48
|
+
|
|
14
49
|
## 使用
|
|
15
50
|
|
|
16
|
-
|
|
51
|
+
### 前置说明
|
|
17
52
|
|
|
18
|
-
|
|
53
|
+
如果使用 PowerShell 时提示禁止运行脚本,用管理员身份运行终端后执行:
|
|
19
54
|
|
|
20
|
-
```
|
|
55
|
+
```powershell
|
|
21
56
|
Set-ExecutionPolicy RemoteSigned
|
|
22
57
|
```
|
|
23
58
|
|
|
24
|
-
|
|
59
|
+
建议将 npm 源设为国内镜像以加速下载:
|
|
60
|
+
|
|
25
61
|
```sh
|
|
62
|
+
# yarn 和 npm 需要分别设置
|
|
26
63
|
yarn config set registry https://registry.npmmirror.com
|
|
27
64
|
npm config set registry https://registry.npmmirror.com
|
|
28
65
|
```
|
|
29
66
|
|
|
30
|
-
###
|
|
67
|
+
### 第一步:打包依赖(pack)
|
|
68
|
+
|
|
69
|
+
将项目所有依赖包下载至 `./node_modules_pack` 文件夹:
|
|
70
|
+
|
|
31
71
|
```sh
|
|
32
72
|
cd your_project
|
|
33
73
|
pack-util pack
|
|
34
|
-
#
|
|
74
|
+
# 交互式选择包管理器( pnpm | yarn | npm )
|
|
35
75
|
```
|
|
36
|
-
**解释:**
|
|
37
|
-
工具是根据你使用的包管理工具来检索项目的所有依赖包。
|
|
38
|
-
- pnpm: 根据 `pnpm-lock.yaml` 文件来检索
|
|
39
|
-
- yarn: 根据 `yarn.lock` 文件来检索
|
|
40
|
-
- npm: 根据 `npm list --all` 的输出来检索
|
|
41
76
|
|
|
42
|
-
|
|
77
|
+
**原理说明:**
|
|
78
|
+
|
|
79
|
+
工具根据包管理器类型读取不同来源的依赖信息:
|
|
80
|
+
|
|
81
|
+
| 包管理器 | 依赖来源 |
|
|
82
|
+
|---------|---------|
|
|
83
|
+
| pnpm | `pnpm-lock.yaml` |
|
|
84
|
+
| yarn | `yarn.lock` |
|
|
85
|
+
| npm | `npm list --all` 输出 |
|
|
86
|
+
|
|
87
|
+
> **提示:** 如果只想下载特定包,可以新建空目录,用 `pnpm add` / `yarn add` / `npm install` 添加需要的包后直接执行 `pack-util pack`。
|
|
88
|
+
|
|
89
|
+
### 第二步:上传至 Nexus 私服(upload)
|
|
90
|
+
|
|
91
|
+
1. 将 `./node_modules_pack` 文件夹拷贝至内网机器,然后执行:
|
|
43
92
|
|
|
44
|
-
### 第二步. 将依赖包批量上传到 nexus 搭建的 npm 私服上
|
|
45
|
-
1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网机器上,然后执行上传。
|
|
46
93
|
```sh
|
|
47
94
|
cd your_project
|
|
48
95
|
pack-util upload
|
|
49
96
|
```
|
|
50
|
-
**解释:**
|
|
51
|
-
工具是在命令执行目录下查找 ./node_modules_pack 文件夹,然后把文件夹中的包批量上传,所以只需要把 ./node_modules_pack 文件夹拷贝到命令执行目录即可。
|
|
52
97
|
|
|
53
|
-
2.
|
|
98
|
+
2. 按提示输入 Nexus 连接信息:
|
|
99
|
+
|
|
54
100
|
```
|
|
55
101
|
? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
|
|
56
|
-
? Please input your Nexus npm repository (eg: localNpm):
|
|
57
|
-
? login to Nexus by username:
|
|
58
|
-
? password:
|
|
102
|
+
? Please input your Nexus npm repository (eg: localNpm): localNpm
|
|
103
|
+
? login to Nexus by username: admin
|
|
104
|
+
? password: [hidden]
|
|
59
105
|
```
|
|
60
106
|
|
|
61
|
-
3.
|
|
107
|
+
3. 如果出现上传失败的包,可以直接重新执行 `pack-util upload`(已成功的包会被 Nexus 自动跳过)。
|
|
108
|
+
|
|
109
|
+
### 第三步:内网安装依赖
|
|
62
110
|
|
|
63
|
-
|
|
64
|
-
先删除 `yarn.lock` / `package-lock.json` / `pnpm-lock.yaml` 文件。
|
|
111
|
+
先删除旧的锁文件(`yarn.lock` / `package-lock.json` / `pnpm-lock.yaml`),然后:
|
|
65
112
|
|
|
66
113
|
**PNPM**
|
|
67
|
-
```
|
|
114
|
+
```sh
|
|
68
115
|
pnpm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
69
116
|
pnpm install
|
|
70
117
|
```
|
|
71
118
|
|
|
72
119
|
**YARN**
|
|
73
|
-
```
|
|
120
|
+
```sh
|
|
74
121
|
yarn config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
75
122
|
yarn install
|
|
76
123
|
```
|
|
77
124
|
|
|
78
125
|
**NPM**
|
|
79
|
-
```
|
|
126
|
+
```sh
|
|
80
127
|
npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
81
|
-
npm install --
|
|
128
|
+
npm install --legacy-peer-deps
|
|
82
129
|
```
|
|
130
|
+
|
|
131
|
+
> 注意:npm 在私服环境安装时建议添加 `--legacy-peer-deps` 参数以避免 peer dependency 解析问题。
|
|
132
|
+
|
|
133
|
+
## 常见问题
|
|
134
|
+
|
|
135
|
+
### Q: 打包后的文件可以直接用于其他项目吗?
|
|
136
|
+
|
|
137
|
+
A: 可以。`./node_modules_pack` 文件夹是独立的 `.tgz` 包集合,只要上传到 Nexus 后,任何指向该私服的项目都可以安装这些包。
|
|
138
|
+
|
|
139
|
+
### Q: 上传时出现 500 错误怎么办?
|
|
140
|
+
|
|
141
|
+
A: Nexus 偶尔会返回 500 错误,直接重试 `pack-util upload` 即可,已上传成功的包不会重复上传。
|
|
142
|
+
|
|
143
|
+
### Q: 支持哪些版本的 Nexus?
|
|
144
|
+
|
|
145
|
+
A: 支持 Nexus Repository Manager 3.x 的 npm hosted 仓库。
|
|
146
|
+
|
|
147
|
+
## 开发
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
# 安装依赖
|
|
151
|
+
npm install
|
|
152
|
+
|
|
153
|
+
# 运行测试(执行 pack 命令)
|
|
154
|
+
npm test
|
|
155
|
+
|
|
156
|
+
# 提交代码(使用 conventional commits)
|
|
157
|
+
npm run commit
|
|
158
|
+
|
|
159
|
+
# 发布新版本
|
|
160
|
+
npm run release
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
[MIT](./LICENSE) - Copyright (c) 2022 vampirefan
|
package/bin/index.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* pack-util - CLI tool for packing npm dependencies and uploading to Nexus registry
|
|
5
|
+
* pack-util - 用于打包 npm 依赖并上传到 Nexus 私服的 CLI 工具
|
|
6
|
+
*
|
|
7
|
+
* Workflow / 工作流程:
|
|
8
|
+
* 1. pack - Read lockfile/dependency tree, download all packages to ./node_modules_pack
|
|
9
|
+
* 读取锁文件/依赖树,将所有包下载到 ./node_modules_pack
|
|
10
|
+
* 2. upload - Batch upload packages from ./node_modules_pack to Nexus repository
|
|
11
|
+
* 将 ./node_modules_pack 中的包批量上传到 Nexus 仓库
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { Buffer } from 'node:buffer'
|
|
15
|
+
import { createReadStream, createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync } from 'node:fs'
|
|
4
16
|
import process from 'node:process'
|
|
5
17
|
import url from 'node:url'
|
|
6
18
|
import { Command } from 'commander'
|
|
@@ -16,30 +28,51 @@ const program = new Command()
|
|
|
16
28
|
program
|
|
17
29
|
.name('pack-util')
|
|
18
30
|
.description('Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.')
|
|
19
|
-
.version('3.2.
|
|
31
|
+
.version('3.2.4')
|
|
32
|
+
|
|
33
|
+
/** 并发限制数 / Concurrency limit for parallel tasks */
|
|
34
|
+
const CONCURRENCY = 10
|
|
35
|
+
const limit = pLimit(CONCURRENCY)
|
|
20
36
|
|
|
21
|
-
|
|
37
|
+
/**
|
|
38
|
+
* 根据包管理器类型获取所有依赖包信息
|
|
39
|
+
* Retrieve all dependency package info based on the package manager type
|
|
40
|
+
*
|
|
41
|
+
* @param {'pnpm' | 'yarn' | 'npm'} packageManager - 包管理器类型 / package manager type
|
|
42
|
+
* @returns {Promise<string[]>} 包信息列表 (yarn 返回下载地址, pnpm/npm 返回 name@version)
|
|
43
|
+
* Package info list (yarn returns download URLs, pnpm/npm returns name@version)
|
|
44
|
+
*/
|
|
22
45
|
async function getPackageInfos(packageManager) {
|
|
23
|
-
// 使用 yarn
|
|
24
46
|
if (packageManager === 'yarn') {
|
|
25
|
-
|
|
47
|
+
// yarn: 从 yarn.lock 中提取 resolved 字段(即包的下载地址)
|
|
48
|
+
// yarn: Extract resolved URLs from yarn.lock
|
|
49
|
+
const lockfilePath = './yarn.lock'
|
|
50
|
+
if (!existsSync(lockfilePath)) {
|
|
51
|
+
throw new Error('yarn.lock not found. Please run "yarn install" first. / 未找到 yarn.lock,请先运行 "yarn install"')
|
|
52
|
+
}
|
|
53
|
+
const fileData = readFileSync(lockfilePath)
|
|
26
54
|
return fileData.toString().split(/\r?\n/).filter(line => line.startsWith(' resolved')).map(line => line.substring(12, line.length - 1))
|
|
27
55
|
}
|
|
28
|
-
// 使用 pnpm
|
|
29
56
|
else if (packageManager === 'pnpm') {
|
|
30
|
-
|
|
57
|
+
// pnpm: 解析 pnpm-lock.yaml,提取所有包及其 peerDependencies
|
|
58
|
+
// pnpm: Parse pnpm-lock.yaml, extract all packages and their peerDependencies
|
|
59
|
+
const lockfilePath = './pnpm-lock.yaml'
|
|
60
|
+
if (!existsSync(lockfilePath)) {
|
|
61
|
+
throw new Error('pnpm-lock.yaml not found. Please run "pnpm install" first. / 未找到 pnpm-lock.yaml,请先运行 "pnpm install"')
|
|
62
|
+
}
|
|
63
|
+
const fileData = readFileSync(lockfilePath, 'utf8')
|
|
31
64
|
const pnpmLock = yaml.parse(fileData)
|
|
32
|
-
//
|
|
65
|
+
// 遍历 packages 对象,收集所有包及其 peerDependencies
|
|
66
|
+
// Iterate packages object, collect all packages and their peerDependencies
|
|
33
67
|
const packages = Object.keys(pnpmLock.packages).reduce((acc, key) => {
|
|
34
68
|
const pkg = pnpmLock.packages[key]
|
|
35
|
-
// 将当前包加入列表
|
|
36
69
|
acc.add(key)
|
|
37
70
|
if (pkg.peerDependencies) {
|
|
38
71
|
Object.keys(pkg.peerDependencies).forEach((peer) => {
|
|
39
72
|
let peerVersion = pkg.peerDependencies[peer]
|
|
73
|
+
// 处理范围版本号,使用通配符 / Handle range versions with wildcard
|
|
40
74
|
if (peerVersion.includes('>='))
|
|
41
75
|
peerVersion = '*'
|
|
42
|
-
// 只匹配版本号
|
|
43
76
|
const versionRegex = /(\d+\.\d+\.\d+)/
|
|
44
77
|
const versionMatch = peerVersion.match(versionRegex)
|
|
45
78
|
if (versionMatch && versionMatch[1])
|
|
@@ -50,36 +83,43 @@ async function getPackageInfos(packageManager) {
|
|
|
50
83
|
}
|
|
51
84
|
return acc
|
|
52
85
|
}, new Set())
|
|
53
|
-
return Array.from(packages)
|
|
86
|
+
return Array.from(packages)
|
|
54
87
|
}
|
|
55
|
-
// 使用 npm
|
|
56
88
|
else {
|
|
89
|
+
// npm: 通过 npm list --all 获取完整依赖树
|
|
90
|
+
// npm: Get full dependency tree via npm list --all
|
|
57
91
|
const { stdout } = await execa('npm', ['list', '--all'])
|
|
58
92
|
let packages = stdout.split('\n')
|
|
59
|
-
packages.splice(0, 1) //
|
|
60
|
-
packages = packages
|
|
93
|
+
packages.splice(0, 1) // 去掉首行项目路径 / Remove first line (project path)
|
|
94
|
+
packages = packages
|
|
95
|
+
.filter(item => !item.includes('deduped')) // 排除已去重的包 / Exclude deduped packages
|
|
61
96
|
.map((name) => {
|
|
62
97
|
const nameRegex = /(\S+@.+)/g
|
|
63
98
|
const nameParse = name.match(nameRegex)
|
|
64
99
|
if (nameParse && nameParse.length > 0)
|
|
65
100
|
return nameParse[0]
|
|
66
101
|
else return ''
|
|
67
|
-
})
|
|
68
|
-
.filter(packageName => packageName.length > 0) //
|
|
69
|
-
return Array.from(new Set(packages)) //
|
|
102
|
+
})
|
|
103
|
+
.filter(packageName => packageName.length > 0) // 过滤空行 / Filter empty lines
|
|
104
|
+
return Array.from(new Set(packages)) // 去重 / Deduplicate
|
|
70
105
|
}
|
|
71
106
|
}
|
|
72
107
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
108
|
+
/**
|
|
109
|
+
* 使用 npm pack 打包单个包(用于无法直接通过 URL 下载的情况)
|
|
110
|
+
* Pack a single package using npm pack (fallback for packages without direct download URL)
|
|
111
|
+
*
|
|
112
|
+
* @param {string} packageName - 包名 / Package name
|
|
113
|
+
* @param {{ index: number }} counter - 共享计数器对象 / Shared counter object
|
|
114
|
+
* @returns {Promise<string>} 打包后的文件名 / Packed filename
|
|
115
|
+
*/
|
|
116
|
+
async function packPackage(packageName, counter) {
|
|
76
117
|
try {
|
|
77
118
|
const { stdout } = await execa('npm', ['pack', packageName, '--pack-destination=./node_modules_pack'])
|
|
78
|
-
index++
|
|
79
|
-
// console.log(`${index}: ${stdout}`)
|
|
119
|
+
counter.index++
|
|
80
120
|
process.stdout.clearLine()
|
|
81
121
|
process.stdout.cursorTo(0)
|
|
82
|
-
process.stdout.write(`${index}: ${stdout}`)
|
|
122
|
+
process.stdout.write(`${counter.index}: ${stdout}`)
|
|
83
123
|
return stdout
|
|
84
124
|
}
|
|
85
125
|
catch (error) {
|
|
@@ -88,12 +128,20 @@ async function packPackage(packageName) {
|
|
|
88
128
|
}
|
|
89
129
|
}
|
|
90
130
|
|
|
91
|
-
|
|
92
|
-
|
|
131
|
+
/**
|
|
132
|
+
* 通过 URL 直接下载包的 .tgz 文件
|
|
133
|
+
* Download package .tgz file directly via URL
|
|
134
|
+
*
|
|
135
|
+
* @param {string} packageUrl - 包下载地址 / Package download URL
|
|
136
|
+
* @param {{ index: number }} counter - 共享计数器对象 / Shared counter object
|
|
137
|
+
* @returns {Promise<string>} 下载后的文件名 / Downloaded filename
|
|
138
|
+
*/
|
|
139
|
+
async function downloadPackage(packageUrl, counter) {
|
|
93
140
|
const urlObj = new url.URL(packageUrl)
|
|
94
141
|
let filename = urlObj.pathname.substring(urlObj.pathname.lastIndexOf('/') + 1)
|
|
95
142
|
|
|
96
|
-
//
|
|
143
|
+
// 提取 scoped package 的命名空间前缀,避免同名文件覆盖
|
|
144
|
+
// Extract scoped package namespace prefix to avoid filename conflicts
|
|
97
145
|
const namespaceMatch = urlObj.pathname.match(/@([^/]+)/)
|
|
98
146
|
if (namespaceMatch) {
|
|
99
147
|
filename = `${namespaceMatch[1]}-${filename}`
|
|
@@ -117,10 +165,10 @@ async function downloadPackage(packageUrl) {
|
|
|
117
165
|
await new Promise((resolve, reject) => {
|
|
118
166
|
fileStream.on('finish', () => {
|
|
119
167
|
fileStream.close()
|
|
120
|
-
index++
|
|
168
|
+
counter.index++
|
|
121
169
|
process.stdout.clearLine()
|
|
122
170
|
process.stdout.cursorTo(0)
|
|
123
|
-
process.stdout.write(`${index}: ${filename}`)
|
|
171
|
+
process.stdout.write(`${counter.index}: ${filename}`)
|
|
124
172
|
resolve(filename)
|
|
125
173
|
})
|
|
126
174
|
fileStream.on('error', reject)
|
|
@@ -129,39 +177,65 @@ async function downloadPackage(packageUrl) {
|
|
|
129
177
|
}
|
|
130
178
|
}
|
|
131
179
|
|
|
132
|
-
|
|
133
|
-
|
|
180
|
+
/**
|
|
181
|
+
* 通过 Nexus Components API 上传单个 npm 包
|
|
182
|
+
* Upload a single npm package via Nexus Components API
|
|
183
|
+
*
|
|
184
|
+
* NOTE: Nexus 偶尔会返回 500 错误,重试即可
|
|
185
|
+
* NOTE: Nexus may occasionally return 500 errors, retry should resolve it
|
|
186
|
+
*
|
|
187
|
+
* @param {string} packageName - .tgz 文件名 / .tgz filename
|
|
188
|
+
* @param {object} options - 上传配置 / Upload configuration
|
|
189
|
+
* @param {string} options.host - Nexus 服务地址 / Nexus server URL
|
|
190
|
+
* @param {string} options.repository - npm 仓库名 / npm repository name
|
|
191
|
+
* @param {string} options.username - 用户名 / Username
|
|
192
|
+
* @param {string} options.password - 密码 / Password
|
|
193
|
+
* @param {{ index: number }} counter - 共享计数器对象 / Shared counter object
|
|
194
|
+
* @returns {Promise<string>} 上传结果 / Upload result
|
|
195
|
+
*/
|
|
196
|
+
function uploadPackage(packageName, options, counter) {
|
|
134
197
|
const { host, repository, username, password } = options
|
|
198
|
+
const hostUrl = new URL(host)
|
|
199
|
+
// 拼接 URL 路径前缀(兼容 Nexus 部署在子路径下的情况,如 /nexus/)
|
|
200
|
+
// Construct path with URL prefix (compatible with Nexus deployed under a sub-path, e.g. /nexus/)
|
|
201
|
+
const basePath = hostUrl.pathname.replace(/\/$/, '')
|
|
202
|
+
const uploadUrl = `${hostUrl.protocol}//${hostUrl.host}${basePath}/service/rest/v1/components?repository=${repository}`
|
|
203
|
+
const auth = Buffer.from(`${username}:${password}`).toString('base64')
|
|
204
|
+
|
|
135
205
|
const form = new FormData()
|
|
136
206
|
form.append('npm.asset', createReadStream(`./node_modules_pack/${packageName}`))
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
207
|
+
|
|
208
|
+
return fetch(uploadUrl, {
|
|
209
|
+
method: 'POST',
|
|
210
|
+
headers: {
|
|
211
|
+
...form.getHeaders(),
|
|
212
|
+
Authorization: `Basic ${auth}`,
|
|
213
|
+
},
|
|
214
|
+
body: form,
|
|
215
|
+
}).then((res) => {
|
|
216
|
+
if (res.ok) {
|
|
217
|
+
// 上传成功 / Upload success
|
|
218
|
+
counter.index++
|
|
219
|
+
process.stdout.clearLine()
|
|
220
|
+
process.stdout.cursorTo(0)
|
|
221
|
+
process.stdout.write(`${counter.index}: ${packageName}`)
|
|
145
222
|
}
|
|
146
223
|
else {
|
|
147
|
-
|
|
148
|
-
// upload success
|
|
149
|
-
index++
|
|
150
|
-
process.stdout.clearLine()
|
|
151
|
-
process.stdout.cursorTo(0)
|
|
152
|
-
process.stdout.write(`${index}: ${packageName}`) // 在同一行打印处理进度
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
process.stdout.write(`\n${packageName} : UPLOAD ERROR -- code ${res.statusCode} -- ${res.statusMessage}`)
|
|
156
|
-
} // 打印上传失败的包
|
|
224
|
+
process.stdout.write(`\n${packageName} : UPLOAD ERROR -- code ${res.status} -- ${res.statusText}`)
|
|
157
225
|
}
|
|
226
|
+
return packageName
|
|
227
|
+
}).catch((err) => {
|
|
228
|
+
process.stdout.write(`\n${packageName} : ${err.message}`)
|
|
229
|
+
return packageName
|
|
158
230
|
})
|
|
159
231
|
}
|
|
160
232
|
|
|
233
|
+
// ======================== CLI Commands / CLI 命令 ========================
|
|
234
|
+
|
|
161
235
|
program
|
|
162
236
|
.command('pack')
|
|
163
237
|
.description('Pack all installed package in ./node_modules')
|
|
164
|
-
.action(() => {
|
|
238
|
+
.action(async () => {
|
|
165
239
|
const questions = [
|
|
166
240
|
{
|
|
167
241
|
type: 'list',
|
|
@@ -171,54 +245,88 @@ program
|
|
|
171
245
|
default: 'pnpm',
|
|
172
246
|
},
|
|
173
247
|
]
|
|
174
|
-
inquirer.prompt(questions)
|
|
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
|
-
|
|
248
|
+
const options = await inquirer.prompt(questions)
|
|
249
|
+
try {
|
|
250
|
+
const packages = await getPackageInfos(options.packageManager)
|
|
251
|
+
process.stdout.write(`Total ${packages.length} packages to be packed, please wait...\n`)
|
|
252
|
+
mkdirSync('./node_modules_pack', { recursive: true })
|
|
253
|
+
|
|
254
|
+
// 共享计数器用于跟踪进度 / Shared counter for progress tracking
|
|
255
|
+
const counter = { index: 0 }
|
|
256
|
+
|
|
257
|
+
if (options.packageManager === 'yarn') {
|
|
258
|
+
// yarn 使用 resolved URL 直接下载 / yarn uses resolved URLs for direct download
|
|
259
|
+
const downloadTasks = packages.map(packageUrl =>
|
|
260
|
+
limit(() => downloadPackage(packageUrl, counter)),
|
|
261
|
+
)
|
|
262
|
+
await Promise.all(downloadTasks)
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
// pnpm/npm 先获取 registry URL,然后拼接下载地址
|
|
266
|
+
// pnpm/npm: get registry URL first, then construct download URLs
|
|
267
|
+
const { stdout } = await execa('npm', ['config', 'get', 'registry'])
|
|
268
|
+
const registryUrl = stdout.endsWith('/') ? stdout : `${stdout}/`
|
|
269
|
+
const packTasks = packages.map((packageName) => {
|
|
270
|
+
const atIndex = packageName.lastIndexOf('@')
|
|
271
|
+
const fullname = packageName.substring(0, atIndex)
|
|
272
|
+
const name = fullname.includes('/') ? fullname.split('/')[1] : fullname
|
|
273
|
+
const version = packageName.substring(atIndex + 1)
|
|
274
|
+
if (version === '*') {
|
|
275
|
+
// 通配符版本使用 npm pack 回退 / Wildcard version uses npm pack fallback
|
|
276
|
+
return limit(() => packPackage(fullname, counter))
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
const packageUrl = `${registryUrl}${fullname}/-/${name}-${version}.tgz`
|
|
280
|
+
return limit(() => downloadPackage(packageUrl, counter))
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
await Promise.all(packTasks)
|
|
284
|
+
}
|
|
285
|
+
process.stdout.write('\nAll packages are successfully packed in ./node_modules_pack!\n')
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
console.error(`\nError: ${error.message}`)
|
|
289
|
+
process.exit(1)
|
|
290
|
+
}
|
|
205
291
|
})
|
|
206
292
|
|
|
207
293
|
program
|
|
208
294
|
.command('upload')
|
|
209
295
|
.description('Upload all packed packages in ./node_modules_pack to Nexus self-hosted Registry')
|
|
210
|
-
.action(() => {
|
|
296
|
+
.action(async () => {
|
|
297
|
+
// 检查 node_modules_pack 目录是否存在
|
|
298
|
+
// Check if node_modules_pack directory exists
|
|
299
|
+
if (!existsSync('./node_modules_pack')) {
|
|
300
|
+
console.error('Error: ./node_modules_pack directory not found. Please run "pack-util pack" first.')
|
|
301
|
+
console.error('错误: 未找到 ./node_modules_pack 目录,请先运行 "pack-util pack"')
|
|
302
|
+
process.exit(1)
|
|
303
|
+
}
|
|
304
|
+
|
|
211
305
|
const questions = [
|
|
212
306
|
{
|
|
213
307
|
type: 'input',
|
|
214
308
|
name: 'host',
|
|
215
309
|
message: 'Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): ',
|
|
310
|
+
validate: (input) => {
|
|
311
|
+
if (!input)
|
|
312
|
+
return 'Nexus URL is required / Nexus URL 不能为空'
|
|
313
|
+
try {
|
|
314
|
+
const parsed = new URL(input)
|
|
315
|
+
if (!parsed.hostname)
|
|
316
|
+
return 'Invalid URL format / URL 格式不正确'
|
|
317
|
+
return true
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
return 'Invalid URL format (eg: http://xx.xx.xx.xx:8081) / URL 格式不正确'
|
|
321
|
+
}
|
|
322
|
+
},
|
|
216
323
|
},
|
|
217
324
|
{
|
|
218
325
|
type: 'input',
|
|
219
326
|
name: 'repository',
|
|
220
327
|
message: 'Please input your Nexus npm repository (eg: localNpm): ',
|
|
221
328
|
default: 'localNpm',
|
|
329
|
+
validate: input => input ? true : 'Repository name is required / 仓库名不能为空',
|
|
222
330
|
},
|
|
223
331
|
{
|
|
224
332
|
type: 'input',
|
|
@@ -230,15 +338,96 @@ program
|
|
|
230
338
|
type: 'password',
|
|
231
339
|
name: 'password',
|
|
232
340
|
message: 'password: ',
|
|
341
|
+
validate: input => input ? true : 'Password is required / 密码不能为空',
|
|
233
342
|
},
|
|
234
343
|
]
|
|
235
|
-
inquirer.prompt(questions)
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
344
|
+
const options = await inquirer.prompt(questions)
|
|
345
|
+
|
|
346
|
+
const packages = readdirSync('./node_modules_pack')
|
|
347
|
+
if (packages.length === 0) {
|
|
348
|
+
console.error('Error: ./node_modules_pack is empty. Nothing to upload.')
|
|
349
|
+
console.error('错误: ./node_modules_pack 为空,没有可上传的包')
|
|
350
|
+
process.exit(1)
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
process.stdout.write(`\nTotal ${packages.length} packages to be uploaded, please wait...\n`)
|
|
354
|
+
|
|
355
|
+
// 使用并发控制批量上传 / Upload with concurrency control
|
|
356
|
+
const counter = { index: 0 }
|
|
357
|
+
const uploadTasks = packages.map(packageName =>
|
|
358
|
+
limit(() => uploadPackage(packageName, options, counter)),
|
|
359
|
+
)
|
|
360
|
+
await Promise.all(uploadTasks)
|
|
361
|
+
process.stdout.write(`\nUpload completed! ${counter.index}/${packages.length} packages uploaded successfully.\n`)
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
// ======================== Hidden Commands / 隐藏命令 ========================
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 内网快捷上传命令(隐藏)- 预置 Nexus 地址和凭据,选择后直接上传
|
|
368
|
+
* Internal quick upload command (hidden) - preset Nexus hosts and credentials, upload directly after selection
|
|
369
|
+
*/
|
|
370
|
+
const NEXUS_PRESETS = [
|
|
371
|
+
{
|
|
372
|
+
name: 'http://10.102.81.50/nexus/',
|
|
373
|
+
host: 'http://10.102.81.50/nexus/',
|
|
374
|
+
repository: 'npm',
|
|
375
|
+
username: 'admin',
|
|
376
|
+
password: 'Nexus@2023.',
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
name: 'http://10.102.2.119:8081/',
|
|
380
|
+
host: 'http://10.102.2.119:8081/',
|
|
381
|
+
repository: 'localNpm',
|
|
382
|
+
username: 'admin',
|
|
383
|
+
password: 'admin1234',
|
|
384
|
+
},
|
|
385
|
+
]
|
|
386
|
+
|
|
387
|
+
const uploadToNexusCmd = new Command('upload-to-nexus')
|
|
388
|
+
.description('Quick upload to preset Nexus registry (internal use)')
|
|
389
|
+
.action(async () => {
|
|
390
|
+
if (!existsSync('./node_modules_pack')) {
|
|
391
|
+
console.error('Error: ./node_modules_pack directory not found. Please run "pack-util pack" first.')
|
|
392
|
+
process.exit(1)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const { host } = await inquirer.prompt([
|
|
396
|
+
{
|
|
397
|
+
type: 'list',
|
|
398
|
+
name: 'host',
|
|
399
|
+
message: 'Please select upload Nexus url host:',
|
|
400
|
+
choices: NEXUS_PRESETS.map(p => p.name),
|
|
401
|
+
default: NEXUS_PRESETS[0].name,
|
|
402
|
+
},
|
|
403
|
+
])
|
|
404
|
+
|
|
405
|
+
// 根据选择的 host 匹配预置配置 / Match preset config by selected host
|
|
406
|
+
const preset = NEXUS_PRESETS.find(p => p.name === host)
|
|
407
|
+
const options = {
|
|
408
|
+
host: preset.host,
|
|
409
|
+
repository: preset.repository,
|
|
410
|
+
username: preset.username,
|
|
411
|
+
password: preset.password,
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const packages = readdirSync('./node_modules_pack')
|
|
415
|
+
if (packages.length === 0) {
|
|
416
|
+
console.error('Error: ./node_modules_pack is empty. Nothing to upload.')
|
|
417
|
+
process.exit(1)
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
process.stdout.write(`Uploading to ${options.host} (repo: ${options.repository})...\n`)
|
|
421
|
+
process.stdout.write(`Total ${packages.length} packages to be uploaded, please wait...\n`)
|
|
422
|
+
|
|
423
|
+
const counter = { index: 0 }
|
|
424
|
+
const uploadTasks = packages.map(packageName =>
|
|
425
|
+
limit(() => uploadPackage(packageName, options, counter)),
|
|
426
|
+
)
|
|
427
|
+
await Promise.all(uploadTasks)
|
|
428
|
+
process.stdout.write(`\nUpload completed! ${counter.index}/${packages.length} packages uploaded successfully.\n`)
|
|
242
429
|
})
|
|
243
430
|
|
|
431
|
+
program.addCommand(uploadToNexusCmd, { hidden: true })
|
|
432
|
+
|
|
244
433
|
program.parse()
|
package/package.json
CHANGED
|
Binary file
|