123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- const { execSync } = require('child_process');
- const { isNullES } = require('js-fast-way')
- const args = process.argv[2]
- const serverAddress = process.argv[3]
- const username = process.argv[4]
- const password = process.argv[5]
- const fileName = process.argv[6]
- const filePath = process.argv[7]
- const url = process.argv[8]
- const [serverIp, port] = serverAddress.split(':');
- const sshPort = port || '22';
- const hostKey = "SHA256:qS+DxUoW0NM1Nc//Dh0ebUHTB+MXMNTr+BF0njciwrU"
- function commandExists(command) {
- try {
- 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')) {
-
- 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') {
-
- 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 {
-
- 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}`);
- }
- function delPublic() {
- console.log('准备移除 plugins 等目录');
- const sshCommand = `cd ${filePath} && rm -rf plugins`;
- if (process.platform === 'win32') {
-
- runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw '${password}' -hostkey "${hostKey}" -batch "${sshCommand}"`);
- } else {
-
- runCommand(`sshpass -p '${password}' ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
- }
- console.log('plugins 等目录移除完成');
- }
- function runCode() {
-
- if (args === 'undefined' || serverIp === 'undefined' || username === 'undefined' || password === 'undefined' || fileName === 'undefined' || filePath === 'undefined') {
- console.log('参数异常,终止执行:', { args,serverIp, username, password, fileName, filePath });
- process.exit(1);
- return
- }
- if (isNullES(args) || isNullES(serverIp) || isNullES(username) || isNullES(password) || isNullES(fileName) || isNullES(filePath)) {
- console.log('参数异常,终止执行:', { args,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 (args === 'all') {
- delPublic();
- }
-
- uploadServer();
- }
- runCode()
|