@fanwang/pack-util 1.1.0 → 2.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.
@@ -0,0 +1,63 @@
1
+ # pack-util
2
+
3
+ Pack all installed package in ./node_modules, and publish to Nexus hosted Registry. Support both `yarn` and `npm`.
4
+
5
+ **English** | [中文](./README.md)
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install -g @fanwang/pack-util
11
+ ```
12
+
13
+ ## Usage: pack-util [ pack | upload ]
14
+ If power shell warning for forbidding running scripts, input command below as administrator:
15
+ ```
16
+ Set-ExecutionPolicy RemoteSigned
17
+ ```
18
+
19
+ ### Step 1. Pack all installed package in ./node_modules
20
+
21
+ ```sh
22
+ cd your_project
23
+ pack-util pack
24
+ # select your package manager ( yarn | npm )
25
+ ```
26
+
27
+ ### Step 2. Upload all packed packages in ./node_modules_pack
28
+ 1. Copy ./node_modules_pack along with your project.
29
+ ```sh
30
+ cd your_project
31
+ pack-util upload
32
+ ```
33
+
34
+ 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
+ ```
42
+
43
+ 3. If there is upload error, delete other packages within the ./node_modules_pack, and upload again.
44
+
45
+ ### Step 3. Install packages in LAN
46
+ Remove yarn.lock or package-lock.json file.
47
+
48
+ **YARN**
49
+ ```
50
+ yarn config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
51
+ yarn install
52
+ ```
53
+
54
+ **NPM**
55
+ ```
56
+ npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
57
+ npm install --save --legacy-peer-deps # NOTE:NEED TO USE LEGACY PEER DEPS WHEN INSTALLING IN LAN
58
+ ```
59
+
60
+ ## TODO
61
+ - [x] show package and upload progress
62
+ - [x] support yarn
63
+ - [ ] support pnpm
package/README.md CHANGED
@@ -1,15 +1,19 @@
1
- # pack-util
2
- `npm package` 打包工具,可以在公网上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,然后批量上传到 nexus 搭建的 npm 私服上。(Pack all installed package in ./node_modules, and publish to Nexus hosted Registry.)
1
+ # pack-util
3
2
 
4
- ## !!注意:在私网上安装 npm 包需要忽略对 peer 依赖!!
5
- ```
6
- npm install --save --legacy-peer-deps
7
- ```
8
- ## 安装 (Install)
3
+ **中文** | [English](./README.en-US.md)
4
+
5
+ 一个打包工具,可以在互联网网机器上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,并批量上传到 nexus 搭建的 npm 私服上。支持 `yarn` 和 `npm` 两种打包工具。
6
+
7
+ ## 安装
8
+
9
+ 全局安装即可。
9
10
  ```sh
10
11
  npm install -g @fanwang/pack-util
11
12
  ```
12
- ## 使用说明 (Usage: pack-util [ pack | upload ])
13
+
14
+ ## 使用
15
+
16
+ **使用说明**
13
17
 
14
18
  如果使用控制台命令的时候提示电脑禁止运行脚本,用管理员身份运行 cmd 或者 vscode 等然后在命令行输入:
15
19
 
@@ -17,49 +21,53 @@ npm install -g @fanwang/pack-util
17
21
  Set-ExecutionPolicy RemoteSigned
18
22
  ```
19
23
 
20
- ### 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack(Pack all installed package in ./node_modules)
21
- **推荐现将 npm 仓库设为阿里仓库,这样打包更快:`npm config set registry https://registry.npmmirror.com`**
24
+ 建议先将源仓库设为阿里镜像仓库,这样打包速度更快(yarn 和 npm 需要同时设置):
25
+ ```sh
26
+ yarn config set registry https://registry.npmmirror.com
27
+ npm config set registry https://registry.npmmirror.com
28
+ ```
22
29
 
