common.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. const path = require('node:path')
  3. const fs = require('fs')
  4. const Axios = require('axios')
  5. const axios = Axios.create({
  6. timeout: 30000,
  7. baseURL: 'https://swagger.auyen.com',
  8. })
  9. /** 色彩打印 */
  10. const cl = {
  11. colors: {
  12. reset: '\x1b[0m',
  13. red: '\x1b[31m',
  14. green: '\x1b[32m',
  15. yellow: '\x1b[33m',
  16. },
  17. /** 成功消息 */
  18. logs() {
  19. console.log(`${this.colors.green}%s${this.colors.reset}`, ...arguments)
  20. },
  21. /** 错误消息 */
  22. loge() {
  23. console.log(`${this.colors.red}%s${this.colors.reset}`, ...arguments)
  24. },
  25. /** 警告消息 */
  26. logw() {
  27. console.log(`${this.colors.yellow}%s${this.colors.reset}`, ...arguments)
  28. },
  29. }
  30. const request = {
  31. get(path) {
  32. return axios(path).then((res) => res.data)
  33. },
  34. }
  35. const files = {
  36. path,
  37. basename(filePath, extname = true) {
  38. let rv = path.basename(filePath)
  39. if (!extname) {
  40. rv = rv.replace(/\..*$/, '')
  41. }
  42. return rv
  43. },
  44. /**
  45. * 遍历指定目录下的所有文件
  46. * @param {*} dir
  47. */
  48. getAllFile(dir) {
  49. const res = []
  50. function traverse(dir) {
  51. fs.readdirSync(dir).forEach((file) => {
  52. const pathname = path.join(dir, file)
  53. if (fs.statSync(pathname).isDirectory()) {
  54. traverse(pathname)
  55. } else {
  56. res.push(pathname)
  57. }
  58. })
  59. }
  60. traverse(dir)
  61. return res
  62. },
  63. getPath(relPath) {
  64. return path.resolve(process.cwd(), relPath)
  65. },
  66. getIFPath(fileName) {
  67. const rv = {
  68. schemaPath: this.getPath(`src/types/schemas/${fileName}.d.ts`),
  69. webApiPath: this.getPath(`src/utils/config/interFaces/${fileName}.ts`),
  70. }
  71. return rv
  72. },
  73. write(path, content) {
  74. try {
  75. fs.writeFileSync(path, content, 'utf-8')
  76. } catch {}
  77. },
  78. read(path) {
  79. try {
  80. return fs.readFileSync(path, 'utf-8')
  81. } catch {}
  82. },
  83. }
  84. module.exports = {
  85. cl,
  86. request,
  87. files,
  88. }