/
magentoFS.ts
326 lines (287 loc) · 10 KB
/
magentoFS.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
import { promises as fs } from 'fs';
import { isAbsolute, join, relative, sep } from 'path';
import { wrapP } from './wrapP';
import { ThemeAsset, ModuleAsset, Module, Theme, Components } from './types';
import { flatten } from './flatten';
import { parse } from 'fast-xml-parser';
import fromEntries from 'fromentries';
/**
* @summary Parses config.php to find a list of enabled modules
* @see https://devdocs.magento.com/guides/v2.3/config-guide/config/config-php.html
* @todo This will break if app:config:dump has been run, Fix it
*/
const CONFIG_PATH = 'app/etc/config.php';
export async function getEnabledModules(root: string) {
assertAbsolute(root);
const path = join(root, CONFIG_PATH);
const [err, raw] = await wrapP(fs.readFile(path, 'utf8'));
if (err) {
throw new Error(`Failed reading Magento config.php at ${path}`);
}
const enabledModules: string[] = [];
for (const line of raw!.split('\n')) {
// Note: Attempting to get away with a RegExp here instead
// of a full-blown PHP parser. config.php is code-gen'd, so
// the format _should_ be consistent
const matches = line.match(/^\s*'(\w+)'\s*=>\s*(\d)/);
if (!matches) continue;
const [, moduleName, enabled] = matches;
if (Number(enabled)) enabledModules.push(moduleName);
}
return enabledModules;
}
/**
* @summary Takes a guess at whether or not a dir is a
* Magento store root
*/
export async function isMagentoRoot(root: string) {
assertAbsolute(root);
const files = await fs.readdir(root);
return (
files.includes('index.php') &&
files.includes('app') &&
files.includes('vendor')
);
}
/**
* @summary Finds all Magento components (modules/themes) in
* both app/code and vendor
*/
export async function getComponents(root: string): Promise<Components> {
assertAbsolute(root);
const [composer, nonComposer] = await Promise.all([
getComposerComponents(root),
getNonComposerComponents(root),
]);
const allModules = [...composer.modules, ...nonComposer.modules];
const allThemes = [...composer.themes, ...nonComposer.themes];
const modulesByName = fromEntries(allModules.map(m => [m.moduleID, m]));
const themesByID = fromEntries(
allThemes.map(theme => {
return [theme.themeID, theme];
}),
);
return {
modules: modulesByName,
themes: themesByID,
};
}
type ComposerLock = {
packages: {
name: string;
type:
| 'magento2-module'
| 'magento2-theme'
| 'metapackage'
| 'magento2-language';
}[];
};
async function getComposerComponents(root: string) {
const [, lockfile] = await wrapP(
fs.readFile(join(root, 'composer.lock'), 'utf8'),
);
// Composer lock file isn't a requirement if you're
// not using composer
if (!lockfile) {
return { modules: [], themes: [] };
}
// We're relying on the composer lock file because it's
// significantly faster than crawling each dependency dir
const composerLock = JSON.parse(lockfile) as ComposerLock;
const pendingModules: Promise<Module>[] = [];
const pendingThemes: Promise<Theme>[] = [];
for (const { name, type } of composerLock.packages) {
if (type === 'magento2-module') {
const modulePath = join('vendor', name);
pendingModules.push(getModuleConfig(root, modulePath));
}
if (type === 'magento2-theme') {
pendingThemes.push(getThemeFromComposerName(root, name));
}
}
return {
modules: await Promise.all(pendingModules),
themes: await Promise.all(pendingThemes),
};
}
async function getNonComposerComponents(root: string) {
const [modules, themes] = await Promise.all([
getNonComposerModules(root),
getNonComposerThemes(root),
]);
return { modules, themes };
}
async function getNonComposerThemes(root: string) {
const [frontendVendors, adminVendors] = await Promise.all([
safeDirRead(join(root, 'app', 'design', 'frontend')),
safeDirRead(join(root, 'app', 'design', 'adminhtml')),
]);
const pendingFrontend = frontendVendors.map(vendor =>
getNonComposerThemesFromVendorInArea(root, vendor, 'frontend'),
);
const pendingAdmin = adminVendors.map(vendor =>
getNonComposerThemesFromVendorInArea(root, vendor, 'adminhtml'),
);
const frontendThemes = flatten(await Promise.all(pendingFrontend));
const adminThemes = flatten(await Promise.all(pendingAdmin));
return [...frontendThemes, ...adminThemes];
}
async function getNonComposerThemesFromVendorInArea(
root: string,
vendor: string,
area: Theme['area'],
): Promise<Theme[]> {
const vendorPath = join('app', 'design', area, vendor);
const themes = await fs.readdir(join(root, vendorPath), 'utf8');
return Promise.all(
themes.map(async name => ({
name,
vendor,
themeID: `${vendor}/${name}`,
area,
parentID: await getThemeParentName(join(root, vendorPath, name)),
pathFromStoreRoot: join(sep, 'app', 'design', area, vendor, name),
})),
);
}
async function getThemeParentName(themePath: string) {
const themeXMLPath = join(themePath, 'theme.xml');
const [err, source] = await wrapP(fs.readFile(themeXMLPath, 'utf8'));
if (err) {
throw new Error(
`Could not find theme configuration (theme.xml) for theme at "${themeXMLPath}"`,
);
}
// Note: Skipping a full blown XML parser (for now) to maintain speed.
// Sander will hate me :D
const [, parent = ''] = source!.match(/<parent>(.+)<\/parent>/) || [];
return parent;
}
async function getNonComposerModules(root: string) {
const codeVendorsDir = join(root, 'app', 'code');
const vendors = await safeDirRead(codeVendorsDir);
if (!vendors.length) return [];
const modules = await Promise.all(
vendors.map(async vendor => {
const moduleNames = await fs.readdir(
join(codeVendorsDir, vendor),
'utf8',
);
return Promise.all(
moduleNames.map(mod =>
getModuleConfig(root, join('app', 'code', vendor, mod)),
),
);
}),
);
return flatten(modules);
}
async function getThemeFromComposerName(
root: string,
pkgName: string,
): Promise<Theme> {
const [vendor, pieces] = pkgName.split('/');
const [firstPart, area, themeName] = pieces.split('-');
if (
firstPart !== 'theme' ||
(area !== 'frontend' && area !== 'adminhtml')
) {
throw new Error(
`Unrecognized theme package name: ${pkgName}. ` +
'The format "<vendor>/theme-<area>-<name>" must be used.',
);
}
const pathFromStoreRoot = join(sep, 'vendor', pkgName);
return {
name: themeName,
vendor,
themeID: normalizeComposerThemeName(vendor, themeName),
area: area as Theme['area'],
parentID: await getThemeParentName(join(root, pathFromStoreRoot)),
pathFromStoreRoot: pathFromStoreRoot,
};
}
function normalizeComposerThemeName(vendor: string, name: string) {
// I have no clue if this is the logic used in Magento core,
// but it's certainly a half-decent guess, yeah?
const normalizedVendor = vendor
.split('-')
.map(v => `${v[0].toUpperCase()}${v.slice(1)}`)
.join('');
return `${normalizedVendor}/${name}`;
}
async function getModuleConfig(root: string, path: string): Promise<Module> {
const configPath = join(root, path, 'etc', 'module.xml');
const rawConfig = await fs.readFile(configPath, 'utf8');
const parsedConfig = parse(rawConfig, {
ignoreAttributes: false,
attributeNamePrefix: '',
ignoreNameSpace: true,
});
const config = {
moduleID: parsedConfig.config.module.name as string,
sequence: [] as string[],
pathFromStoreRoot: join(sep, path),
};
const { sequence } = parsedConfig.config.module;
if (!sequence) return config;
if (Array.isArray(sequence.module)) {
// multiple dependencies
config.sequence = sequence.module.map((m: any) => m.name) as string[];
} else {
// single dependency (the xml parser is weird)
config.sequence.push(sequence.module.name);
}
return config;
}
/**
* @summary Provide contextful information about a file path within a theme
*/
export function parseThemePath(path: string, theme: Theme): ThemeAsset {
const relPath = relative(theme.pathFromStoreRoot, path);
// prettier-ignore
const [firstDir, /* web */, ...rest] = relPath.split(sep);
const isModuleContext = /^[a-z0-9]+_[a-z0-9]+$/i.test(firstDir);
if (isModuleContext) {
return {
type: 'ThemeAsset',
themeID: theme.themeID,
moduleID: firstDir,
pathFromStoreRoot: path,
finalPath: join(firstDir, ...rest),
};
}
return {
type: 'ThemeAsset',
themeID: theme.themeID,
pathFromStoreRoot: path,
finalPath: relative('web', relPath),
};
}
export function parseModulePath(path: string, mod: Module): ModuleAsset {
const viewDir = join(mod.pathFromStoreRoot, 'view');
// prettier-ignore
const [/*area*/, /* web */, ...rest] = relative(viewDir, path).split(sep);
return {
type: 'ModuleAsset',
moduleID: mod.moduleID,
pathFromStoreRoot: path,
finalPath: join(mod.moduleID, ...rest),
};
}
/**
* @summary Wrapper around fs.readdir that _always_ returns an array
*/
async function safeDirRead(path: string) {
return fs.readdir(path, 'utf8').catch(() => [] as string[]);
}
function assertAbsolute(path: string) {
if (isAbsolute(path)) return;
throw new Error(
`Expected an absolute path for the store root, but instead saw: "${path}"`,
);
}