-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcartridges.ts
More file actions
96 lines (88 loc) · 2.84 KB
/
cartridges.ts
File metadata and controls
96 lines (88 loc) · 2.84 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
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {globSync} from 'glob';
import path from 'node:path';
/**
* Represents a discovered cartridge in the local filesystem.
*/
export interface CartridgeMapping {
/** Cartridge name (directory name containing .project) */
name: string;
/** Absolute path to the cartridge directory */
src: string;
/** Destination name (same as name, used for WebDAV path) */
dest: string;
}
/**
* Options for finding cartridges.
*/
export interface FindCartridgesOptions {
/** Cartridge names to include (if empty, all are included) */
include?: string[];
/** Cartridge names to exclude */
exclude?: string[];
}
/**
* Find cartridges recursively in a directory.
*
* Cartridges are identified by the presence of a `.project` file
* (Eclipse project marker commonly used in SFCC development).
*
* @param directory - Directory to search for cartridges (defaults to cwd)
* @param options - Filter options for including/excluding cartridges
* @returns Array of discovered cartridge mappings
*
* @example
* ```typescript
* // Find all cartridges in current directory
* const cartridges = findCartridges();
*
* // Find cartridges in specific directory
* const cartridges = findCartridges('./my-project');
*
* // Find specific cartridges only
* const cartridges = findCartridges('.', { include: ['app_storefront_base'] });
*
* // Find all except certain cartridges
* const cartridges = findCartridges('.', { exclude: ['test_cartridge'] });
* ```
*/
export function findCartridges(directory?: string, options: FindCartridgesOptions = {}): CartridgeMapping[] {
const searchDir = directory ? path.resolve(directory) : process.cwd();
// Find all .project files (Eclipse project markers).
// Ignore common non-cartridge dirs to keep discovery fast when projectRoot is broad (e.g. MCP working directory).
const projectFiles = globSync('**/.project', {
cwd: searchDir,
ignore: [
'**/node_modules/**',
'**/.git/**',
'**/dist/**',
'**/build/**',
'**/coverage/**',
'**/.cache/**',
'**/tmp/**',
'**/temp/**',
],
});
let cartridges = projectFiles.map((f) => {
const dirname = path.resolve(searchDir, path.dirname(f));
const cartridgeName = path.basename(dirname);
return {
name: cartridgeName,
dest: cartridgeName,
src: dirname,
};
});
// Apply include filter
if (options.include && options.include.length > 0) {
cartridges = cartridges.filter((c) => options.include!.includes(c.name));
}
// Apply exclude filter
if (options.exclude && options.exclude.length > 0) {
cartridges = cartridges.filter((c) => !options.exclude!.includes(c.name));
}
return cartridges;
}