You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB
TypeScript

import process from 'node:process'
import fs from 'node:fs'
import cp from 'node:child_process'
import chalk from 'chalk'
import { getNow } from 'easy-fns-ts/dist/lib'
import pkg from '../../package.json'
function title(stage: string) {
return chalk.magenta.bgBlack(
`[${pkg.name.toUpperCase()}] - [${getNow()}] - [${stage}]`,
)
}
export function buildUtilsLog(msg: string, stage = 'log') {
console.log(`${title(stage)}: ${msg}`)
}
export function buildUtilsWarn(warns: string) {
console.warn(chalk.yellow(`[${pkg.name.toUpperCase()} Warning] - [${getNow()}] \n ${warns}`))
}
export function writeIntoLog(title: string, command: string, path: string) {
const prefix = (msg: string, emoji: string) => `
/**
* ==============================================
* ==============================================
* ${title} - ${msg} - ${emoji} - ${getNow()}
* ==============================================
* ==============================================
*/
`
const log_file = fs.createWriteStream(path, {
flags: 'w',
})
const start = new Date().getTime()
cp.exec(command, (error, stdout, stderr) => {
prefix(`Excuting command: ${command}`, '⚡⚡⚡⚡⚡')
const end = new Date().getTime()
const cost = new Date(end - start).getSeconds()
buildUtilsLog(chalk.blue(`${title} done in ${cost}s`))
if (error) {
process.exitCode = 1
log_file.write(
prefix('Error', '😈😈😈😈😈') + JSON.stringify(error),
() => {
// // recover icon file if failed
// cp.execSync('npm run postbuild')
buildUtilsLog(
chalk.red.bgBlack(`${title} Failed, see more in ${path}`),
)
},
)
}
else {
buildUtilsLog(chalk.green.bgBlack(`${title} Successfully! ✨✨✨✨✨ `))
}
if (stdout) {
log_file.write(prefix('Std Out', '✨✨✨✨✨') + stdout, () => {
buildUtilsLog(
chalk.magenta.bgBlack(`${title} Std out, see more in ${path}`),
)
})
}
if (stderr) {
log_file.write(prefix('Std Err', '💊💊💊💊💊') + stderr, () => {
buildUtilsLog(
chalk.yellow.bgBlack(`${title} Std Error, see more in ${path}`),
)
})
}
})
}