generateSCSSMap.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import path from 'path';
  2. import fs from "fs-extra";
  3. import { set } from 'lodash';
  4. const lodash = { set };
  5. const generateComponentsScssMap = (foundationPath: string, iconPath?: string) => {
  6. const foundationComponentList = fs.readdirSync(foundationPath);
  7. const componentScssMap: { [componentName: string]: { [scssFileName: string]: string } } = {};
  8. foundationComponentList.forEach(fileName => {
  9. const fileAbsolutePath = path.join(foundationPath, fileName);
  10. if (fs.existsSync(fileAbsolutePath) && fs.statSync(fileAbsolutePath).isDirectory()) {
  11. //in component folder
  12. const componentPath = fileAbsolutePath;
  13. const scssFileList = fs.readdirSync(componentPath).filter((fileName) => fileName.endsWith('.scss'));
  14. scssFileList.forEach(scssFileName => {
  15. let scssRaw = fs.readFileSync(path.join(componentPath, scssFileName), { encoding: 'utf-8' });
  16. scssRaw = `\n\n//----${fileName}/${scssFileName} start-----\n` + scssRaw + `\n\n//----${fileName}/${scssFileName} end-----\n`;
  17. lodash.set(componentScssMap, [fileName, scssFileName], scssRaw);
  18. });
  19. }
  20. });
  21. if (iconPath) {
  22. //for react icon
  23. const stylePath = path.join(iconPath, 'src', 'styles');
  24. const scssFileList = fs.readdirSync(stylePath).filter((fileName) => fileName.endsWith('.scss'));
  25. scssFileList.forEach(scssFileName => {
  26. let scssRaw = fs.readFileSync(path.join(stylePath, scssFileName), { encoding: 'utf-8' });
  27. scssRaw = `\n\n//----${stylePath}/${scssFileName} start-----\n` + scssRaw + `\n\n//----${stylePath}/${scssFileName} end-----\n`;
  28. lodash.set(componentScssMap, ['icons', scssFileName], scssRaw);
  29. });
  30. }
  31. return componentScssMap;
  32. };
  33. const generateThemeScssMap = (themePath: string) => {
  34. const fileList = ['_font.scss', '_palette.scss', 'global.scss', 'index.scss', 'local.scss', 'mixin.scss', 'variables.scss'] as const;
  35. const themeScssMap: { [key in typeof fileList[number]]?: string } = {};
  36. for (const fileName of fileList) {
  37. const scssAbsolutePath = path.join(themePath, 'scss', fileName);
  38. if (fs.existsSync(scssAbsolutePath)) {
  39. //in theme folder
  40. themeScssMap[fileName] = fs.readFileSync(scssAbsolutePath, { encoding: "utf8" });
  41. }
  42. }
  43. // console.log(themeScssMap)
  44. return themeScssMap;
  45. };
  46. const generateScssMap = (foundationPath: string, themePath: string, iconPath: string) => {
  47. return {
  48. components: generateComponentsScssMap(foundationPath, iconPath),
  49. theme: generateThemeScssMap(themePath),
  50. };
  51. };
  52. export default generateScssMap;