@fanwang/pack-util 2.0.4 → 3.0.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 +8 -0
- package/README.en-US.md +18 -7
- package/README.md +18 -8
- package/bin/index.js +28 -28
- package/package.json +13 -8
package/README.en-US.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# pack-util
|
|
2
2
|
|
|
3
|
-
Pack all installed package in ./node_modules, and publish to Nexus hosted Registry. Support
|
|
3
|
+
Pack all installed package in ./node_modules, and publish to Nexus hosted Registry. Support `pnpm`/`yarn`/`npm`.
|
|
4
4
|
|
|
5
5
|
**English** | [中文](./README.md)
|
|
6
6
|
|
|
@@ -21,8 +21,15 @@ Set-ExecutionPolicy RemoteSigned
|
|
|
21
21
|
```sh
|
|
22
22
|
cd your_project
|
|
23
23
|
pack-util pack
|
|
24
|
-
# select your package manager ( yarn | npm )
|
|
24
|
+
# select your package manager ( pnpm | yarn | npm )
|
|
25
25
|
```
|
|
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
|
+
|
|
32
|
+
Therefore, if you simply want to download some packages from the internet, you can start with just a `package.json` file and use different package managers to execute `install`, which will generate the corresponding lock file. After that, you can perform the `pack` operation. You can even create an empty folder and use `yarn add` | `pnpm add` | `npm install` to add the packages you need, which will directly generate the lock file for retrieval.
|
|
26
33
|
|
|
27
34
|
### Step 2. Upload all packed packages in ./node_modules_pack
|
|
28
35
|
1. Copy ./node_modules_pack along with your project.
|
|
@@ -30,6 +37,8 @@ pack-util pack
|
|
|
30
37
|
cd your_project
|
|
31
38
|
pack-util upload
|
|
32
39
|
```
|
|
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.
|
|
33
42
|
|
|
34
43
|
2. Input some params
|
|
35
44
|
```
|
|
@@ -42,7 +51,13 @@ pack-util upload
|
|
|
42
51
|
3. If there is upload error, delete other packages within the ./node_modules_pack, and upload again.
|
|
43
52
|
|
|
44
53
|
### Step 3. Install packages in LAN
|
|
45
|
-
Remove yarn.lock
|
|
54
|
+
Remove `yarn.lock` / `package-lock.json` / `pnpm-lock.yaml` file.
|
|
55
|
+
|
|
56
|
+
**PNPM**
|
|
57
|
+
```
|
|
58
|
+
pnpm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
59
|
+
pnpm install
|
|
60
|
+
```
|
|
46
61
|
|
|
47
62
|
**YARN**
|
|
48
63
|
```
|
|
@@ -56,7 +71,3 @@ npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
|
56
71
|
npm install --save --legacy-peer-deps # NOTE:NEED TO USE LEGACY PEER DEPS WHEN INSTALLING IN LAN
|
|
57
72
|
```
|
|
58
73
|
|
|
59
|
-
## TODO
|
|
60
|
-
- [x] show package and upload progress
|
|
61
|
-
- [x] support yarn
|
|
62
|
-
- [ ] support pnpm
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**中文** | [English](./README.en-US.md)
|
|
4
4
|
|
|
5
|
-
一个打包工具,可以在互联网网机器上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,并批量上传到 nexus 搭建的 npm 私服上。支持 `yarn
|
|
5
|
+
一个打包工具,可以在互联网网机器上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,并批量上传到 nexus 搭建的 npm 私服上。支持 `pnpm`/`yarn`/`npm` 打包工具。
|
|
6
6
|
|
|
7
7
|
## 安装
|
|
8
8
|
|
|
@@ -27,12 +27,18 @@ yarn config set registry https://registry.npmmirror.com
|
|
|
27
27
|
npm config set registry https://registry.npmmirror.com
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
### 第一步.
|
|
30
|
+
### 第一步. 将项目所有依赖包下载至 ./node_modules_pack 文件夹
|
|
31
31
|
```sh
|
|
32
32
|
cd your_project
|
|
33
33
|
pack-util pack
|
|
34
|
-
# 选择所使用的包管理工具即可( yarn | npm )
|
|
34
|
+
# 选择所使用的包管理工具即可( pnpm | yarn | npm )
|
|
35
35
|
```
|
|
36
|
+
**解释:**
|
|
37
|
+
工具是根据你使用的包管理工具来检索项目的所有依赖包。
|
|
38
|
+
- pnpm: 根据 pnpm-lock.yaml 文件来检索
|
|
39
|
+
- yarn: 根据 yarn.lock 文件来检索
|
|
40
|
+
- npm: 根据 npm list --all 的输出来检索
|
|
41
|
+
因此,如果单纯的想从互联网下载一些包,可以仅凭一个 package.json 文件,利用不同的包管理工具执行 install 得到相应的检索文件后,就可以执行 pack 操作了。甚至建立一个空文件夹,然后通过 `yarn add` | `pnpm add` | `npm install` 添加自己需要的包来直接得到检索文件,也是可以的。
|
|
36
42
|
|
|
37
43
|
### 第二步. 将依赖包批量上传到 nexus 搭建的 npm 私服上
|
|
38
44
|
1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网机器上,然后执行上传。
|
|
@@ -40,6 +46,8 @@ pack-util pack
|
|
|
40
46
|
cd your_project
|
|
41
47
|
pack-util upload
|
|
42
48
|
```
|
|
49
|
+
**解释:**
|
|
50
|
+
工具是在命令执行目录下查找 ./node_modules_pack 文件夹,然后把文件夹中的包批量上传,所以只需要把 ./node_modules_pack 文件夹拷贝到命令执行目录即可。
|
|
43
51
|
|
|
44
52
|
2. 输入参数
|
|
45
53
|
```
|
|
@@ -52,7 +60,13 @@ pack-util upload
|
|
|
52
60
|
3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传(也可以直接重试上传)。
|
|
53
61
|
|
|
54
62
|
### 第三步. 在内网机器上执行包依赖安装
|
|
55
|
-
先删除 yarn.lock
|
|
63
|
+
先删除 `yarn.lock` / `package-lock.json` / `pnpm-lock.yaml` 文件。
|
|
64
|
+
|
|
65
|
+
**PNPM**
|
|
66
|
+
```
|
|
67
|
+
pnpm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
68
|
+
pnpm install
|
|
69
|
+
```
|
|
56
70
|
|
|
57
71
|
**YARN**
|
|
58
72
|
```
|
|
@@ -66,7 +80,3 @@ npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
|
|
|
66
80
|
npm install --save --legacy-peer-deps # 注意:在私网上安装 npm 包需要忽略对 peer 依赖
|
|
67
81
|
```
|
|
68
82
|
|
|
69
|
-
## TODO
|
|
70
|
-
- [x] 实现打包和上传显示进度
|
|
71
|
-
- [x] 支持 yarn
|
|
72
|
-
- [ ] 支持 pnpm
|
package/bin/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
|
|
2
3
|
import { execa } from 'execa'
|
|
3
4
|
import { mkdirSync, readdirSync, createReadStream, createWriteStream, readFileSync } from 'fs'
|
|
4
5
|
import { Command } from 'commander'
|
|
@@ -6,25 +7,38 @@ import inquirer from 'inquirer'
|
|
|
6
7
|
import FormData from 'form-data'
|
|
7
8
|
import fetch from 'node-fetch'
|
|
8
9
|
import url from 'url'
|
|
10
|
+
import yaml from 'yaml'
|
|
11
|
+
import pLimit from 'p-limit'
|
|
9
12
|
|
|
10
13
|
const program = new Command()
|
|
11
14
|
|
|
12
15
|
program
|
|
13
16
|
.name('pack-util')
|
|
14
17
|
.description('Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.')
|
|
15
|
-
.version('
|
|
18
|
+
.version('3.0.1')
|
|
19
|
+
|
|
16
20
|
|
|
21
|
+
const limit = pLimit(10)
|
|
17
22
|
const getPackageInfos = async (packageManager) => {
|
|
23
|
+
// 使用 yarn
|
|
18
24
|
if (packageManager === 'yarn') {
|
|
19
25
|
const fileData = readFileSync('./yarn.lock')
|
|
20
26
|
return fileData.toString().split(/\r?\n/)
|
|
21
27
|
.filter(line => line.startsWith(' resolved'))
|
|
22
28
|
.map(line => line.substring(12, line.length - 1))
|
|
23
29
|
}
|
|
30
|
+
else if (packageManager === 'pnpm') {
|
|
31
|
+
const fileData = readFileSync('./pnpm-lock.yaml', 'utf8')
|
|
32
|
+
const pnpmLock = yaml.parse(fileData)
|
|
33
|
+
// 直接使用 `packages` 下的键作为包名和版本
|
|
34
|
+
const packages = Object.keys(pnpmLock.packages)
|
|
35
|
+
return Array.from(new Set(packages)) // 去掉重复项
|
|
36
|
+
}
|
|
37
|
+
// 使用 npm
|
|
24
38
|
else {
|
|
25
39
|
const { stdout } = await execa('npm', ['list', '--all'])
|
|
26
40
|
let packages = stdout.split('\n')
|
|
27
|
-
packages.splice(0, 1)//去掉首行路径字符串
|
|
41
|
+
packages.splice(0, 1) //去掉首行路径字符串
|
|
28
42
|
packages = packages.filter(item => !item.includes('deduped')) // 去掉标记重复的包
|
|
29
43
|
.map(name => {
|
|
30
44
|
const nameRegex = /(\S+@.+)/g
|
|
@@ -80,13 +94,6 @@ const downloadPackage = async (packageUrl) => {
|
|
|
80
94
|
}
|
|
81
95
|
}
|
|
82
96
|
|
|
83
|
-
|
|
84
|
-
// npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。
|
|
85
|
-
const publishPackage = async (packageName) => {
|
|
86
|
-
const { stdout } = await execa('npm', ['publish', './node_modules_pack/' + packageName])
|
|
87
|
-
return stdout
|
|
88
|
-
}
|
|
89
|
-
|
|
90
97
|
// 通过 nexus 的 components upload API 上传 npm 包,有时候会报 statusCode 500,暂不知道原因,重新上传即可。
|
|
91
98
|
const uploadPackage = async (packageName, options) => {
|
|
92
99
|
const { host, repository, username, password } = options
|
|
@@ -121,23 +128,26 @@ program
|
|
|
121
128
|
type: 'list',
|
|
122
129
|
name: 'packageManager',
|
|
123
130
|
message: 'Please select your package manager:',
|
|
124
|
-
choices: ['yarn', 'npm'],
|
|
125
|
-
default: '
|
|
131
|
+
choices: ['pnpm', 'yarn', 'npm'],
|
|
132
|
+
default: 'pnpm'
|
|
126
133
|
},
|
|
127
134
|
]
|
|
128
|
-
inquirer.prompt(questions).then(options => {
|
|
135
|
+
inquirer.prompt(questions).then((options) => {
|
|
129
136
|
getPackageInfos(options.packageManager).then(async (packages) => {
|
|
130
137
|
console.log(`Total ${packages.length} packages to be packed, please wait...`)
|
|
131
138
|
mkdirSync('./node_modules_pack', { recursive: true })
|
|
132
139
|
if (options.packageManager === 'yarn') {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
140
|
+
const downloadTasks = packages.map((packageUrl) =>
|
|
141
|
+
limit(() => downloadPackage(packageUrl))
|
|
142
|
+
)
|
|
143
|
+
await Promise.all(downloadTasks)
|
|
136
144
|
} else {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
145
|
+
const packTasks = packages.map((packageName) =>
|
|
146
|
+
limit(() => packPackage(packageName))
|
|
147
|
+
)
|
|
148
|
+
await Promise.all(packTasks)
|
|
140
149
|
}
|
|
150
|
+
console.log('\nAll packages are successfully packed in ./node_modules_pack!')
|
|
141
151
|
})
|
|
142
152
|
})
|
|
143
153
|
})
|
|
@@ -180,14 +190,4 @@ program
|
|
|
180
190
|
})
|
|
181
191
|
})
|
|
182
192
|
|
|
183
|
-
// program
|
|
184
|
-
// .command('publish')
|
|
185
|
-
// .description('Publish all packed packages in ./node_modules_pack')
|
|
186
|
-
// .action(() => {
|
|
187
|
-
// const packages = readdirSync('./node_modules_pack')
|
|
188
|
-
// packages.forEach(packageName => {
|
|
189
|
-
// publishPackage(packageName).then(stdout => console.log(stdout))
|
|
190
|
-
// })
|
|
191
|
-
// })
|
|
192
|
-
|
|
193
193
|
program.parse()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fanwang/pack-util",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.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"
|
|
@@ -13,12 +13,13 @@
|
|
|
13
13
|
},
|
|
14
14
|
"type": "module",
|
|
15
15
|
"scripts": {
|
|
16
|
-
"test": "
|
|
16
|
+
"test": "node bin/index.js pack",
|
|
17
|
+
"commit": "git add . && czg && git push",
|
|
17
18
|
"release": "bumpp package.json bin/index.js"
|
|
18
19
|
},
|
|
19
20
|
"repository": {
|
|
20
21
|
"type": "git",
|
|
21
|
-
"url": "https://
|
|
22
|
+
"url": "https://github.com/vampirefan/pack-util.git"
|
|
22
23
|
},
|
|
23
24
|
"keywords": [
|
|
24
25
|
"npm",
|
|
@@ -28,13 +29,17 @@
|
|
|
28
29
|
"author": "fanwang",
|
|
29
30
|
"license": "MIT",
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"commander": "^9.
|
|
32
|
-
"execa": "^
|
|
32
|
+
"commander": "^9.5.0",
|
|
33
|
+
"execa": "^9.4.0",
|
|
33
34
|
"form-data": "^4.0.0",
|
|
34
|
-
"inquirer": "^9.
|
|
35
|
-
"node-fetch": "^3.3.
|
|
35
|
+
"inquirer": "^9.3.7",
|
|
36
|
+
"node-fetch": "^3.3.2",
|
|
37
|
+
"p-limit": "^6.1.0",
|
|
38
|
+
"yaml": "^2.5.1"
|
|
36
39
|
},
|
|
37
40
|
"devDependencies": {
|
|
38
|
-
"bumpp": "^9.
|
|
41
|
+
"bumpp": "^9.6.1",
|
|
42
|
+
"czg": "^1.10.1",
|
|
43
|
+
"taze": "^0.17.2"
|
|
39
44
|
}
|
|
40
45
|
}
|