-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodule-loader.js
More file actions
157 lines (137 loc) · 4.6 KB
/
Copy pathmodule-loader.js
File metadata and controls
157 lines (137 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
/**
* Module Asset Loader
*
* Automatically discovers enabled modules and collects their lang paths,
* Playwright configs, and other metadata for the main Vite configuration.
*
* @fileoverview Modules are identified by the presence of a vite.config.js file.
* Module CSS and JS assets are imported directly in each module's app.ts entry point.
*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const MODULES_PATH = 'modules';
export async function loadEnabledModuleNames(baseDir) {
const modulesDir = path.join(baseDir, MODULES_PATH);
try {
const entries = await fs.readdir(modulesDir, { withFileTypes: true });
const names = [];
for (const entry of entries) {
if (!entry.isDirectory() || entry.name.startsWith('.')) continue;
try {
await fs.access(
path.join(modulesDir, entry.name, 'vite.config.js'),
);
names.push(entry.name);
} catch {
// no vite.config.js — not a module directory
}
}
return names;
} catch (error) {
console.error(
`Failed to read modules directory at ${modulesDir}: ${error.message}`,
);
return [];
}
}
async function importModuleFile(filePath, moduleName, displayName) {
try {
await fs.access(filePath);
} catch (error) {
if (error?.code === 'ENOENT') {
return null;
}
console.warn(
`Module ${moduleName}: unable to access ${displayName} - ${
error && error.message
}`,
);
return null;
}
try {
return await import(pathToFileURL(filePath).href);
} catch (error) {
console.warn(
`Module ${moduleName}: Invalid ${displayName} - ${error.message}`,
);
return null;
}
}
/**
* Collects Playwright projects from enabled modules
*
* @returns {Promise<{ projects: Object[], setups: Object[] }>} Module test projects and setup projects
*/
export async function collectModulePlaywrightConfigs() {
const projects = [];
const setups = [];
const modulesDir = path.join(__dirname, MODULES_PATH);
const enabledModules = await loadEnabledModuleNames(__dirname);
const configFile = 'playwright.config.ts';
for (const moduleName of enabledModules) {
const moduleTestDir = path.join(
MODULES_PATH,
moduleName,
'tests',
'e2e',
);
const moduleSetupNames = [];
const config = await importModuleFile(
path.join(modulesDir, moduleName, configFile),
moduleName,
configFile,
);
if (config?.default) {
const rawSetups = Array.isArray(config.default)
? config.default
: [config.default];
for (const setupConfig of rawSetups) {
if (!setupConfig || !setupConfig.name) {
console.warn(
`Module ${moduleName}: playwright.config.ts setup entry missing 'name' field — skipped`,
);
continue;
}
moduleSetupNames.push(setupConfig.name);
setups.push({
...setupConfig,
testDir: setupConfig.testDir ?? moduleTestDir,
});
}
}
projects.push({
name: `@${moduleName}`,
testDir: moduleTestDir,
dependencies: ['database.setup', ...moduleSetupNames],
});
}
return { projects, setups };
}
/**
* Collects language paths from enabled modules
*
* @returns {Promise<string[]>} Array of module language directory paths
*
* @example
* const langPaths = await collectModuleLangPaths();
* // Returns: ['modules/auth/lang', 'modules/billing/lang', ...]
*/
export async function collectModuleLangPaths() {
const langPaths = [];
const modulesDir = path.join(__dirname, MODULES_PATH);
const enabledModules = await loadEnabledModuleNames(__dirname);
for (const moduleName of enabledModules) {
const langPath = path.join(MODULES_PATH, moduleName, 'lang');
const fullLangPath = path.join(modulesDir, moduleName, 'lang');
try {
await fs.access(fullLangPath);
langPaths.push(langPath);
} catch {
// Module doesn't have a lang directory, skip it
}
}
return langPaths;
}