index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * @copyright chuzhixin 1204505056@qq.com
  3. * @description 导入所有 controller 模块,npm run serve时在node环境中自动输出controller文件夹下Mock接口,请勿修改。
  4. */
  5. import { handleMockArray } from "./utils";
  6. import chalk from "chalk";
  7. import fs from "fs";
  8. import { baseURL, devPort, httpRequestFile } from "../src/config/settings";
  9. const mocks = [];
  10. const mockArray = handleMockArray();
  11. if (httpRequestFile) {
  12. fs.writeFile("./http/mock.http", "", {}, function (err) {
  13. if (err) throw err;
  14. });
  15. }
  16. mockArray.forEach(async (item) => {
  17. const obj = require(item).default;
  18. await mocks.push(...obj);
  19. if (httpRequestFile) {
  20. obj.forEach((item) => {
  21. fs.appendFile(
  22. "./http/mock.http",
  23. `\r\n###${item.url}###\r\POST http://localhost:${devPort}/${baseURL}${item.url}\r\nContent-Type: application/x-www-form-urlencoded\r\n###\r\n`,
  24. (error) => {
  25. if (error)
  26. return chalk.red(`\n > 追加HTTP Request失败${error.message}`);
  27. }
  28. );
  29. });
  30. }
  31. });
  32. export default mocks;