前端转vue
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const fs = require('fs')
  2. const path = require('path')
  3. const colors = require('colors-console');
  4. export function copyFolder(from, to) {
  5. const fromPath = path.resolve(from)
  6. const toPath = path.resolve(to)
  7. fs.access(toPath, function (err) {
  8. if (err) {
  9. fs.mkdirSync(toPath)
  10. }
  11. })
  12. fs.readdir(fromPath, function (err, paths) {
  13. if (err) {
  14. console.log(err)
  15. return
  16. }
  17. paths.forEach(function (item) {
  18. const newFromPath = fromPath + '/' + item
  19. const newToPath = path.resolve(toPath + '/' + item)
  20. fs.stat(newFromPath, function (err, stat) {
  21. if (err) return
  22. if (stat.isFile()) {
  23. copyFile(newFromPath, newToPath)
  24. }
  25. if (stat.isDirectory()) {
  26. copyFolder(newFromPath, newToPath)
  27. }
  28. })
  29. })
  30. })
  31. }
  32. export function copyFile(from, to) {
  33. fs.copyFileSync(from, to)
  34. }
  35. export const pathResolve = (pathStr: string) => {
  36. return path.resolve(__dirname, pathStr);
  37. };
  38. export const log = {
  39. error: (txt) => console.error(colors('red', txt)),
  40. warning: (txt) => console.error(colors('yellow', txt)),
  41. info: (txt) => console.error(colors('cyan', txt)),
  42. default: (txt) => console.error(colors('bright', txt)),
  43. success: (txt) => console.error(colors('green', txt)),
  44. }
  45. export const templatePath = pathResolve('../template')
  46. export const disPath = (moduleName) => pathResolve('../../src/' + moduleName)
  47. export function deleteFolder(url) {
  48. let files = [];
  49. /**
  50. * 判断给定的路径是否存在
  51. */
  52. if (fs.existsSync(url)) {
  53. /**
  54. * 返回文件和子目录的数组
  55. */
  56. files = fs.readdirSync(url);
  57. files.forEach(function (file, index) {
  58. const curPath = path.join(url, file);
  59. log.error(curPath);
  60. /**
  61. * fs.statSync同步读取文件夹文件,如果是文件夹,在重复触发函数
  62. */
  63. if (fs.statSync(curPath).isDirectory()) { // recurse
  64. deleteFolder(curPath);
  65. } else {
  66. fs.unlinkSync(curPath);
  67. }
  68. });
  69. /**
  70. * 清除文件夹
  71. */
  72. fs.rmdirSync(url);
  73. } else {
  74. log.error("要删除的路径不存在,请给出正确的路径!");
  75. }
  76. }
  77. export const firstLetterUpperCase = (str) => {
  78. str = str.toString()
  79. return str.replace(str[0], str[0].toUpperCase());
  80. }
  81. export function modifyIndexVue(type, moduleName) {
  82. const filePath = disPath(type + '/' + moduleName + '/index.vue')
  83. fs.readFile(filePath, 'utf8', function (err, data) {
  84. if (err) throw err;
  85. data = data.replace(/\${class}/g, firstLetterUpperCase(moduleName))
  86. fs.writeFileSync(filePath, data, 'utf8', (err) => {
  87. if (err) throw err;
  88. console.log('success done');
  89. });
  90. });
  91. }