create_directory.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const fs = require('fs');
  2. const path = require('path');
  3. // 定义基础路径:src/app
  4. const basePath = path.join('src', 'app');
  5. // 要创建的文件夹结构
  6. const folderStructure = {
  7. assets: [],
  8. environments: [],
  9. modules: [],
  10. shared: {
  11. components: [],
  12. models: [],
  13. pipes: [],
  14. services: []
  15. }
  16. };
  17. /**
  18. * 递归创建目录
  19. * @param {string} currentPath 当前路径
  20. * @param {Object|Array} structure 文件夹结构
  21. */
  22. function createFolders(currentPath, structure) {
  23. if (Array.isArray(structure)) {
  24. // 如果是数组,表示当前层级只需创建自己
  25. if (!fs.existsSync(currentPath)) {
  26. fs.mkdirSync(currentPath, {recursive: true});
  27. console.log(`✅ Created directory: ${currentPath}`);
  28. }
  29. } else if (typeof structure === 'object') {
  30. // 如果是对象,遍历子项并继续递归
  31. for (const key in structure) {
  32. const newPath = path.join(currentPath, key);
  33. if (!fs.existsSync(newPath)) {
  34. fs.mkdirSync(newPath, {recursive: true});
  35. console.log(`✅ Created directory: ${newPath}`);
  36. }
  37. createFolders(newPath, structure[key]);
  38. }
  39. }
  40. }
  41. // 开始创建
  42. try {
  43. createFolders(basePath, folderStructure);
  44. console.log('\n🎉 文件夹结构已成功创建!');
  45. } catch (error) {
  46. console.error('\n❌ 创建文件夹结构失败:', error.message);
  47. }