@fanwang/pack-util 1.0.5 → 1.0.8

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.
Files changed (3) hide show
  1. package/README.md +65 -6
  2. package/bin/index.js +91 -17
  3. package/package.json +4 -2
package/README.md CHANGED
@@ -1,33 +1,92 @@
1
1
  # pack-util
2
- 打包工具,可以将 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,然后批量发布到 nexus 搭建的 npm 私服上。
2
+ 打包工具,可以在公网上将项目 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,然后批量上传到 nexus 搭建的 npm 私服上。
3
+
3
4
  Pack all installed package in ./node_modules, and publish to Nexus hosted Registry.
4
5
 
5
- ## Install
6
+ ## !!注意:在私网上安装 npm 包需要忽略对 peer 依赖!!
7
+
8
+ ```
9
+ npm install --save --legacy-peer-deps
10
+ ```
11
+
12
+ ## 安装 (Install)
6
13
 
7
14
  ```sh
8
15
  npm install -g @fanwang/pack-util
9
16
  ```
10
17
 
11
- ## Usage: pack-util [command]
18
+ ## 使用说明 (Usage: pack-util [ pack | upload ])
19
+
20
+ 如果使用控制台命令的时候提示电脑禁止运行脚本,用管理员身份运行 cmd 或者 vscode 等然后在命令行输入:
21
+
22
+ ```
23
+ Set-ExecutionPolicy RemoteSigned
24
+ ```
25
+
26
+ ### 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack
27
+ Pack all installed package in ./node_modules
12
28
 
13
- ### Pack all installed package in ./node_modules
14
29
  ```sh
15
30
  cd your_project
16
31
  pack-util pack
17
32
  ```
18
33
 
19
- ### Publish all packed packages in ./node_modules_pack
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
+
42
+ ```sh
43
+ cd your_project
44
+ pack-util upload
45
+ ```
46
+
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
+ ```
57
+
58
+ 3. 如果出现上传失败,删除其他成功上传的包,然后重新执行上传。
59
+
60
+ If there is upload error, delete other packages within the ./node_modules_pack, and upload again.
61
+
62
+
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
+
20
70
  copy node_modules_pack along with your project.
71
+
21
72
  1. change registry
73
+
22
74
  ```sh
23
75
  npm config set registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
24
76
  ```
25
77
  2. adduser
78
+
26
79
  ```sh
27
80
  npm adduser -registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
28
81
  ```
29
- 2. batch publish
82
+
83
+ 3. batch publish
84
+
30
85
  ```sh
31
86
  cd your_project
32
87
  pack-util publish
