server-deploy-cli
Version:
connect server with ssh2-sftp-client, and publish local files to remote server or delete some files from the remote server or other operations you wanna do
63 lines (58 loc) • 2.52 kB
JavaScript
/*
* @Author: famous
* @Date: 2022-10-14 10:44:55
* @Last Modified by: famous
* @Last Modified time: 2023-07-11 16:52:04
*/
// const path = require('path')
// const fs = require('fs')
// const extend = require('./extend')
// const defaultConfig = require('./defaultConfig')
import path from 'path'
import fs from 'fs'
import os from 'os'
import extend from './extend.js'
import defaultConfig from './defaultConfig.js'
import Log from './log.js'
export const getAbsPath = relativePath => {
const deployExecDir = fs.realpathSync(process.cwd()) //命令执行时所在目录的路径
return path.resolve(deployExecDir, relativePath)
}
export const getConfig = async (deployConfigFileRelativePath = 'deploy.config.js') => {
/**
* @deployConfigFileRelativePath
* 服务器连接、项目对应远程 nginx root 目录及本地打包目录等相关配置文件
* 默认项目根目录同级的 deploy.config.js 文件为此配置文件
* 也可通过命令参数传入(项目根目录的相对路径),如: deploy-cli -c [deployConfigFileRelativePath]
*/
const userConfigPath = getAbsPath(deployConfigFileRelativePath)
let userConfig
try {
const { default: config } = await import(userConfigPath)
userConfig = config
} catch (error) {
const systemType = os.type()
const isWindows = systemType.indexOf('Windows') > -1
/**
* Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader.
* On Windows, absolute paths must be valid file:// URLs. Received protocol 'g:'
*
* 以上报错是node在Windows系统中的bug,所以手动加上 file:///
*/
if (error.toString().startsWith('Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]:') && isWindows) {
const userConfigEnvPath = `file:///${ userConfigPath }`
const { default: config } = await import(userConfigEnvPath)
userConfig = config
} else {
Log.error('%s', [error])
return null
}
}
// const { default: userConfig } = await import(userConfigEnvPath)
let servers = [].concat(userConfig.server)
servers = servers.map( server => {
return extend(true, {}, defaultConfig.server, server)
})
const deployConfig = Object.assign({}, defaultConfig, userConfig, {server: servers})
return deployConfig
}