@fanwang/pack-util 1.0.9 → 2.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,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,21 +1,19 @@
1
- # pack-util
2
- 打包工具,可以在公网上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,然后批量上传到 nexus 搭建的 npm 私服上。
1
+ # pack-util
3
2
 
4
- Pack all installed package in ./node_modules, and publish to Nexus hosted Registry.
3
+ **中文** | [English](./README.en-US.md)
5
4
 
6
- ## !!注意:在私网上安装 npm 包需要忽略对 peer 依赖!!
5
+ 一个打包工具,可以在互联网网机器上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,并批量上传到 nexus 搭建的 npm 私服上。支持 `yarn` 和 `npm` 两种打包工具。
7
6
 
8
- ```
9
- npm install --save --legacy-peer-deps
10
- ```
11
-
12
- ## 安装 (Install)
7
+ ## 安装
13
8
 
9
+ 全局安装即可。
14
10
  ```sh
15
11
  npm install -g @fanwang/pack-util
16
12
  ```
17
13
 
18
- ## 使用说明 (Usage: pack-util [ pack | upload ])
14
+ ## 使用
15
+
16
+ **使用说明**
19
17
 
20
18
  如果使用控制台命令的时候提示电脑禁止运行脚本,用管理员身份运行 cmd 或者 vscode 等然后在命令行输入:
21
19
 
@@ -23,70 +21,52 @@ npm install -g @fanwang/pack-util
23
21
  Set-ExecutionPolicy RemoteSigned
24
22
  ```
25
23
 
26
- ### 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack
27
- Pack all installed package in ./node_modules
24
+ 建议先将源仓库设为阿里镜像仓库,这样打包速度更快:
25
+ ```sh
26
+ npm config set registry https://registry.npmmirror.com
27
+ ```
28
28
 
29
+ ### 第一步. 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack 文件夹
29
30
  ```sh
30
31
  cd your_project
31
32
  pack-util pack
33
+ # 选择所使用的包管理工具即可( yarn | npm )
32
34
  ```
33
35
 
34
- ### 批量上传到 nexus 搭建的 npm 私服上
35
- Upload all packed packages in ./node_modules_pack
36
-
37
- 1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网,然后执行上传。
38
-
39
- Copy ./node_modules_pack along with your project.
40
-
41
-
36
+ ### 第二步. 将依赖包批量上传到 nexus 搭建的 npm 私服上
37
+ 1. 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网机器上,然后执行上传。
42
38
  ```sh
43
39
  cd your_project
44
40
  pack-util upload
45
41
  ```
46
42
 
47
- 2. 输入参数
48
-
49
- Input some params
50
-
51
- ```
52
- ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
53
- ? Please input your Nexus npm repository (eg: localNpm): localNpm
54
- ? login to Nexus by username: admin
55
- ? password: [hidden]
56
- ```
43
+ 2. 输入参数
57
44
 
58
- 3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传。
45
+ ```
46
+ ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
47
+ ? Please input your Nexus npm repository (eg: localNpm): localNpm
48
+ ? login to Nexus by username: admin
49
+ ? password: [hidden]
50
+ ```
59
51
 
60
- If there is upload error, delete other packages within the ./node_modules_pack, and upload again.
52
+ 3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传(也可以直接重试上传)。
61
53
 
54
+ ### 第三步. 在内网机器上执行包依赖安装
55
+ 先删除 yarn.lock 或者 package-lock.json 文件。
62
56
 
63
-
64
- ### ~~批量发布至 Nexus 搭建的 npm 私服上,Publish all packed packages in ./node_modules_pack~~
65
- !!以下方式已弃用:
66
- 原因是 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)。
67
-
68
- 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网,然后执行发布。
69
-
70
- copy node_modules_pack along with your project.
71
-
72
- 1. change registry
73
-
74
- ```sh
75
- npm config set registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
57
+ **YARN**
76
58
  ```
77
- 2. adduser
78
-
79
- ```sh
80
- npm adduser -registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
59
+ yarn config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
60
+ yarn install
81
61
  ```
82
62
 
83
- 3. batch publish
84
-
85
- ```sh
86
- cd your_project
87
- pack-util publish
63
+ **NPM**
64
+ ```
65
+ npm config set registry http://xx.xx.xx.xx:xxxx/repository/localNpm/
66
+ npm install --save --legacy-peer-deps # 注意:在私网上安装 npm 包需要忽略对 peer 依赖
88
67
  ```
89
-
90
68
 
91
69
  ## TODO
92
70
  - [x] 实现打包和上传显示进度
71
+ - [x] 支持 yarn
72
+ - [ ] 支持 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.0.9')
15
+ .version('2.0.0')
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 中的所有包打包。
@@ -34,13 +42,27 @@ var index = 0
34
42
  const packPackage = async (packageName) => {
35
43
  const { stdout } = await execa('npm', ['pack', packageName, '--pack-destination=./node_modules_pack'])
36
44
  index++
37
- console.log(`${index}: ${stdout}`)
38
- // process.stdout.clearLine()
39
- // process.stdout.cursorTo(0)
40
- // process.stdout.write(`${index}: ${stdout}`) // 在同一行打印处理进度
45
+ // console.log(`${index}: ${stdout}`)
46
+ process.stdout.clearLine()
47
+ process.stdout.cursorTo(0)
48
+ process.stdout.write(`${index}: ${stdout}`) // 在同一行打印处理进度
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
+ await fetch(packageUrl).then(res => {
56
+ res.body.pipe(createWriteStream('./node_modules_pack/' + filename))
57
+ })
58
+ index++
59
+ process.stdout.clearLine()
60
+ process.stdout.cursorTo(0)
61
+ process.stdout.write(`${index}: ${filename}`) // 在同一行打印处理进度
62
+ return filename
63
+ }
64
+
65
+
44
66
  // npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。
45
67
  const publishPackage = async (packageName) => {
46
68
  const { stdout } = await execa('npm', ['publish', './node_modules_pack/' + packageName])
@@ -76,12 +98,29 @@ program
76
98
  .command('pack')
77
99
  .description('Pack all installed package in ./node_modules')
78
100
  .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
- }
101
+ const questions = [
102
+ {
103
+ type: 'list',
104
+ name: 'packageManager',
105
+ message: 'Please select your package manager:',
106
+ choices: ['yarn', 'npm'],
107
+ default: 'npm'
108
+ },
109
+ ]
110
+ inquirer.prompt(questions).then(options => {
111
+ getPackageInfos(options.packageManager).then(packages => {
112
+ console.log(`Total ${packages.length} packages to be packed, please wait...`)
113
+ mkdirSync('./node_modules_pack', { recursive: true })
114
+ if (options.packageManager === 'yarn') {
115
+ for (const packageUrl of packages) {
116
+ downloadPackage(packageUrl)
117
+ }
118
+ } else {
119
+ for (const packageName of packages) {
120
+ packPackage(packageName)
121
+ }
122
+ }
123
+ })
85
124
  })
86
125
  })
87
126
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fanwang/pack-util",
3
- "version": "1.0.9",
3
+ "version": "2.0.0",
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"
@@ -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
  }