| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /* eslint-disable @typescript-eslint/no-var-requires */
- const path = require('node:path')
- const fs = require('fs')
- const Axios = require('axios')
- const axios = Axios.create({
- timeout: 30000,
- baseURL: 'https://swagger.auyen.com',
- })
- /** 色彩打印 */
- const cl = {
- colors: {
- reset: '\x1b[0m',
- red: '\x1b[31m',
- green: '\x1b[32m',
- yellow: '\x1b[33m',
- },
- /** 成功消息 */
- logs() {
- console.log(`${this.colors.green}%s${this.colors.reset}`, ...arguments)
- },
- /** 错误消息 */
- loge() {
- console.log(`${this.colors.red}%s${this.colors.reset}`, ...arguments)
- },
- /** 警告消息 */
- logw() {
- console.log(`${this.colors.yellow}%s${this.colors.reset}`, ...arguments)
- },
- }
- const request = {
- get(path) {
- return axios(path).then((res) => res.data)
- },
- }
- const files = {
- path,
- basename(filePath, extname = true) {
- let rv = path.basename(filePath)
- if (!extname) {
- rv = rv.replace(/\..*$/, '')
- }
- return rv
- },
- /**
- * 遍历指定目录下的所有文件
- * @param {*} dir
- */
- getAllFile(dir) {
- const res = []
- function traverse(dir) {
- fs.readdirSync(dir).forEach((file) => {
- const pathname = path.join(dir, file)
- if (fs.statSync(pathname).isDirectory()) {
- traverse(pathname)
- } else {
- res.push(pathname)
- }
- })
- }
- traverse(dir)
- return res
- },
- getPath(relPath) {
- return path.resolve(process.cwd(), relPath)
- },
- getIFPath(fileName) {
- const rv = {
- schemaPath: this.getPath(`src/types/schemas/${fileName}.d.ts`),
- webApiPath: this.getPath(`src/utils/config/interFaces/${fileName}.ts`),
- }
- return rv
- },
- write(path, content) {
- try {
- fs.writeFileSync(path, content, 'utf-8')
- } catch {}
- },
- read(path) {
- try {
- return fs.readFileSync(path, 'utf-8')
- } catch {}
- },
- }
- module.exports = {
- cl,
- request,
- files,
- }
|