@fanwang/pack-util 1.0.5 → 1.0.6

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 +30 -3
  2. package/bin/index.js +73 -12
  3. package/package.json +4 -2
package/README.md CHANGED
@@ -3,20 +3,43 @@
3
3
  Pack all installed package in ./node_modules, and publish to Nexus hosted Registry.
4
4
 
5
5
  ## Install
6
-
7
6
  ```sh
8
7
  npm install -g @fanwang/pack-util
9
8
  ```
10
9
 
11
10
  ## Usage: pack-util [command]
12
-
11
+ 如果使用控制台命令的时候提示电脑禁止运行脚本,用管理员身份运行 cmd 或者 vscode 等然后在命令行输入:
12
+ ```
13
+ Set-ExecutionPolicy RemoteSigned
14
+ ```
13
15
  ### Pack all installed package in ./node_modules
16
+ 将所有 ./node_modules 中安装的包打包至 ./node_modules_pack
14
17
  ```sh
15
18
  cd your_project
16
19
  pack-util pack
17
20
  ```
18
21
 
19
- ### Publish all packed packages in ./node_modules_pack
22
+ ### Upload all packed packages in ./node_modules_pack
23
+ 1. Copy ./node_modules_pack along with your project.
24
+ 将 ./node_modules_pack 文件夹和你的项目一起拷贝至内网,然后执行上传。
25
+ ```sh
26
+ cd your_project
27
+ pack-util upload
28
+ ```
29
+ 2. Input some params
30
+ 输入参数
31
+ ```
32
+ ? Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): http://127.0.0.1:8081/
33
+ ? Please input your Nexus npm repository (eg: localNpm): localNpm
34
+ ? login to Nexus by username: admin
35
+ ? password: [hidden]
36
+ ```
37
+ 3. If there is upload error, delete other packages within the ./node_modules_pack, and upload again.
38
+ 如果出现上传失败,删除其他成功上传的包,然后重新执行上传。
39
+
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)。
42
+
20
43
  copy node_modules_pack along with your project.
21
44
  1. change registry
22
45
  ```sh
@@ -31,3 +54,7 @@ npm adduser -registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
31
54
  cd your_project
32
55
  pack-util publish
33
56
  ```
57
+ ~~
58
+
59
+ ## TODO
60
+ - [] 实现打包和上传显示进度
package/bin/index.js CHANGED
@@ -1,18 +1,25 @@
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.6')
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
20
  return 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 ''
@@ -20,20 +27,34 @@ const getPackageNames = async () => {
20
27
  .filter(packageName => packageName.length > 0)//去掉空行
21
28
  }
22
29
 
30
+ // 利用 npm pack 在外网上对 dependencies 中的所有包打包。
23
31
  const packPackage = async (packageName) => {
24
32
  const { stdout } = await execa('npm', ['pack', packageName, '--pack-destination=./node_modules_pack'])
25
33
  return stdout
26
34
  }
27
35
 
36
+ // npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。
28
37
  const publishPackage = async (packageName) => {
29
38
  const { stdout } = await execa('npm', ['publish', './node_modules_pack/' + packageName])
30
39
  return stdout
31
40
  }
32
41
 
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')
42
+ // 通过 nexus 的 components upload API 上传 npm 包,有时候会报 statusCode 500,暂不知道原因,重新上传即可。
43
+ const uploadPackage = async (packageName, options) => {
44
+ const { host, repository, username, password } = options
45
+ const form = new FormData()
46
+ form.append('npm.asset', createReadStream('./node_modules_pack/' + packageName))
47
+ // return form
48
+ form.submit({
49
+ hostname: new URL(host).hostname,
50
+ port: new URL(host).port,
51
+ path: `/service/rest/v1/components?repository=${repository}`,
52
+ auth: `${username}:${password}`
53
+ }, 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}`)
56
+ })
57
+ }
37
58
 
38
59
  program
39
60
  .command('pack')
@@ -43,19 +64,59 @@ program
43
64
  console.log(`Total ${packages.length} packages to be packed, please wait...`)
44
65
  mkdirSync('./node_modules_pack', { recursive: true })
45
66
  packages.forEach(packageName => {
46
- packPackage(packageName).then(stdout => console.log(stdout))
67
+ packPackage(packageName).then(stdout => {
68
+ console.log(stdout)
69
+ })
47
70
  })
48
71
  })
49
72
  })
50
73
 
74
+
51
75
  program
52
- .command('publish')
53
- .description('Publish all packed packages in ./node_modules_pack')
76
+ .command('upload')
77
+ .description('Upload all packed packages in ./node_modules_pack to Nexus self-hosted Registry')
54
78
  .action(() => {
55
- const packages = readdirSync('./node_modules_pack')
56
- packages.forEach(packageName => {
57
- publishPackage(packageName).then(stdout => console.log(stdout))
79
+ const questions = [
80
+ {
81
+ type: 'input',
82
+ name: 'host',
83
+ message: 'Please input your Nexus url host (eg: http://xx.xx.xx.xx:xxxx): '
84
+ },
85
+ {
86
+ type: 'input',
87
+ name: 'repository',
88
+ message: 'Please input your Nexus npm repository (eg: localNpm): ',
89
+ default: 'localNpm'
90
+ },
91
+ {
92
+ type: 'input',
93
+ name: 'username',
94
+ message: 'login to Nexus by username: ',
95
+ default: 'admin'
96
+ },
97
+ {
98
+ type: 'password',
99
+ name: 'password',
100
+ message: 'password: '
101
+ },
102
+ ]
103
+ inquirer.prompt(questions).then(options => {
104
+ const packages = readdirSync('./node_modules_pack')
105
+ console.log(`Total ${packages.length} packages to be uploaded, please wait...`)
106
+ packages.forEach(packageName => {
107
+ uploadPackage(packageName, options)
108
+ })
58
109
  })
59
110
  })
60
111
 
112
+ // program
113
+ // .command('publish')
114
+ // .description('Publish all packed packages in ./node_modules_pack')
115
+ // .action(() => {
116
+ // const packages = readdirSync('./node_modules_pack')
117
+ // packages.forEach(packageName => {
118
+ // publishPackage(packageName).then(stdout => console.log(stdout))
119
+ // })
120
+ // })
121
+
61
122
  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.6",
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
  }