Files
hudi-service/bin/library.js

165 lines
5.4 KiB
JavaScript

import {$, fetch, fs, glob, os, path, spinner, syncProcessCwd, usePowerShell} from 'zx'
import {fileSize, isEqual, trim} from "licia";
import md5file from 'md5-file'
syncProcessCwd(true)
if (isEqual(os.platform(), 'win32')) {
usePowerShell()
}
const maven_setting = path.join(os.homedir(), '.m2', 'settings-nas.xml')
const upload_url = 'http://132.126.207.124:36800'
const upload_username = 'AxhEbscwsJDbYMH2'
const upload_password = 'cYxg3b4PtWoVD5SjFayWxtnSVsjzRsg4'
/**
* 时间戳转自然语言
*
* @param timestamp 时间戳
* @returns {string} 自然语言描述的时间
*/
const millisecondToString = (timestamp) => {
const totalSeconds = Math.floor(parseFloat(timestamp) / 1000)
if (isNaN(totalSeconds) || totalSeconds < 0) {
return "0秒";
}
const days = Math.floor(totalSeconds / 86400)
let remaining = totalSeconds % 86400
const hours = Math.floor(remaining / 3600)
remaining %= 3600
const minutes = Math.floor(remaining / 60)
const seconds = remaining % 60
const parts = []
if (days > 0) parts.push(`${days}`)
if (days > 0 || hours > 0) parts.push(`${hours}小时`)
if (days > 0 || hours > 0 || minutes > 0) parts.push(`${minutes}分钟`)
parts.push(`${seconds}`)
return parts.join('')
}
const dotBuildPath = () => `.build`
const modifiedDataPath = () => `${dotBuildPath()}/modified_time.json`
const readModifiedTimeData = async () => {
if (!fs.existsSync(dotBuildPath())) {
fs.mkdirSync(dotBuildPath(), {recursive: true})
}
if (!(await fs.exists(modifiedDataPath()))) {
fs.writeFileSync(modifiedDataPath(), '{}')
}
return JSON.parse(await fs.readFile(modifiedDataPath(), 'utf-8'))
}
const updateModifiedTimeData = (data) => {
fs.writeFileSync(modifiedDataPath(), JSON.stringify(data, null, 2))
}
const isModified = async (target) => {
if (!target || !(await fs.exists(target))) {
throw new Error("Target 不存在")
}
let stat = fs.statSync(target)
let currentModifiedTime = stat.mtimeMs
let lastModifiedTime = (await readModifiedTimeData())[target]
return !(lastModifiedTime && isEqual(currentModifiedTime, lastModifiedTime));
}
const updateModifiedTime = async (target) => {
if (!target || !(await fs.exists(target))) {
throw new Error("Target 不存在")
}
let stat = fs.statSync(target)
let currentModifiedTime = stat.mtimeMs
let modifiedTimeData = await readModifiedTimeData()
modifiedTimeData[target] = currentModifiedTime
updateModifiedTimeData(modifiedTimeData)
}
export const run_deploy = async (project) => {
if (!(await isModified(project))) {
console.log(`✅ Skip deploy ${project}`)
return
}
let output = await spinner(
`Deploying project ${project}`,
() => $`mvn -pl ${project} clean deploy -D skipTests -s ${maven_setting}`
)
console.log(`✅ Finished deploy ${project} (${millisecondToString(output['duration'])})`)
await updateModifiedTime(project)
}
export const run_deploy_root = async () => {
if (!(await isModified(pom.xml))) {
console.log(`✅ Skip deploy root`)
return
}
let output = await spinner(
`Deploying root`,
() => $`mvn clean deploy -N -D skipTests -s ${maven_setting}`
)
console.log(`✅ Finished deploy root (${millisecondToString(output['duration'])})`)
await updateModifiedTime(`pom.xml`)
}
export const run_deploy_batch = async (projects) => {
for (const project of projects) {
await run_deploy(project)
}
}
export const run_package = async (project, profile = 'b2b12') => {
let output = await spinner(
`Packaging project ${project}${isEqual(profile, 'b2b12') ? '' : ` ${profile}`}`,
() => $`mvn -pl ${project} clean package -D skipTests -P ${profile} -s ${maven_setting}`
)
console.log(`✅ Finished package ${project}${isEqual(profile, 'b2b12') ? '' : ` ${profile}`} (${millisecondToString(output['duration'])})`)
}
export const run_package_batch = async (projects) => {
for (const project of projects) {
await run_package(project)
}
}
export const upload = async (file_path) => {
let start = new Date().getTime()
let basename = path.basename(file_path)
let response = await spinner(
`Uploading project ${file_path}`,
() => fetch(`${upload_url}/file/upload/${basename}`, {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'Authorization': `Basic ${Buffer.from(`${upload_username}:${upload_password}`).toString('base64')}`,
},
body: fs.createReadStream(file_path),
duplex: 'half',
})
)
if (!isEqual(response.status, 200)) {
throw response
}
console.log(`✅ Finished upload ${file_path} (${millisecondToString((new Date().getTime()) - start)})`)
console.log(`📘 Uploaded ${fileSize(fs.statSync(file_path).size)}`)
console.log(`📘 MD5 ${md5file.sync(file_path)}`)
console.log(`📘 Download curl http://AxhEbscwsJDbYMH2:cYxg3b4PtWoVD5SjFayWxtnSVsjzRsg4@132.126.207.124:36800/file/download/${basename} -o ${basename}`)
fs.rmSync(file_path)
}
export const run_upload = async (pattern) => {
for (let p of glob.sync(pattern)) {
await upload(path.join(trim($.sync`pwd`.text()), p))
}
}
export const run_upload_normal = async (project) => {
await run_upload(`${project}/target/${project}-1.0.0-SNAPSHOT.jar`)
}