33
88
  ```
89
+
90
+
91
+ ## TODO
92
+ - [x] 实现打包和上传显示进度
package/bin/index.js CHANGED
@@ -1,39 +1,75 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { execa } from 'execa'
4
- import { mkdirSync, readdirSync } from 'fs'
4
+ import { mkdirSync, readdirSync, createReadStream } from 'fs'
5
5
  import { Command } from 'commander'
6
+ import inquirer from 'inquirer'
7
+ import FormData from 'form-data'
6
8
 
7
9
  const program = new Command()
8
10
 
11
+ program
12
+ .name('pack-util')
13
+ .description('Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.')
14
+ .version('1.0.8')
15
+
9
16
  const getPackageNames = async () => {
10
17
  const { stdout } = await execa('npm', ['list', '--all'])
11
18
  let packages = stdout.split('\n')
12
19
  packages.splice(0, 1)//去掉首行路径字符串
13
- return packages.filter(item => !item.includes('deduped'))//去掉重复包
20
+ packages = packages.filter(item => !item.includes('deduped')) // 去掉标记重复的包
14
21
  .map(name => {
15
- const nameRegex = /(\S+@.+)/g;
22
+ const nameRegex = /(\S+@.+)/g
16
23
  const nameParse = name.match(nameRegex)
17
24
  if (nameParse && nameParse.length > 0) return nameParse[0]
18
25
  else return ''
19
- })//解析每行的包名称
20
- .filter(packageName => packageName.length > 0)//去掉空行
26
+ }) // 解析每行的包名称
27
+ .filter(packageName => packageName.length > 0) // 去掉空行
28
+ return Array.from(new Set(packages)) // 去掉重复项
29
+ // return packages
21
30
  }
22
31
 
32
+ // 利用 npm pack 在外网上对 dependencies 中的所有包打包。
33
+ var index = 0
23
34
  const packPackage = async (packageName) => {
24
35
  const { stdout } = await execa('npm', ['pack', packageName, '--pack-destination=./node_modules_pack'])
36
+ index++
37
+ process.stdout.clearLine()
38
+ process.stdout.cursorTo(0)
39
+ process.stdout.write(`${index}: ${stdout}`) // 在同一行打印处理进度
25
40
  return stdout
26
41
  }
27
42
 
43
+ // npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。
28
44
  const publishPackage = async (packageName) => {
29
45
  const { stdout } = await execa('npm', ['publish', './node_modules_pack/' + packageName])
30
46
  return stdout
31
47
  }
32
48
 
33
- program
34
- .name('pack-util')
35
- .description('Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.')
36
- .version('1.0.5')
49
+ // 通过 nexus 的 components upload API 上传 npm 包,有时候会报 statusCode 500,暂不知道原因,重新上传即可。
50
+ const uploadPackage = async (packageName, options) => {
51
+ const { host, repository, username, password } = options
52
+ const form = new FormData()
53
+ form.append('npm.asset', createReadStream('./node_modules_pack/' + packageName))
54
+ form.submit({
55
+ hostname: new URL(host).hostname,
56
+ port: new URL(host).port,
57
+ path: `/service/rest/v1/components?repository=${repository}`,
58
+ auth: `${username}:${password}`
59
+ }, function (err, res) {
60
+ if (err) console.log(err.message)
61
+ else {
62
+ if (res.statusCode > 200 && res.statusCode < 300) {
63
+ // upload success
64
+ index++
65
+ process.stdout.clearLine()
66
+ process.stdout.cursorTo(0)
67
+ process.stdout.write(`${index}: ${packageName}`) // 在同一行打印处理进度
68
+ }
69
+ else console.log(`${packageName} : UPLOAD ERROR -- code ${res.statusCode} -- ${res.statusMessage}`) //打印上传失败的包
70
+ }
71
+ })
72
+ }
37
73
 
38
74
  program
39
75
  .command('pack')
@@ -42,20 +78,58 @@ program
42
78
  getPackageNames().then(packages => {
43
79
  console.log(`Total ${packages.length} packages to be packed, please wait...`)
44
80
  mkdirSync('./node_modules_pack', { recursive: true })
45
- packages.forEach(packageName => {
46
- packPackage(packageName).then(stdout => console.log(stdout))
47
- })
81
+ for (const packageName of packages) {
82
+ packPackage(packageName)
83
+ }
48
84
  })
49
85
  })
50
86
 
87
+
51
88
  program
52
- .command('publish')
53
- .description('Publish all packed packages in ./node_modules_pack')
89
+ .command('upload')
90
+ .description('Upload all packed packages in ./node_modules_pack to Nexus self-hosted Registry')
54
91
  .action(() => {
55
- const packages = readdirSync('./node_modules_pack')
56
- packages.forEach(packageName => {
57
- publishPackage(packageName).then(stdout => console.log(stdout))
92
+ const questions = [
93
+ {
94
+ type: 'input',
95
+ name: 'host',
96
+ message: 'Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): '
97
+ },
98
+ {
99
+ type: 'input',
100
+ name: 'repository',
101
+ message: 'Please input your Nexus npm repository (eg: localNpm): ',
102
+ default: 'localNpm'
103
+ },
104
+ {
105
+ type: 'input',
106
+ name: 'username',
107
+ message: 'login to Nexus by username: ',
108
+ default: 'admin'
109
+ },
110
+ {
111
+ type: 'password',
112
+ name: 'password',
113
+ message: 'password: '
114
+ },
115
+ ]
116
+ inquirer.prompt(questions).then(options => {
117
+ const packages = readdirSync('./node_modules_pack')
118
+ console.log(`Total ${packages.length} packages to be uploaded, please wait...`)
119
+ for (const packageName of packages) {
120
+ uploadPackage(packageName, options)
121
+ }
58
122
  })
59
123
  })
60
124
 
125
+ // program
126
+ // .command('publish')
127
+ // .description('Publish all packed packages in ./node_modules_pack')
128
+ // .action(() => {
129
+ // const packages = readdirSync('./node_modules_pack')
130
+ // packages.forEach(packageName => {
131
+ // publishPackage(packageName).then(stdout => console.log(stdout))
132
+ // })
133
+ // })
134
+
61
135
  program.parse()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fanwang/pack-util",
3
- "version": "1.0.5",
3
+ "version": "1.0.8",
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"
@@ -28,6 +28,8 @@
28
28
  "license": "MIT",
29
29
  "dependencies": {
30
30
  "commander": "^9.4.1",
31
- "execa": "^6.1.0"
31
+ "execa": "^6.1.0",
32
+ "form-data": "^4.0.0",
33
+ "inquirer": "^9.1.3"
32
34
  }
33
35
  }