mage-obsidian 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mage-obsidian",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Mage Obsidian Ultis",
5
5
  "main": "./src/scripts/buildThemes.js",
6
6
  "keywords": [
@@ -25,12 +25,14 @@
25
25
  "./config/": "./src/config/"
26
26
  },
27
27
  "dependencies": {
28
- "chalk": "^5.3.0",
28
+ "chalk": "^5.4.1",
29
+ "commander": "^13.1.0",
29
30
  "deepmerge": "^4.3.1",
31
+ "readline-sync": "^1.4.10"
32
+ },
33
+ "devDependencies": {
30
34
  "jest": "^29.7.0",
31
- "jest-environment-node": "^29.7.0",
32
- "readline-sync": "^1.4.10",
33
- "commander": "^13.1.0"
35
+ "jest-environment-node": "^29.7.0"
34
36
  },
35
37
  "scripts": {
36
38
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
@@ -1,192 +1,73 @@
1
- import path from "path";
2
- import configResolver from "./configResolver.js";
3
- import {
4
- ALL_JS_VUE_FILES_WITH_INHERITANCE_FILE_NAME,
5
- MODULE_WEB_PATH,
6
- PRECOMPILED_FOLDER,
7
- THEME_MODULE_WEB_PATH
8
- } from "../config/default.js";
9
- import getFilesFromFolders from '../utils/findComponents.js'
10
- import fs from "fs";
11
- import deepmerge from "deepmerge";
1
+ import themeResolver from './themeResolverSync.js';
2
+ import path from 'node:path';
3
+ import { MODULE_WEB_PATH, THEME_CSS_FOLDER, THEME_MODULE_WEB_PATH } from '../config/default.js';
4
+ import { resolveFileByTheme, getAllJsVueFilesWithInheritanceCached } from './moduleResolver.js';
5
+ import configResolver from './configResolver.js';
6
+ import fs from 'node:fs/promises';
12
7
 
13
- const defaultFoldersToMap = [
14
- {
15
- src: configResolver.getMagentoConfig().VUE_COMPONENTS_PATH,
16
- ext: [
17
- 'vue',
18
- 'js'
19
- ]
20
- },
21
- {
22
- src: configResolver.getMagentoConfig().JS_PATH,
23
- ext: [
24
- 'js'
25
- ]
26
- }
27
- ];
28
-
29
- const cachedModulesByTheme = {};
30
-
31
- async function getAllJsVueFilesFromActiveModules() {
32
- let result = {};
33
- for (const entry of configResolver.getModulesConfigArray()) {
34
- const [moduleName, moduleConfig] = entry;
35
- const moduleResult = await getFilesFromFolders(
36
- moduleName,
37
- path.resolve(moduleConfig.src, MODULE_WEB_PATH),
38
- defaultFoldersToMap
39
- );
40
- Object.assign(result, moduleResult);
41
- }
42
- return result;
43
- }
44
-
45
- async function getAllJsVueFilesFromTheme(themeName) {
46
- let result = {};
47
- const theme = configResolver.getMagentoConfig().themes[themeName];
48
- if (!theme) {
49
- return {}
50
- }
51
-
52
- if (theme.parent) {
53
- result = await getAllJsVueFilesFromTheme(theme.parent)
54
- }
55
-
56
- for (const moduleName of configResolver.getAllMagentoModulesEnabled()) {
57
- const modulePath = path.resolve(theme.src, moduleName, THEME_MODULE_WEB_PATH);
58
- const moduleResult = await getFilesFromFolders(
59
- moduleName,
60
- modulePath,
61
- defaultFoldersToMap
62
- );
63
- if (moduleResult) {
64
- result = deepmerge(result, moduleResult);
65
- }
66
- }
67
- const moduleResult = await getFilesFromFolders(
68
- 'Theme',
69
- path.resolve(theme.src, THEME_MODULE_WEB_PATH),
70
- defaultFoldersToMap
71
- );
72
- result = deepmerge(result, moduleResult);
73
- return result;
74
- }
75
-
76
-
77
- function getAllJsVueFilesWithInheritanceCached(themeName) {
78
- themeName = themeName ?? process.env.CURRENT_THEME;
79
- if (cachedModulesByTheme[themeName]) {
80
- return cachedModulesByTheme[themeName];
81
- }
82
- //check if exits
8
+ const { getMagentoConfig, getModuleDefinition, getThemeDefinition, getModulesConfigArray } = configResolver;
83
9
 
84
- const filePath = path.join(PRECOMPILED_FOLDER, themeName, ALL_JS_VUE_FILES_WITH_INHERITANCE_FILE_NAME);
85
- const fileContent = fs.readFileSync(filePath, "utf-8");
86
- const data = JSON.parse(fileContent);
87
- cachedModulesByTheme[themeName] = data;
88
- return data;
89
- }
10
+ const MODULE_CSS_EXTEND_FILE = getMagentoConfig().MODULE_CSS_EXTEND_FILE;
11
+ const THEME_CSS_SOURCE_FILE = getMagentoConfig().THEME_CSS_SOURCE_FILE;
90
12
 
91
- async function getAllJsVueFilesWithInheritance(themeName) {
92
- themeName = themeName ?? process.env.CURRENT_THEME;
93
- if (cachedModulesByTheme[themeName]) {
94
- return cachedModulesByTheme[themeName];
13
+ async function getThemeImports(themeName, themeConfig) {
14
+ if (!themeConfig) {
15
+ themeConfig = themeResolver.getThemeConfig(themeName);
95
16
  }
96
- const allJsVueFilesFromModules = await getAllJsVueFilesFromActiveModules();
97
- const allJsVueFilesFromThemes = await getAllJsVueFilesFromTheme(themeName);
98
- cachedModulesByTheme[themeName] = deepmerge(allJsVueFilesFromModules, allJsVueFilesFromThemes);
99
- return cachedModulesByTheme[themeName];
100
- }
101
-
102
- const moduleConfigByThemeCache = new Map();
103
-
104
- const MODULE_CONFIG_FILE = configResolver.getMagentoConfig().MODULE_CONFIG_FILE;
17
+ const themeDefinition = getThemeDefinition(themeName);
18
+ const themePath = themeDefinition.src;
105
19
 
106
- /**
107
- * Gets the theme configuration file path.
108
- * @param {string} moduleSrc
109
- * @returns {string} Full path to the configuration file.
110
- */
111
- function getModuleConfigPath(moduleSrc) {
112
- return path.join(moduleSrc, 'view/frontend/web/', MODULE_CONFIG_FILE);
113
- }
114
-
115
- function resolveFileByTheme(themeName, moduleName, filePath, includeTailwindConfigFromParentThemes) {
116
- const theme = configResolver.getMagentoConfig().themes[themeName];
117
- const fullFilePath = path.join(theme.src, moduleName, 'web', filePath);
118
- if (fs.existsSync(fullFilePath)) {
119
- return fullFilePath;
20
+ let imports = '';
21
+ if (themeConfig.includeCssSourceFromParentThemes && themeDefinition.parent) {
22
+ imports += await getThemeImports(themeDefinition.parent, themeConfig);
120
23
  }
121
24
 
122
- if (includeTailwindConfigFromParentThemes && theme.parent) {
123
- return resolveFileByTheme(theme.parent, moduleName, filePath, includeTailwindConfigFromParentThemes);
124
- }
125
- return null;
25
+ imports += `@import "${path.join(themePath, THEME_MODULE_WEB_PATH, THEME_CSS_FOLDER, THEME_CSS_SOURCE_FILE)}";\n`;
26
+ return imports;
126
27
  }
127
28
 
128
- async function resolveModuleConfig(moduleName, themeName, includeTailwindConfigFromParentThemes) {
129
- const module = configResolver.getMagentoConfig().modules[moduleName];
130
- let moduleConfigSourcePath = resolveFileByTheme(themeName, moduleName, MODULE_CONFIG_FILE, includeTailwindConfigFromParentThemes);
131
-
132
- if (!moduleConfigSourcePath && !module) {
133
- return null;
134
- }
29
+ function resolveModuleCssSourcePath(moduleName, themeName) {
30
+ let moduleConfigSourcePath = resolveFileByTheme(themeName, moduleName, 'css', MODULE_CSS_EXTEND_FILE);
135
31
  if (!moduleConfigSourcePath) {
136
- moduleConfigSourcePath = getModuleConfigPath(module.src);
137
- }
138
- if (!fs.existsSync(moduleConfigSourcePath)) {
139
- return null;
32
+ const moduleDefinition = getModuleDefinition(moduleName);
33
+ moduleConfigSourcePath = path.join(moduleDefinition.src, MODULE_WEB_PATH, 'css', MODULE_CSS_EXTEND_FILE);
140
34
  }
141
-
142
- // Importación dinámica común en ESModules
143
- const moduleConfig = await import(moduleConfigSourcePath);
144
- return moduleConfig.default ?? moduleConfig;
35
+ return moduleConfigSourcePath;
145
36
  }
146
37
 
147
- /**
148
- * Retrieves the Tailwind configuration for a theme synchronously.
149
- * Uses a cache system to optimize performance.
150
- * @param {string} themeName - Name of the theme.
151
- * @param {object} themeConfig
152
- * @returns {object|null} Tailwind configuration object.
153
- */
154
- async function getModuleConfigByThemeConfig(themeName, themeConfig) {
155
- if (moduleConfigByThemeCache.has(themeName)) {
156
- return moduleConfigByThemeCache.get(themeName);
157
- }
158
- if (!themeConfig) return null;
159
- const modules = configResolver.getAllMagentoModulesEnabled();
160
- let modulesConfig = {};
38
+ async function getVueComponentsSource(themeName) {
39
+ const vueComponents = await getAllJsVueFilesWithInheritanceCached(themeName);
40
+ // split with @source al inicio
41
+ const vueComponentSources = Object.entries(vueComponents).map(([name, url]) => {
42
+ return `@source "${url}";\n`;
43
+ })
44
+ return vueComponentSources.join('');
45
+ }
161
46
 
162
- for (const moduleName of modules) {
163
- let moduleConfig = await resolveModuleConfig(moduleName, themeName, themeConfig.includeTailwindConfigFromParentThemes);
164
- if (!moduleConfig) continue;
165
- if (
166
- themeConfig.ignoredTailwindConfigFromModules === 'all' ||
167
- themeConfig.ignoredTailwindConfigFromModules.includes(moduleName)
168
- ) {
169
- moduleConfig.tailwind = {};
170
- } else if (moduleConfig.tailwind?.content) {
171
- const moduleSrc = configResolver.getMagentoConfig().modules[moduleName].src;
172
- moduleConfig.tailwind.content = moduleConfig.tailwind.content.map((content) =>
173
- path.join(moduleSrc, 'view/frontend/web', content)
174
- );
47
+ async function getCssImports(themeName) {
48
+ const themeConfig = themeResolver.getThemeConfig(themeName);
49
+ const excludedModules = new Set(themeConfig.ignoredCssFromModules || []);
50
+ const modulesConfig = getModulesConfigArray();
51
+
52
+ let cssSource = await getVueComponentsSource(themeName);
53
+ let cssImports = '';
54
+
55
+ if (themeConfig.ignoredCssFromModules !== 'all') {
56
+ for (const [moduleName, moduleConfig] of modulesConfig) {
57
+ if (excludedModules.has(moduleName)) continue;
58
+
59
+ const filePath = resolveModuleCssSourcePath(moduleName, themeName);
60
+ try {
61
+ await fs.access(filePath);
62
+ cssImports += `@import "${filePath}";\n`;
63
+ } catch {
64
+ // ignore if file doesn't exist
65
+ }
175
66
  }
176
-
177
- modulesConfig = deepmerge(modulesConfig, moduleConfig);
178
67
  }
179
68
 
180
- moduleConfigByThemeCache.set(themeName, modulesConfig);
181
- return modulesConfig;
69
+ cssImports += await getThemeImports(themeName);
70
+ return `${cssSource}\n${cssImports}`;
182
71
  }
183
72
 
184
- export default {
185
- getAllJsVueFilesWithInheritance,
186
- getAllJsVueFilesWithInheritanceCached,
187
- getModuleConfigByThemeConfig,
188
- resolveFileByTheme
189
- };
190
-
191
- export { getModuleConfigByThemeConfig, resolveFileByTheme, getAllJsVueFilesWithInheritance, getAllJsVueFilesWithInheritanceCached };
192
-
73
+ export default getCssImports;
@@ -186,5 +186,5 @@ export default {
186
186
  resolveFileByTheme
187
187
  };
188
188
 
189
- export { getModuleConfigByThemeConfig, resolveFileByTheme };
189
+ export { getModuleConfigByThemeConfig, resolveFileByTheme, getAllJsVueFilesWithInheritance, getAllJsVueFilesWithInheritanceCached };
190
190