Skip to content

Commit 9ebc6be

Browse files
committed
dw json should not return config on invalid instance name
1 parent 035cb59 commit 9ebc6be

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

packages/b2c-tooling-sdk/src/config/dw-json.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function findDwJson(startDir: string = process.cwd()): string | undefined
124124
* 2. Config marked as `active: true`
125125
* 3. Root-level config
126126
*/
127-
function selectConfig(json: DwJsonMultiConfig, instanceName?: string): DwJsonConfig {
127+
function selectConfig(json: DwJsonMultiConfig, instanceName?: string): DwJsonConfig | undefined {
128128
const logger = getLogger();
129129

130130
// Single config or no configs array
@@ -152,11 +152,9 @@ function selectConfig(json: DwJsonMultiConfig, instanceName?: string): DwJsonCon
152152
logger.trace({selection: 'named', instanceName}, `[DwJsonSource] Selected config "${instanceName}" by name`);
153153
return found;
154154
}
155-
// Instance not found, fall through to other selection methods
156-
logger.trace(
157-
{requestedInstance: instanceName},
158-
`[DwJsonSource] Named instance "${instanceName}" not found, falling back`,
159-
);
155+
// Instance explicitly requested but not found - return undefined
156+
logger.trace({requestedInstance: instanceName}, `[DwJsonSource] Named instance "${instanceName}" not found`);
157+
return undefined;
160158
}
161159

162160
// Find active config
@@ -224,8 +222,12 @@ export function loadDwJson(options: LoadDwJsonOptions = {}): LoadDwJsonResult |
224222
try {
225223
const content = fs.readFileSync(dwJsonPath, 'utf8');
226224
const json = JSON.parse(content) as DwJsonMultiConfig;
225+
const config = selectConfig(json, options.instance);
226+
if (!config) {
227+
return undefined;
228+
}
227229
return {
228-
config: selectConfig(json, options.instance),
230+
config,
229231
path: dwJsonPath,
230232
};
231233
} catch (error) {

packages/b2c-tooling-sdk/test/config/dw-json.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ describe('config/dw-json', () => {
106106
expect(result?.config.name).to.equal('staging');
107107
});
108108

109+
it('returns undefined when requested instance does not exist', () => {
110+
const dwJsonPath = path.join(tempDir, 'dw.json');
111+
const multiConfig = {
112+
hostname: 'root.demandware.net',
113+
configs: [
114+
{name: 'staging', hostname: 'staging.demandware.net'},
115+
{name: 'production', hostname: 'prod.demandware.net'},
116+
],
117+
};
118+
fs.writeFileSync(dwJsonPath, JSON.stringify(multiConfig));
119+
120+
const result = loadDwJson({instance: 'nonexistent'});
121+
expect(result).to.be.undefined;
122+
});
123+
109124
it('selects active config when no instance specified', () => {
110125
const dwJsonPath = path.join(tempDir, 'dw.json');
111126
const multiConfig = {

0 commit comments

Comments
 (0)