公司内部使用的C++/Qt的版本固定,因此我们只需要管理编译好的库文件(*.lib, *.dll)以及头文件就好了。

我们使用npm来管理它们,可以使用npm的诸多功能,例如版本依赖、用户管理等。我们使用verdaccio搭建自己的npm镜像,可以方便在网页上查看目前公司已有的包。

搭建公司私有npm镜像

1
2
$ docker pull verdaccio/verdaccio
$ docker run -it --rm --name verdaccio -d -p 4873:4873 verdaccio/verdaccio

添加用户

1
$ npm adduser --registry http://x.x.x.x:4873

package.json

package.json中写入要打包的头文件和库文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"name": "@gw/liclib",
"version": "1.0.0",
"author": "leon.li",
"files": [
"include/gw/liclib/*",
"dist/debug/gw-liclibd.dll",
"dist/debug/gw-liclibd.lib",
"dist/debug/gw-liclibd.pdb",
"dist/debug/gw-hardware-key-getter.exe",
"dist/release/gw-liclib.dll",
"dist/release/gw-liclib.lib",
"dist/release/gw-liclib.pdb",
"dist/release/gw-hardware-key-getter.exe"
]
}

发布包

1
npm publish --registry http://x.x.x.x:4873/

若某个库需要在多个平台发布,可以使用多个package.json,目录结构:

1
2
3
4
5
6
7
8
9
10
11
|--@gw\
| |--liclib\
| | |--dist\
| | |--include\
| | |--package.json
| | |--publish.bat
| |--liclib-linux\
| | |--dist\
| | |--include\
| | |--package.json
| | |--publish.sh

在publish.bat/publish.sh脚本中,将需要的文件拷贝到当前目录,再使用npm publish发布

1
2
3
4
5
6
7
8
9
10
@echo off
set include_tmp_dir=%~dp0include\
set dist_tmp_dir=%~dp0dist\
set root_dir=%~dp0..\..\
if exist "%include_tmp_dir%" rmdir /S /Q "%include_tmp_dir%"
if exist "%dist_tmp_dir%" rmdir /S /Q "%dist_tmp_dir%"
xcopy "%root_dir%include\" "%include_tmp_dir%" /E /Y
xcopy "%root_dir%dist\" "%dist_tmp_dir%" /E /Y
xcopy "%root_dir%README.md" "%~dp0" /Y
npm publish --registry http://x.x.x.x:4873/

取消发布

1
$ npm unpublish @gw/liclib --registry http://x.x.x.x:4873/ --force

使用包

在使用项目的根目录下添加一个package.json,添加依赖项

1
2
3
4
5
6
7
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"@gw/liclib": "^1.0.0"
}
}

编写一个脚本执行npm包的下载,以及文件的拷贝,node_modules文件夹的删除。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@echo off
set gw_libs=liclib utillib

if exist "node_modules" rmdir /S /Q "node_modules"

call npm install --registry http://x.x.x.x:4873/

for %%a in (%gw_libs%) do (
echo %%a
xcopy node_modules\@gw\%%a\include\ include\ /E /Y
xcopy node_modules\@gw\%%a\dist\ dist\ /E /Y
)

if exist "node_modules" rmdir /S /Q "node_modules"

Reference