mirror of
https://github.com/cool-team-official/cool-admin-vue.git
synced 2024-11-01 22:20:27 +08:00
71 lines
1.2 KiB
TypeScript
71 lines
1.2 KiB
TypeScript
|
import fs from "fs";
|
||
|
import { join } from "path";
|
||
|
|
||
|
// 首字母大写
|
||
|
export function firstUpperCase(value: string): string {
|
||
|
return value.replace(/\b(\w)(\w*)/g, function ($0, $1, $2) {
|
||
|
return $1.toUpperCase() + $2;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 横杠转驼峰
|
||
|
export function toCamel(str: string): string {
|
||
|
return str.replace(/([^-])(?:-+([^-]))/g, function ($0, $1, $2) {
|
||
|
return $1 + $2.toUpperCase();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 创建目录
|
||
|
export function createDir(path: string) {
|
||
|
if (!fs.existsSync(path)) fs.mkdirSync(path);
|
||
|
}
|
||
|
|
||
|
// 读取文件
|
||
|
export function readFile(name: string) {
|
||
|
try {
|
||
|
return fs.readFileSync(name, "utf8");
|
||
|
} catch (e) {}
|
||
|
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
// 解析body
|
||
|
export function parseJson(req: any): Promise<any> {
|
||
|
return new Promise((resolve) => {
|
||
|
let d = "";
|
||
|
req.on("data", function (chunk: Buffer) {
|
||
|
d += chunk;
|
||
|
});
|
||
|
req.on("end", function () {
|
||
|
try {
|
||
|
resolve(JSON.parse(d));
|
||
|
} catch {
|
||
|
resolve({});
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 深度创建目录
|
||
|
export function mkdirs(path: string) {
|
||
|
const arr = path.split("/");
|
||
|
let p = "";
|
||
|
|
||
|
arr.forEach((e) => {
|
||
|
const t = join(p, e);
|
||
|
|
||
|
try {
|
||
|
fs.statSync(t);
|
||
|
} catch (err) {
|
||
|
try {
|
||
|
fs.mkdirSync(t);
|
||
|
} catch (error) {
|
||
|
console.error(error);
|
||
|
}
|
||
|
}
|
||
|
p = t;
|
||
|
});
|
||
|
|
||
|
return p;
|
||
|
}
|