自动上传打包后文件
准备内容
- 服务器ip 43.139.5.111
- 服务器用户名 root
- 服务器密码 xxxxxx
- 服务静态资源目录路径 /usr/local/nginx/html/
项目依赖
shell
npm i sftp-uploader -D
创建upload.js文件放置到根目录
js
const sftp = require('sftp-uploader');
const path = require('path');
const fs = require('fs');
const { exec } = require('child_process');
const uploader = new sftp({
// (常规不用改)本地目录
dir: resolve("./dist"),
// (需更改)服务器ip
host: "43.139.5.111",
// (常规不用改)端口号
port: "22",
// (常规不用改)服务器用户名
username: "root",
// (需更改)服务器密码
password: "xxxxxx", // 密码
// (需更改)服务器目录
url: "/usr/local/nginx/html/",
// (常规不用改)延迟上传时间(毫秒),解决部分项目会触发多次打包完成的问题
delay: 60,
});
uploader.upload();
package.json配置
json
"scripts": {
...,
"upload": "node upload.js",
npm run build 指令根据打包指令自行修改常见如npm run build:prod npm run build:dev
"build:upload": "npm run build && npm run upload",
},
使用方式
一键打包发布
shell
npm run build:upload
手动上传
shell
npm run build #用户可能是build或build:prod等其他指令自行调整
npm run upload