初始化
This commit is contained in:
58
build/config/cli-helper.ts
Normal file
58
build/config/cli-helper.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/* eslint-disable */
|
||||
import process from 'node:process'
|
||||
import readline from 'node:readline'
|
||||
|
||||
function clearScreen() {
|
||||
const repeatCount = process.stdout.rows - 2
|
||||
const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : ''
|
||||
console.log(blank)
|
||||
readline.cursorTo(process.stdout, 0, 0)
|
||||
readline.clearScreenDown(process.stdout)
|
||||
}
|
||||
|
||||
function formatter(open: string, close: string, replace = open) {
|
||||
return (input: string) => {
|
||||
const string = `${input}`
|
||||
const index = string.indexOf(close, open.length)
|
||||
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close
|
||||
}
|
||||
}
|
||||
|
||||
function replaceClose(string: string, close: string, replace: string, index: number): string {
|
||||
const start = string.substring(0, index) + replace
|
||||
const end = string.substring(index + close.length)
|
||||
const nextIndex = end.indexOf(close)
|
||||
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
|
||||
}
|
||||
|
||||
function createColors() {
|
||||
return {
|
||||
bgBlack: formatter('\x1B[40m', '\x1B[49m'),
|
||||
bgBlue: formatter('\x1B[44m', '\x1B[49m'),
|
||||
bgCyan: formatter('\x1B[46m', '\x1B[49m'),
|
||||
bgGreen: formatter('\x1B[42m', '\x1B[49m'),
|
||||
bgMagenta: formatter('\x1B[45m', '\x1B[49m'),
|
||||
bgRed: formatter('\x1B[41m', '\x1B[49m', '\x1B[22m\x1B[1m'),
|
||||
bgWhite: formatter('\x1B[47m', '\x1B[49m'),
|
||||
bgYellow: formatter('\x1B[43m', '\x1B[49m'),
|
||||
black: formatter('\x1B[30m', '\x1B[39m'),
|
||||
blue: formatter('\x1B[34m', '\x1B[39m'),
|
||||
bold: formatter('\x1B[1m', '\x1B[22m', '\x1B[22m\x1B[1m'),
|
||||
cyan: formatter('\x1B[36m', '\x1B[39m'),
|
||||
dim: formatter('\x1B[2m', '\x1B[22m', '\x1B[22m\x1B[2m'),
|
||||
gray: formatter('\x1B[90m', '\x1B[39m'),
|
||||
green: formatter('\x1B[32m', '\x1B[39m'),
|
||||
hidden: formatter('\x1B[8m', '\x1B[28m'),
|
||||
inverse: formatter('\x1B[7m', '\x1B[27m'),
|
||||
italic: formatter('\x1B[3m', '\x1B[23m'),
|
||||
magenta: formatter('\x1B[35m', '\x1B[39m'),
|
||||
red: formatter('\x1B[31m', '\x1B[39m'),
|
||||
reset: (s: string) => `\x1B[0m${s}\x1B[0m`,
|
||||
strikethrough: formatter('\x1B[9m', '\x1B[29m'),
|
||||
underline: formatter('\x1B[4m', '\x1B[24m'),
|
||||
white: formatter('\x1B[37m', '\x1B[39m'),
|
||||
yellow: formatter('\x1B[33m', '\x1B[39m'),
|
||||
}
|
||||
}
|
||||
|
||||
export { clearScreen, createColors }
|
||||
2
build/config/index.ts
Normal file
2
build/config/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './proxy';
|
||||
export * from './time';
|
||||
57
build/config/proxy.ts
Normal file
57
build/config/proxy.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { ProxyOptions } from 'vite';
|
||||
import { bgRed, bgYellow, green, lightBlue } from 'kolorist';
|
||||
import { consola } from 'consola';
|
||||
import { createServiceConfig } from '../../src/utils/service';
|
||||
|
||||
/**
|
||||
* Set http proxy
|
||||
*
|
||||
* @param env - The current env
|
||||
* @param enable - If enable http proxy
|
||||
*/
|
||||
export function createViteProxy(env: Env.ImportMeta, enable: boolean) {
|
||||
const isEnableHttpProxy = enable && env.VITE_HTTP_PROXY === 'Y';
|
||||
|
||||
if (!isEnableHttpProxy) return undefined;
|
||||
|
||||
const isEnableProxyLog = env.VITE_PROXY_LOG === 'Y';
|
||||
|
||||
const { baseURL, proxyPattern, other } = createServiceConfig(env);
|
||||
|
||||
const proxy: Record<string, ProxyOptions> = createProxyItem({ baseURL, proxyPattern }, isEnableProxyLog);
|
||||
|
||||
other.forEach(item => {
|
||||
Object.assign(proxy, createProxyItem(item, isEnableProxyLog));
|
||||
});
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
function createProxyItem(item: App.Service.ServiceConfigItem, enableLog: boolean) {
|
||||
const proxy: Record<string, ProxyOptions> = {};
|
||||
|
||||
proxy[item.proxyPattern] = {
|
||||
target: item.baseURL,
|
||||
changeOrigin: true,
|
||||
configure: (_proxy, options) => {
|
||||
_proxy.on('proxyReq', (_proxyReq, req, _res) => {
|
||||
if (!enableLog) return;
|
||||
|
||||
const requestUrl = `${lightBlue('[proxy url]')}: ${bgYellow(` ${req.method} `)} ${green(
|
||||
`${item.proxyPattern}${req.url}`
|
||||
)}`;
|
||||
|
||||
const proxyUrl = `${lightBlue('[real request url]')}: ${green(`${options.target}${req.url}`)}`;
|
||||
|
||||
consola.log(`${requestUrl}\n${proxyUrl}`);
|
||||
});
|
||||
_proxy.on('error', (_err, req, _res) => {
|
||||
if (!enableLog) return;
|
||||
consola.log(bgRed(`Error: ${req.method} `), green(`${options.target}${req.url}`));
|
||||
});
|
||||
},
|
||||
rewrite: path => path.replace(new RegExp(`^${item.proxyPattern}`), '')
|
||||
};
|
||||
|
||||
return proxy;
|
||||
}
|
||||
12
build/config/time.ts
Normal file
12
build/config/time.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
import timezone from 'dayjs/plugin/timezone';
|
||||
|
||||
export function getBuildTime() {
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
const buildTime = dayjs.tz(Date.now(), 'Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss');
|
||||
|
||||
return buildTime;
|
||||
}
|
||||
Reference in New Issue
Block a user