30
+ ### 第一步. 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack 文件夹
23
31
  ```sh
24
32
  cd your_project
25
33
  pack-util pack
34
+ # 选择所使用的包管理工具即可( yarn | npm )
26
35
  ```
27
36
 
28
- ### 批量上传到 nexus 搭建的 npm 私服上(Upload all packed packages in ./node_modules_pack)
29
- 1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网,然后执行上传。(Copy ./node_modules_pack along with your project.)
37
+ ### 第二步. 将依赖包批量上传到 nexus 搭建的 npm 私服上
38
+ 1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网机器上,然后执行上传。
30
39
  ```sh
31
40
  cd your_project
32
41
  pack-util upload
33
42
  ```
34
43
 
35
- 2. 输入参数(Input some params)
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
+ 2. 输入参数
42
45
 
43
- 3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传。(If there is upload error, delete other packages within the ./node_modules_pack, and upload again.)
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
+ ```
44
52
 
45
- ### ~~批量发布至 Nexus 搭建的 npm 私服上,Publish all packed packages in ./node_modules_pack~~
46
- !!以下方式已弃用:
47
- 原因是 npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。[目前没法 Hack npm 的这种行为](https://stackoverflow.com/questions/66914753/override-registry-mentioned-in-publishconfig-of-package-json-through-command-lin)。
53
+ 3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传(也可以直接重试上传)。
48
54
 
49
- 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网,然后执行发布。(copy node_modules_pack along with your project.)
55
+ ### 第三步. 在内网机器上执行包依赖安装
56
+ 先删除 yarn.lock 或者 package-lock.json 文件。
50
57
 
51
- 1. change registry
52
- ```sh
53
- npm config set registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
58
+ **YARN**
54
59
  ```
55
- 2. adduser
56
- ```sh
57
- npm adduser -registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
60
+ yarn config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
61
+ yarn install
58
62
  ```
59
- 3. batch publish
60
- ```sh
61
- cd your_project
62
- pack-util publish
63
+
64
+ **NPM**
63
65
  ```
66
+ npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
67
+ npm install --save --legacy-peer-deps # 注意:在私网上安装 npm 包需要忽略对 peer 依赖
68
+ ```
69
+
64
70
  ## TODO
65
71
  - [x] 实现打包和上传显示进度
72
+ - [x] 支持 yarn
73
+ - [ ] 支持 pnpm
package/bin/index.js CHANGED
@@ -1,32 +1,40 @@
1
1
  #!/usr/bin/env node
2
-
3
2
  import { execa } from 'execa'
4
- import { mkdirSync, readdirSync, createReadStream } from 'fs'
3
+ import { mkdirSync, readdirSync, createReadStream, createWriteStream, readFileSync } from 'fs'
5
4
  import { Command } from 'commander'
6
5
  import inquirer from 'inquirer'
7
6
  import FormData from 'form-data'
7
+ import fetch from 'node-fetch'
8
+ import url from 'url'
8
9
 
9
10
  const program = new Command()
10
11
 
11
12
  program
12
13
  .name('pack-util')
13
14
  .description('Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.')
14
- .version('1.1.0')
15
+ .version('2.0.1')
15
16
 
