@fanwang/pack-util 2.0.1 → 3.0.0

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.
@@ -0,0 +1,8 @@
1
+ {
2
+ "cSpell.words": [
3
+ "bumpp",
4
+ "deduped",
5
+ "execa",
6
+ "taze"
7
+ ]
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 both `yarn` and `npm`.
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,20 +37,27 @@ 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
-
36
- ```
37
- ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
38
- ? Please input your Nexus npm repository (eg: localNpm): localNpm
39
- ? login to Nexus by username: admin
40
- ? password: [hidden]
41
- ```
44
+ ```
45
+ ? 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): localNpm
47
+ ? login to Nexus by username: admin
48
+ ? password: [hidden]
49
+ ```
42
50
 
43
51
  3. If there is upload error, delete other packages within the ./node_modules_pack, and upload again.
44
52
 
45
53
  ### Step 3. Install packages in LAN
46
- Remove yarn.lock or package-lock.json file.
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
+ ```
47
61
 
48
62
  **YARN**
49
63
  ```
@@ -57,7 +71,3 @@ npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
57
71
  npm install --save --legacy-peer-deps # NOTE:NEED TO USE LEGACY PEER DEPS WHEN INSTALLING IN LAN
58
72
  ```
59
73
 
60
- ## TODO
61
- - [x] show package and upload progress
62
- - [x] support yarn
63
- - [ ] 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` 和 `npm` 两种打包工具。
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
- ### 第一步. 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack 文件夹
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,20 +46,27 @@ 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
-
46
- ```
47
- ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
48
- ? Please input your Nexus npm repository (eg: localNpm): localNpm
49
- ? login to Nexus by username: admin
50
- ? password: [hidden]
51
- ```
53
+ ```
54
+ ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
55
+ ? Please input your Nexus npm repository (eg: localNpm): localNpm
56
+ ? login to Nexus by username: admin
57
+ ? password: [hidden]
58
+ ```
52
59
 
53
60
  3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传(也可以直接重试上传)。
54
61
 
55
62
  ### 第三步. 在内网机器上执行包依赖安装
56
- 先删除 yarn.lock 或者 package-lock.json 文件。
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
+ ```
57
70
 
58
71
  **YARN**
59
72
  ```
@@ -67,7 +80,3 @@ npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
67
80
  npm install --save --legacy-peer-deps # 注意:在私网上安装 npm 包需要忽略对 peer 依赖
68
81
  ```
69
82
 
70
- ## TODO
71
- - [x] 实现打包和上传显示进度
72
- - [x] 支持 yarn
73
- - [ ] 支持 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('2.0.1')
18
+ .version('3.0.0')
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
@@ -53,24 +67,31 @@ const downloadPackage = async (packageUrl) => {
53
67
  const urlObj = url.parse(packageUrl)
54
68
  const filename = urlObj.pathname.substring(urlObj.pathname.lastIndexOf('/') + 1)
55
69
  const fileStream = createWriteStream('./node_modules_pack/' + filename)
56
- await fetch(packageUrl).then(res => {
70
+ let res
71
+ try {
72
+ res = await fetch(packageUrl)
73
+ } catch (error) {
74
+ console.error(`Failed to download package ${packageUrl}: ${error.message}`)
75
+ return filename
76
+ }
77
+ if (!res?.ok) {
78
+ console.error(`Failed to download package ${filename}: ${res.status} ${res.statusText}`)
79
+ return filename
80
+ } else {
57
81
  res.body.pipe(fileStream)
58
- })
59
- fileStream.on('finish', () => {
60
- fileStream.close()
61
- index++
62
- process.stdout.clearLine()
63
- process.stdout.cursorTo(0)
64
- process.stdout.write(`${index}: ${filename}`) // 在同一行打印处理进度
82
+ await new Promise((resolve, reject) => {
83
+ fileStream.on('finish', () => {
84
+ fileStream.close()
85
+ index++
86
+ process.stdout.clearLine()
87
+ process.stdout.cursorTo(0)
88
+ process.stdout.write(`${index}: ${filename}`) // 在同一行打印处理进度
89
+ resolve(filename)
90
+ })
91
+ fileStream.on('error', reject)
92
+ })
65
93
  return filename
66
- })
67
- }
68
-
69
-
70
- // npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。
71
- const publishPackage = async (packageName) => {
72
- const { stdout } = await execa('npm', ['publish', './node_modules_pack/' + packageName])
73
- return stdout
94
+ }
74
95
  }
75
96
 
76
97
  // 通过 nexus 的 components upload API 上传 npm 包,有时候会报 statusCode 500,暂不知道原因,重新上传即可。
@@ -107,23 +128,26 @@ program
107
128
  type: 'list',
108
129
  name: 'packageManager',
109
130
  message: 'Please select your package manager:',
110
- choices: ['yarn', 'npm'],
111
- default: 'npm'
131
+ choices: ['pnpm', 'yarn', 'npm'],
132
+ default: 'pnpm'
112
133
  },
113
134
  ]
114
- inquirer.prompt(questions).then(options => {
115
- getPackageInfos(options.packageManager).then(packages => {
135
+ inquirer.prompt(questions).then((options) => {
136
+ getPackageInfos(options.packageManager).then(async (packages) => {
116
137
  console.log(`Total ${packages.length} packages to be packed, please wait...`)
117
138
  mkdirSync('./node_modules_pack', { recursive: true })
118
139
  if (options.packageManager === 'yarn') {
119
- for (const packageUrl of packages) {
120
- downloadPackage(packageUrl)
121
- }
140
+ const downloadTasks = packages.map((packageUrl) =>
141
+ limit(() => downloadPackage(packageUrl))
142
+ )
143
+ await Promise.all(downloadTasks)
122
144
  } else {
123
- for (const packageName of packages) {
124
- packPackage(packageName)
125
- }
145
+ const packTasks = packages.map((packageName) =>
146
+ limit(() => packPackage(packageName))
147
+ )
148
+ await Promise.all(packTasks)
126
149
  }
150
+ console.log('All packages are successfully packed in ./node_modules_pack !')
127
151
  })
128
152
  })
129
153
  })
@@ -166,14 +190,4 @@ program
166
190
  })
167
191
  })
168
192
 
169
- // program
170
- // .command('publish')
171
- // .description('Publish all packed packages in ./node_modules_pack')
172
- // .action(() => {
173
- // const packages = readdirSync('./node_modules_pack')
174
- // packages.forEach(packageName => {
175
- // publishPackage(packageName).then(stdout => console.log(stdout))
176
- // })
177
- // })
178
-
179
193
  program.parse()
package/package.json CHANGED
@@ -1,36 +1,45 @@
1
- {
2
- "name": "@fanwang/pack-util",
3
- "version": "2.0.1",
4
- "description": "Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.",
5
- "bin": {
6
- "pack-util": "./bin/index.js"
7
- },
8
- "publishConfig": {
9
- "access": "public"
10
- },
11
- "engines": {
12
- "node": ">= 16.17.0"
13
- },
14
- "type": "module",
15
- "scripts": {
16
- "test": "echo \"Error: no test specified\" && exit 1"
17
- },
18
- "repository": {
19
- "type": "git",
20
- "url": "https://gitee.com/vampirefan/pack-util.git"
21
- },
22
- "keywords": [
23
- "npm",
24
- "pack",
25
- "publish"
26
- ],
27
- "author": "fanwang",
28
- "license": "MIT",
29
- "dependencies": {
30
- "commander": "^9.4.1",
31
- "execa": "^6.1.0",
32
- "form-data": "^4.0.0",
33
- "inquirer": "^9.1.3",
34
- "node-fetch": "^3.3.0"
35
- }
36
- }
1
+ {
2
+ "name": "@fanwang/pack-util",
3
+ "version": "3.0.0",
4
+ "description": "Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.",
5
+ "bin": {
6
+ "pack-util": "./bin/index.js"
7
+ },
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "engines": {
12
+ "node": ">= 16.17.0"
13
+ },
14
+ "type": "module",
15
+ "scripts": {
16
+ "test": "node bin/index.js pack",
17
+ "commit": "git add . && czg && git push",
18
+ "release": "bumpp package.json bin/index.js"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/vampirefan/pack-util.git"
23
+ },
24
+ "keywords": [
25
+ "npm",
26
+ "pack",
27
+ "publish"
28
+ ],
29
+ "author": "fanwang",
30
+ "license": "MIT",
31
+ "dependencies": {
32
+ "commander": "^9.5.0",
33
+ "execa": "^9.4.0",
34
+ "form-data": "^4.0.0",
35
+ "inquirer": "^9.3.7",
36
+ "node-fetch": "^3.3.2",
37
+ "p-limit": "^6.1.0",
38
+ "yaml": "^2.5.1"
39
+ },
40
+ "devDependencies": {
41
+ "bumpp": "^9.6.1",
42
+ "czg": "^1.10.1",
43
+ "taze": "^0.17.2"
44
+ }
45
+ }