@fanwang/pack-util 1.0.6 → 1.0.9

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 +50 -18
  2. package/bin/index.js +28 -14
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,60 +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)
13
+
6
14
  ```sh
7
15
  npm install -g @fanwang/pack-util
8
16
  ```
9
17
 
10
- ## Usage: pack-util [command]
18
+ ## 使用说明 (Usage: pack-util [ pack | upload ])
19
+
11
20
  如果使用控制台命令的时候提示电脑禁止运行脚本,用管理员身份运行 cmd 或者 vscode 等然后在命令行输入:
21
+
12
22
  ```
13
23
  Set-ExecutionPolicy RemoteSigned
14
24
  ```
15
- ### Pack all installed package in ./node_modules
16
- 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack
25
+
26
+ ### 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack
27
+ Pack all installed package in ./node_modules
28
+
17
29
  ```sh
18
30
  cd your_project
19
31
  pack-util pack
20
32
  ```
21
33
 
22
- ### Upload all packed packages in ./node_modules_pack
23
- 1. Copy ./node_modules_pack along with your project.
24
- 将 ./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
+
25
42
  ```sh
26
43
  cd your_project
27
44
  pack-util upload
28
45
  ```
29
- 2. Input some params
30
- 输入参数
46
+
47
+ 2. 输入参数
48
+
49
+ Input some params
50
+
31
51
  ```
32
- ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://127.0.0.1:8081/
52
+ ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://xx.xx.xx.xx:8081/
33
53
  ? Please input your Nexus npm repository (eg: localNpm): localNpm
34
54
  ? login to Nexus by username: admin
35
55
  ? password: [hidden]
36
56
  ```
37
- 3. If there is upload error, delete other packages within the ./node_modules_pack, and upload again.
38
- 如果出现上传失败,删除其他成功上传的包,然后重新执行上传。
39
57
 
40
- ### ~~Publish all packed packages in ./node_modules_pack~~
41
- npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。目前[没法 override npm 的这种行为](https://stackoverflow.com/questions/66914753/override-registry-mentioned-in-publishconfig-of-package-json-through-command-lin)。
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 文件夹和你的项目一起拷贝至内网,然后执行发布。
42
69
 
43
70
  copy node_modules_pack along with your project.
71
+
44
72
  1. change registry
73
+
45
74
  ```sh
46
75
  npm config set registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
47
76
  ```
48
77
  2. adduser
78
+
49
79
  ```sh
50
80
  npm adduser -registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
51
81
  ```
52
- 2. batch publish
82
+
83
+ 3. batch publish
84
+
53
85
  ```sh
54
86
  cd your_project
55
87
  pack-util publish
56
88
  ```
57
- ~~
89
+
58
90
 
59
91
  ## TODO
60
- - [] 实现打包和上传显示进度
92
+ - [x] 实现打包和上传显示进度
package/bin/index.js CHANGED
@@ -11,25 +11,33 @@ const program = new Command()
11
11
  program
12
12
  .name('pack-util')
13
13
  .description('Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.')
14
- .version('1.0.6')
14
+ .version('1.0.9')
15
15
 
16
16
  const getPackageNames = async () => {
17
17
  const { stdout } = await execa('npm', ['list', '--all'])
18
18
  let packages = stdout.split('\n')
19
19
  packages.splice(0, 1)//去掉首行路径字符串
20
- return packages.filter(item => !item.includes('deduped'))//去掉重复包
20
+ packages = packages.filter(item => !item.includes('deduped')) // 去掉标记重复的包
21
21
  .map(name => {
22
22
  const nameRegex = /(\S+@.+)/g
23
23
  const nameParse = name.match(nameRegex)
24
24
  if (nameParse && nameParse.length > 0) return nameParse[0]
25
25
  else return ''
26
- })//解析每行的包名称
27
- .filter(packageName => packageName.length > 0)//去掉空行
26
+ }) // 解析每行的包名称
27
+ .filter(packageName => packageName.length > 0) // 去掉空行
28
+ return Array.from(new Set(packages)) // 去掉重复项
29
+ // return packages
28
30
  }
29
31
 
30
32
  // 利用 npm pack 在外网上对 dependencies 中的所有包打包。
33
+ var index = 0
31
34
  const packPackage = async (packageName) => {
32
35
  const { stdout } = await execa('npm', ['pack', packageName, '--pack-destination=./node_modules_pack'])
36
+ index++
37
+ console.log(`${index}: ${stdout}`)
38
+ // process.stdout.clearLine()
39
+ // process.stdout.cursorTo(0)
40
+ // process.stdout.write(`${index}: ${stdout}`) // 在同一行打印处理进度
33
41
  return stdout
34
42
  }
35
43
 
@@ -44,15 +52,23 @@ const uploadPackage = async (packageName, options) => {
44
52
  const { host, repository, username, password } = options
45
53
  const form = new FormData()
46
54
  form.append('npm.asset', createReadStream('./node_modules_pack/' + packageName))
47
- // return form
48
55
  form.submit({
49
56
  hostname: new URL(host).hostname,
50
57
  port: new URL(host).port,
51
58
  path: `/service/rest/v1/components?repository=${repository}`,
52
59
  auth: `${username}:${password}`
53
60
  }, function (err, res) {
54
- if (res.statusCode > 200 && res.statusCode < 300) console.log(packageName)
55
- else console.log(`${packageName} : UPLOAD ERROR -- code ${res.statusCode} -- ${res.statusMessage}`)
61
+ if (err) console.log(err.message)
62
+ else {
63
+ if (res.statusCode > 200 && res.statusCode < 300) {
64
+ // upload success
65
+ index++
66
+ process.stdout.clearLine()
67
+ process.stdout.cursorTo(0)
68
+ process.stdout.write(`${index}: ${packageName}`) // 在同一行打印处理进度
69
+ }
70
+ else console.log(`${packageName} : UPLOAD ERROR -- code ${res.statusCode} -- ${res.statusMessage}`) //打印上传失败的包
71
+ }
56
72
  })
57
73
  }
58
74
 
@@ -63,11 +79,9 @@ program
63
79
  getPackageNames().then(packages => {
64
80
  console.log(`Total ${packages.length} packages to be packed, please wait...`)
65
81
  mkdirSync('./node_modules_pack', { recursive: true })
66
- packages.forEach(packageName => {
67
- packPackage(packageName).then(stdout => {
68
- console.log(stdout)
69
- })
70
- })
82
+ for (const packageName of packages) {
83
+ packPackage(packageName)
84
+ }
71
85
  })
72
86
  })
73
87
 
@@ -103,9 +117,9 @@ program
103
117
  inquirer.prompt(questions).then(options => {
104
118
  const packages = readdirSync('./node_modules_pack')
105
119
  console.log(`Total ${packages.length} packages to be uploaded, please wait...`)
106
- packages.forEach(packageName => {
120
+ for (const packageName of packages) {
107
121
  uploadPackage(packageName, options)
108
- })
122
+ }
109
123
  })
110
124
  })
111
125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fanwang/pack-util",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
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"