16
- const getPackageNames = async () => {
17
- const { stdout } = await execa('npm', ['list', '--all'])
18
- let packages = stdout.split('\n')
19
- packages.splice(0, 1)//去掉首行路径字符串
20
- packages = packages.filter(item => !item.includes('deduped')) // 去掉标记重复的包
21
- .map(name => {
22
- const nameRegex = /(\S+@.+)/g
23
- const nameParse = name.match(nameRegex)
24
- if (nameParse && nameParse.length > 0) return nameParse[0]
25
- else return ''
26
- }) // 解析每行的包名称
27
- .filter(packageName => packageName.length > 0) // 去掉空行
28
- return Array.from(new Set(packages)) // 去掉重复项
29
- // return packages
17
+ const getPackageInfos = async (packageManager) => {
18
+ if (packageManager === 'yarn') {
19
+ const fileData = readFileSync('./yarn.lock')
20
+ return fileData.toString().split(/\r?\n/)
21
+ .filter(line => line.startsWith(' resolved'))
22
+ .map(line => line.substring(12, line.length - 1))
23
+ }
24
+ else {
25
+ const { stdout } = await execa('npm', ['list', '--all'])
26
+ let packages = stdout.split('\n')
27
+ packages.splice(0, 1)//去掉首行路径字符串
28
+ packages = packages.filter(item => !item.includes('deduped')) // 去掉标记重复的包
29
+ .map(name => {
30
+ const nameRegex = /(\S+@.+)/g
31
+ const nameParse = name.match(nameRegex)
32
+ if (nameParse && nameParse.length > 0) return nameParse[0]
33
+ else return ''
34
+ }) // 解析每行的包名称
35
+ .filter(packageName => packageName.length > 0) // 去掉空行
36
+ return Array.from(new Set(packages)) // 去掉重复项
37
+ }
30
38
  }
31
39
 
32
40
  // 利用 npm pack 在外网上对 dependencies 中的所有包打包。
@@ -41,6 +49,24 @@ const packPackage = async (packageName) => {
41
49
  return stdout
42
50
  }
43
51
 
52
+ const downloadPackage = async (packageUrl) => {
53
+ const urlObj = url.parse(packageUrl)
54
+ const filename = urlObj.pathname.substring(urlObj.pathname.lastIndexOf('/') + 1)
55
+ const fileStream = createWriteStream('./node_modules_pack/' + filename)
56
+ await fetch(packageUrl).then(res => {
57
+ 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}`) // 在同一行打印处理进度
65
+ return filename
66
+ })
67
+ }
68
+
69
+
44
70
  // npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。
45
71
  const publishPackage = async (packageName) => {
46
72
  const { stdout } = await execa('npm', ['publish', './node_modules_pack/' + packageName])
@@ -76,12 +102,29 @@ program
76
102
  .command('pack')
77
103
  .description('Pack all installed package in ./node_modules')
78
104
  .action(() => {
79
- getPackageNames().then(packages => {
80
- console.log(`Total ${packages.length} packages to be packed, please wait...`)
81
- mkdirSync('./node_modules_pack', { recursive: true })
82
- for (const packageName of packages) {
83
- packPackage(packageName)
84
- }
105
+ const questions = [
106
+ {
107
+ type: 'list',
108
+ name: 'packageManager',
109
+ message: 'Please select your package manager:',
110
+ choices: ['yarn', 'npm'],
111
+ default: 'npm'
112
+ },
113
+ ]
114
+ inquirer.prompt(questions).then(options => {
115
+ getPackageInfos(options.packageManager).then(packages => {
116
+ console.log(`Total ${packages.length} packages to be packed, please wait...`)
117
+ mkdirSync('./node_modules_pack', { recursive: true })
118
+ if (options.packageManager === 'yarn') {
119
+ for (const packageUrl of packages) {
120
+ downloadPackage(packageUrl)
121
+ }
122
+ } else {
123
+ for (const packageName of packages) {
124
+ packPackage(packageName)
125
+ }
126
+ }
127
+ })
85
128
  })
86
129
  })
87
130
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fanwang/pack-util",
3
- "version": "1.1.0",
3
+ "version": "2.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"
@@ -9,7 +9,7 @@
9
9
  "access": "public"
10
10
  },
11
11
  "engines": {
12
- "node": ">= 12.9.0"
12
+ "node": ">= 16.17.0"
13
13
  },
14
14
  "type": "module",
15
15
  "scripts": {
@@ -30,6 +30,7 @@
30
30
  "commander": "^9.4.1",
31
31
  "execa": "^6.1.0",
32
32
  "form-data": "^4.0.0",
33
- "inquirer": "^9.1.3"
33
+ "inquirer": "^9.1.3",
34
+ "node-fetch": "^3.3.0"
34
35
  }
35
36
  }