|
@@ -1,5 +1,7 @@
|
|
-const { execSync } = require('child_process');
|
|
|
|
-const { isNullES } = require('js-fast-way')
|
|
|
|
|
|
+const {execSync} = require('child_process');
|
|
|
|
+const {isNullES} = require('js-fast-way')
|
|
|
|
+const fs = require('fs');
|
|
|
|
+const path = require('path');
|
|
|
|
|
|
//获取参数
|
|
//获取参数
|
|
const serverAddress = process.argv[2]
|
|
const serverAddress = process.argv[2]
|
|
@@ -13,107 +15,134 @@ const url = process.argv[7]
|
|
const [serverIp, port] = serverAddress.split(':');
|
|
const [serverIp, port] = serverAddress.split(':');
|
|
const sshPort = port || '22'; // 如果没有指定端口,默认使用22
|
|
const sshPort = port || '22'; // 如果没有指定端口,默认使用22
|
|
|
|
|
|
-// 这里填入您之前获取的主机密钥的 SHA256 指纹
|
|
|
|
-//plink -ssh root@192.168.0.109 -P 22 exit
|
|
|
|
-const hostKey = "SHA256:qS+DxUoW0NM1Nc//Dh0ebUHTB+MXMNTr+BF0njciwrU"
|
|
|
|
|
|
+// 获取主机密钥
|
|
|
|
+function getHostKey() {
|
|
|
|
+ const tempFile = path.join(__dirname, 'temp_hostkey');
|
|
|
|
+ try {
|
|
|
|
+ execSync(`echo y | plink -P ${sshPort} ${username}@${serverIp} exit`, {stdio: 'ignore'});
|
|
|
|
+ execSync(`plink -P ${sshPort} ${username}@${serverIp} exit >> ${tempFile} 2>&1`);
|
|
|
|
+ const output = fs.readFileSync(tempFile, 'utf8');
|
|
|
|
+ const match = output.match(/(?<=fingerprint is:).*?(?=\r?\n|$)/);
|
|
|
|
+ if (match) {
|
|
|
|
+ return match[0].trim();
|
|
|
|
+ }
|
|
|
|
+ throw new Error('Unable to extract host key');
|
|
|
|
+ } finally {
|
|
|
|
+ if (fs.existsSync(tempFile)) {
|
|
|
|
+ fs.unlinkSync(tempFile);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 获取主机密钥
|
|
|
|
+let hostKey = ''
|
|
|
|
+if (process.platform === 'win32') {
|
|
|
|
+ if (!commandExists('pscp') || !commandExists('plink')) {
|
|
|
|
+ console.log('putty 未安装,请先安装 putty');
|
|
|
|
+ process.exit(1);
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ hostKey = getHostKey();
|
|
|
|
+ console.log('获取到的主机密钥:', hostKey);
|
|
|
|
+}
|
|
|
|
|
|
// 检查命令是否可用
|
|
// 检查命令是否可用
|
|
function commandExists(command) {
|
|
function commandExists(command) {
|
|
- try {
|
|
|
|
- if (process.platform === 'win32') {
|
|
|
|
- execSync(`where ${command}`, { stdio: 'ignore' });
|
|
|
|
- } else {
|
|
|
|
- execSync(`which ${command}`, { stdio: 'ignore' });
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
- } catch (e) {
|
|
|
|
- return false;
|
|
|
|
|
|
+ try {
|
|
|
|
+ if (process.platform === 'win32') {
|
|
|
|
+ execSync(`where ${command}`, {stdio: 'ignore'});
|
|
|
|
+ } else {
|
|
|
|
+ execSync(`which ${command}`, {stdio: 'ignore'});
|
|
}
|
|
}
|
|
|
|
+ return true;
|
|
|
|
+ } catch (e) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// 执行命令并打印输出
|
|
// 执行命令并打印输出
|
|
function runCommand(command) {
|
|
function runCommand(command) {
|
|
- console.log(`执行命令: ${command}`);
|
|
|
|
- try {
|
|
|
|
- if (command.startsWith('sshpass') || command.startsWith('ssh')) {
|
|
|
|
- // 为SSH命令设置LC_ALL
|
|
|
|
- command = `LC_ALL=C ${command}`;
|
|
|
|
- }
|
|
|
|
- execSync(command, { stdio: 'inherit' });
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error(`命令执行失败: ${error.message}`);
|
|
|
|
- process.exit(1);
|
|
|
|
|
|
+ console.log(`执行命令: ${command}`);
|
|
|
|
+ try {
|
|
|
|
+ if (command.startsWith('sshpass') || command.startsWith('ssh')) {
|
|
|
|
+ // 为SSH命令设置LC_ALL
|
|
|
|
+ command = `LC_ALL=C ${command}`;
|
|
}
|
|
}
|
|
|
|
+ execSync(command, {stdio: 'inherit'});
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error(`命令执行失败: ${error.message}`);
|
|
|
|
+ process.exit(1);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// 上传到服务器
|
|
// 上传到服务器
|
|
function uploadServer() {
|
|
function uploadServer() {
|
|
- const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
|
|
|
|
- if (process.platform === 'win32') {
|
|
|
|
- // Windows
|
|
|
|
- runCommand(`pscp -P ${sshPort} -pw ${password} -hostkey "${hostKey}" ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
|
|
|
|
- runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
|
|
|
|
- } else {
|
|
|
|
- // Mac/Linux
|
|
|
|
- runCommand(`sshpass -p "${password}" scp -o StrictHostKeyChecking=no -P ${sshPort} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
|
|
|
|
- runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
|
|
|
|
- }
|
|
|
|
- console.log('编译打包后自动部署到服务器上完成');
|
|
|
|
- console.log(`服务器上的地址:${url}`);
|
|
|
|
|
|
+ const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
|
|
|
|
+ if (process.platform === 'win32') {
|
|
|
|
+ // Windows
|
|
|
|
+ runCommand(`pscp -P ${sshPort} -pw ${password} -hostkey "${hostKey}" ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
|
|
|
|
+ runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
|
|
|
|
+ } else {
|
|
|
|
+ // Mac/Linux
|
|
|
|
+ runCommand(`sshpass -p "${password}" scp -o StrictHostKeyChecking=no -P ${sshPort} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
|
|
|
|
+ runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
|
|
|
|
+ }
|
|
|
|
+ console.log('编译打包后自动部署到服务器上完成');
|
|
|
|
+ console.log(`服务器上的地址:${url}`);
|
|
}
|
|
}
|
|
|
|
|
|
// 删除 plugins 等目录
|
|
// 删除 plugins 等目录
|
|
function delPublic(name) {
|
|
function delPublic(name) {
|
|
- console.log(`准备移除 ${name} 等目录`);
|
|
|
|
- const sshCommand = `cd ${filePath} && rm -rf ${name}`;
|
|
|
|
- if (process.platform === 'win32') {
|
|
|
|
- // Windows
|
|
|
|
- runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
|
|
|
|
- } else {
|
|
|
|
- // Mac/Linux
|
|
|
|
- runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
|
|
|
|
- }
|
|
|
|
- console.log(`${name} 等目录移除完成`);
|
|
|
|
|
|
+ console.log(`准备移除 ${name} 等目录`);
|
|
|
|
+ const sshCommand = `cd ${filePath} && rm -rf ${name}`;
|
|
|
|
+ if (process.platform === 'win32') {
|
|
|
|
+ // Windows
|
|
|
|
+ runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
|
|
|
|
+ } else {
|
|
|
|
+ // Mac/Linux
|
|
|
|
+ runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
|
|
|
|
+ }
|
|
|
|
+ console.log(`${name} 等目录移除完成`);
|
|
}
|
|
}
|
|
|
|
|
|
//执行
|
|
//执行
|
|
function runCode() {
|
|
function runCode() {
|
|
- //参数是否为空
|
|
|
|
- if (serverIp === 'undefined' || username === 'undefined' || password === 'undefined' || fileName === 'undefined' || filePath === 'undefined') {
|
|
|
|
- console.log('参数异常,终止执行:', { serverIp, username, password, fileName, filePath });
|
|
|
|
- process.exit(1);
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- if (isNullES(serverIp) || isNullES(username) || isNullES(password) || isNullES(fileName) || isNullES(filePath)) {
|
|
|
|
- console.log('参数异常,终止执行:', { serverIp, username, password, fileName, filePath });
|
|
|
|
- process.exit(1);
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
|
|
+ //参数是否为空
|
|
|
|
+ if (serverIp === 'undefined' || username === 'undefined' || password === 'undefined' || fileName === 'undefined' || filePath === 'undefined') {
|
|
|
|
+ console.log('参数异常,终止执行:', {serverIp, username, password, fileName, filePath});
|
|
|
|
+ process.exit(1);
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if (isNullES(serverIp) || isNullES(username) || isNullES(password) || isNullES(fileName) || isNullES(filePath)) {
|
|
|
|
+ console.log('参数异常,终止执行:', {serverIp, username, password, fileName, filePath});
|
|
|
|
+ process.exit(1);
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
- //判断命令是否支持
|
|
|
|
- if (process.platform === 'win32') {
|
|
|
|
- if (!commandExists('pscp') || !commandExists('plink')) {
|
|
|
|
- console.log('putty 未安装,请先安装 putty');
|
|
|
|
- process.exit(1);
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- if (!commandExists('sshpass')) {
|
|
|
|
- console.log('sshpass 未安装,请先安装 sshpass');
|
|
|
|
- process.exit(1);
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
|
|
+ //判断命令是否支持
|
|
|
|
+ if (process.platform === 'win32') {
|
|
|
|
+ if (!commandExists('pscp') || !commandExists('plink')) {
|
|
|
|
+ console.log('putty 未安装,请先安装 putty');
|
|
|
|
+ process.exit(1);
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (!commandExists('sshpass')) {
|
|
|
|
+ console.log('sshpass 未安装,请先安装 sshpass');
|
|
|
|
+ process.exit(1);
|
|
|
|
+ return
|
|
}
|
|
}
|
|
- //删除一些目录
|
|
|
|
- delPublic('app');
|
|
|
|
- delPublic('cdn');
|
|
|
|
- delPublic('css');
|
|
|
|
- delPublic('img');
|
|
|
|
- delPublic('js');
|
|
|
|
- delPublic('svg');
|
|
|
|
- delPublic('util');
|
|
|
|
- //上传到服务器
|
|
|
|
- uploadServer();
|
|
|
|
|
|
+ }
|
|
|
|
+ //删除一些目录
|
|
|
|
+ delPublic('app');
|
|
|
|
+ delPublic('cdn');
|
|
|
|
+ delPublic('css');
|
|
|
|
+ delPublic('img');
|
|
|
|
+ delPublic('js');
|
|
|
|
+ delPublic('svg');
|
|
|
|
+ delPublic('util');
|
|
|
|
+ //上传到服务器
|
|
|
|
+ uploadServer();
|
|
}
|
|
}
|
|
|
|
|
|
runCode()
|
|
runCode()
|