@fanwang/pack-util 1.0.4 → 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.
- package/README.md +31 -3
- package/bin/index.js +95 -33
- package/package.json +10 -4
package/README.md
CHANGED
|
@@ -1,21 +1,45 @@
|
|
|
1
1
|
# pack-util
|
|
2
|
+
打包工具,可以将 ./node_modules 中安装的包遍历后打包,并放在 ./node_modules_pack 文件夹中,然后批量发布到 nexus 搭建的 npm 私服上。
|
|
2
3
|
Pack all installed package in ./node_modules, and publish to Nexus hosted Registry.
|
|
3
4
|
|
|
4
5
|
## Install
|
|
5
|
-
|
|
6
6
|
```sh
|
|
7
7
|
npm install -g @fanwang/pack-util
|
|
8
8
|
```
|
|
9
9
|
|
|
10
10
|
## Usage: pack-util [command]
|
|
11
|
-
|
|
11
|
+
如果使用控制台命令的时候提示电脑禁止运行脚本,用管理员身份运行 cmd 或者 vscode 等然后在命令行输入:
|
|
12
|
+
```
|
|
13
|
+
Set-ExecutionPolicy RemoteSigned
|
|
14
|
+
```
|
|
12
15
|
### Pack all installed package in ./node_modules
|
|
16
|
+
将所有 ./node_modules 中安装的包打包至 ./node_modules_pack
|
|
13
17
|
```sh
|
|
14
18
|
cd your_project
|
|
15
19
|
pack-util pack
|
|
16
20
|
```
|
|
17
21
|
|
|
18
|
-
###
|
|
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
|
+
|
|
19
43
|
copy node_modules_pack along with your project.
|
|
20
44
|
1. change registry
|
|
21
45
|
```sh
|
|
@@ -30,3 +54,7 @@ npm adduser -registry http://xx.xx.xx.xx:xxxx/repository/xxxx/
|
|
|
30
54
|
cd your_project
|
|
31
55
|
pack-util publish
|
|
32
56
|
```
|
|
57
|
+
~~
|
|
58
|
+
|
|
59
|
+
## TODO
|
|
60
|
+
- [] 实现打包和上传显示进度
|
package/bin/index.js
CHANGED
|
@@ -1,60 +1,122 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { execa } from 'execa'
|
|
4
|
-
import { mkdirSync, readdirSync } from 'fs'
|
|
5
|
-
import { Command } from 'commander'
|
|
3
|
+
import { execa } from 'execa'
|
|
4
|
+
import { mkdirSync, readdirSync, createReadStream } from 'fs'
|
|
5
|
+
import { Command } from 'commander'
|
|
6
|
+
import inquirer from 'inquirer'
|
|
7
|
+
import FormData from 'form-data'
|
|
6
8
|
|
|
7
|
-
const program = new Command()
|
|
9
|
+
const program = new Command()
|
|
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')
|
|
8
15
|
|
|
9
16
|
const getPackageNames = async () => {
|
|
10
|
-
const { stdout } = await execa('npm', ['list','--all'])
|
|
17
|
+
const { stdout } = await execa('npm', ['list', '--all'])
|
|
11
18
|
let packages = stdout.split('\n')
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
packages.splice(0, 1)//去掉首行路径字符串
|
|
20
|
+
return 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
|
+
}
|
|
29
|
+
|
|
30
|
+
// 利用 npm pack 在外网上对 dependencies 中的所有包打包。
|
|
31
|
+
const packPackage = async (packageName) => {
|
|
32
|
+
const { stdout } = await execa('npm', ['pack', packageName, '--pack-destination=./node_modules_pack'])
|
|
21
33
|
return stdout
|
|
22
34
|
}
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
// npm publish 会将解析打包的 package.json 中 publishConfig, 有的公共包如果设置了 { "registry": "https://registry.npmjs.org/" } , 会导致publish失败。
|
|
37
|
+
const publishPackage = async (packageName) => {
|
|
38
|
+
const { stdout } = await execa('npm', ['publish', './node_modules_pack/' + packageName])
|
|
26
39
|
return stdout
|
|
27
40
|
}
|
|
28
41
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
}
|
|
33
58
|
|
|
34
59
|
program
|
|
35
60
|
.command('pack')
|
|
36
61
|
.description('Pack all installed package in ./node_modules')
|
|
37
62
|
.action(() => {
|
|
38
|
-
getPackageNames().then(packages=>{
|
|
63
|
+
getPackageNames().then(packages => {
|
|
39
64
|
console.log(`Total ${packages.length} packages to be packed, please wait...`)
|
|
40
|
-
mkdirSync('./node_modules_pack',{recursive:true})
|
|
65
|
+
mkdirSync('./node_modules_pack', { recursive: true })
|
|
41
66
|
packages.forEach(packageName => {
|
|
42
|
-
packPackage(packageName).then(stdout=>
|
|
43
|
-
|
|
67
|
+
packPackage(packageName).then(stdout => {
|
|
68
|
+
console.log(stdout)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
44
71
|
})
|
|
45
72
|
})
|
|
46
73
|
|
|
74
|
+
|
|
47
75
|
program
|
|
48
|
-
.command('
|
|
49
|
-
.description('
|
|
50
|
-
.action(()=>{
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
76
|
+
.command('upload')
|
|
77
|
+
.description('Upload all packed packages in ./node_modules_pack to Nexus self-hosted Registry')
|
|
78
|
+
.action(() => {
|
|
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
|
+
})
|
|
56
109
|
})
|
|
57
110
|
})
|
|
58
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
|
+
// })
|
|
59
121
|
|
|
60
|
-
program.parse()
|
|
122
|
+
program.parse()
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fanwang/pack-util",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Pack all installed package in ./node_modules, and publish to Nexus self-hosted Registry.",
|
|
5
|
-
"bin":{
|
|
5
|
+
"bin": {
|
|
6
6
|
"pack-util": "./bin/index.js"
|
|
7
7
|
},
|
|
8
8
|
"publishConfig": {
|
|
@@ -19,11 +19,17 @@
|
|
|
19
19
|
"type": "git",
|
|
20
20
|
"url": "https://gitee.com/vampirefan/pack-util.git"
|
|
21
21
|
},
|
|
22
|
-
"keywords": [
|
|
22
|
+
"keywords": [
|
|
23
|
+
"npm",
|
|
24
|
+
"pack",
|
|
25
|
+
"publish"
|
|
26
|
+
],
|
|
23
27
|
"author": "fanwang",
|
|
24
28
|
"license": "MIT",
|
|
25
29
|
"dependencies": {
|
|
26
30
|
"commander": "^9.4.1",
|
|
27
|
-
"execa": "^6.1.0"
|
|
31
|
+
"execa": "^6.1.0",
|
|
32
|
+
"form-data": "^4.0.0",
|
|
33
|
+
"inquirer": "^9.1.3"
|
|
28
34
|
}
|
|
29
35
|
}
|