123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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 username = process.argv[3]
- const password = process.argv[4]
- const fileName = process.argv[5]
- const filePath = process.argv[6]
- const url = process.argv[7]
- // 解析服务器地址和端口
- const [serverIp, port] = serverAddress.split(':');
- const sshPort = port || '22'; // 如果没有指定端口,默认使用22
- // 获取主机密钥
- 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) {
- 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) {
- 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() {
- 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 等目录
- 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} 等目录移除完成`);
- }
- //执行
- 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 (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();
- }
- runCode()
|