@domain-first/project-structure 8.5.0 → 8.6.0

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/dist/index.cjs CHANGED
@@ -1419,7 +1419,31 @@ const scaffoldReactPage = async (presentationReactFolder)=>{
1419
1419
  if (usesNext()) scaffoldNextRoute(nestedName, pageFile);
1420
1420
  };
1421
1421
  const openapiExtractorPath = "scripts/extract-open-api-data-from-endpoint.ts";
1422
- const rootFolderSegments = (rootFolder)=>rootFolder.split(/[\\/]/).map((segment)=>segment.trim()).filter((segment)=>!!segment && '.' !== segment).map((segment)=>`'${segment}',\n `).join('');
1422
+ const rootFolderSegments = (rootFolder, indent = 20)=>rootFolder.split(/[\\/]/).map((segment)=>segment.trim()).filter((segment)=>!!segment && '.' !== segment).map((segment)=>`'${segment}',\n${' '.repeat(indent)}`).join('');
1423
+ const endpointFileLookup = (rootFolder)=>` const [currentBoundedContext, ...relativePath] = routeMetadata.path;
1424
+
1425
+ if (!currentBoundedContext) {
1426
+ return undefined;
1427
+ }
1428
+
1429
+ // A controller is the first folder of the path; other routes
1430
+ // belong to the context root in presentation/rest/(root).
1431
+ const restFolder = path.join(
1432
+ ${rootFolderSegments(rootFolder, 16)}'bounded-contexts',
1433
+ currentBoundedContext,
1434
+ 'presentation',
1435
+ 'rest'
1436
+ );
1437
+ const endpointPath = [
1438
+ ...relativePath.map(pathParameterAsName),
1439
+ \`\${routeMetadata.method.toUpperCase()}.ts\`
1440
+ ];
1441
+ const endpointFile =
1442
+ project.getSourceFile(path.join(restFolder, ...endpointPath)) ??
1443
+ project.getSourceFile(
1444
+ path.join(restFolder, '(root)', ...endpointPath)
1445
+ );
1446
+ `;
1423
1447
  const openapiExtractor = (rootFolder)=>`import path from 'node:path';
1424
1448
  import {
1425
1449
  type DocNode,
@@ -1531,25 +1555,7 @@ export const createOpenApiDataFromEndpointLogicExtractor = ({
1531
1555
  path: readonly string[];
1532
1556
  }): OpenApiData | undefined => {
1533
1557
  try {
1534
- const [currentBoundedContext, controller, ...relativePath] =
1535
- routeMetadata.path;
1536
-
1537
- if (!currentBoundedContext || !controller) {
1538
- return undefined;
1539
- }
1540
-
1541
- const endpointFile = project.getSourceFile(
1542
- path.join(
1543
- ${rootFolderSegments(rootFolder)}'bounded-contexts',
1544
- currentBoundedContext,
1545
- 'presentation',
1546
- 'rest',
1547
- controller,
1548
- ...relativePath.map(pathParameterAsName),
1549
- \`\${routeMetadata.method.toUpperCase()}.ts\`
1550
- )
1551
- );
1552
-
1558
+ ${endpointFileLookup(rootFolder)}
1553
1559
  if (!endpointFile) {
1554
1560
  return undefined;
1555
1561
  }
@@ -3200,7 +3206,7 @@ const scaffoldRequestSchema = external_zod_default().discriminatedUnion('operati
3200
3206
  agent_requests_operation('endpoint', {
3201
3207
  ...common,
3202
3208
  ...agent_requests_owner,
3203
- controller: agent_requests_name,
3209
+ controller: agent_requests_name.optional().describe('Omit for the context-root controller (root): `/<context>/<path>`.'),
3204
3210
  method: external_zod_default()["enum"]([
3205
3211
  'get',
3206
3212
  'post',
@@ -4236,6 +4242,35 @@ const deleteReactModule = (projectRoot, reactFolder, module, route)=>{
4236
4242
  ]
4237
4243
  };
4238
4244
  };
4245
+ const rootControllerFolder = '(root)';
4246
+ const pathParameterAsName = (segment)=>segment.startsWith(':') ? `_${segment.slice(1)}_` : segment;
4247
+ const isPathParameterName = (name)=>name.startsWith(':') || /^_.+_$/.test(name);
4248
+ const endpointFileNames = new Set([
4249
+ 'get',
4250
+ 'post',
4251
+ 'put',
4252
+ 'patch',
4253
+ 'delete'
4254
+ ].map((method)=>`${method.toUpperCase()}.ts`));
4255
+ const rest_layout_entries = (folder)=>external_node_fs_default().existsSync(folder) ? external_node_fs_default().readdirSync(folder, {
4256
+ withFileTypes: true
4257
+ }) : [];
4258
+ const rest_layout_subfolders = (folder)=>rest_layout_entries(folder).filter((entry)=>entry.isDirectory()).map((entry)=>entry.name);
4259
+ const restFolders = (boundedContextFolder)=>({
4260
+ presentation: external_node_path_default().join(boundedContextFolder, 'presentation', 'rest'),
4261
+ wiring: external_node_path_default().join(boundedContextFolder, 'wiring', 'presentation', 'rest')
4262
+ });
4263
+ const restControllers = (boundedContextFolder)=>rest_layout_subfolders(restFolders(boundedContextFolder).presentation).filter((name)=>name !== rootControllerFolder && !isPathParameterName(name));
4264
+ const rootStaticSegments = (boundedContextFolder)=>rest_layout_subfolders(external_node_path_default().join(restFolders(boundedContextFolder).presentation, rootControllerFolder)).filter((name)=>!isPathParameterName(name));
4265
+ const legacyRootEndpoints = (boundedContextFolder)=>{
4266
+ const { presentation, wiring } = restFolders(boundedContextFolder);
4267
+ return {
4268
+ endpointFiles: rest_layout_entries(presentation).filter((entry)=>entry.isFile() && endpointFileNames.has(entry.name)).map((entry)=>external_node_path_default().join(presentation, entry.name)),
4269
+ parameterFolders: rest_layout_subfolders(presentation).filter(isPathParameterName).map((name)=>external_node_path_default().join(presentation, name)),
4270
+ wireFiles: rest_layout_entries(wiring).filter((entry)=>entry.isFile() && 'index.ts' !== entry.name && /\.ts$/.test(entry.name)).map((entry)=>external_node_path_default().join(wiring, entry.name))
4271
+ };
4272
+ };
4273
+ const hasLegacyRootEndpoints = (boundedContextFolder)=>Object.values(legacyRootEndpoints(boundedContextFolder)).some((paths)=>paths.length > 0);
4239
4274
  const systemImplementationChoices = (kind, crossContext = false)=>{
4240
4275
  if ('ports' === kind) return crossContext ? [
4241
4276
  {
@@ -5130,47 +5165,49 @@ export const ${names.namespace(ClassName)} = errorNamespace('${boundedContextFol
5130
5165
  const namespaceFile = currentErrorFolder.file(`${namespace}.ts`);
5131
5166
  namespaceFile.addLineIfMissing(errorClass(names.error(`${namespaceNaming.ClassName}${ClassName}`), exportedNamespace(namespaceFile, names.namespace(namespaceNaming.ClassName)), fileName), '\n\n');
5132
5167
  };
5168
+ const newController = 'New controller';
5169
+ const rootControllerChoice = 'No controller (context root)';
5170
+ const sharedRestFolder = (boundedContextFolder)=>boundedContextFolder.subitem([
5171
+ '..',
5172
+ '..',
5173
+ 'presentation',
5174
+ 'rest'
5175
+ ]);
5176
+ const legacyRootEndpointsMessage = (context)=>`${context} keeps its context-root endpoints in the 8.5 layout (presentation/rest/<METHOD>.ts, _param_/). Run npx df-upgrade to move them to presentation/rest/(root)/ first.`;
5133
5177
  const scaffoldNewHandlersRestEndpoint = async (boundedContextFolder)=>{
5178
+ const context = boundedContextFolder.name;
5134
5179
  const restPresentationFolder = boundedContextFolder.subitem([
5135
5180
  'presentation',
5136
5181
  'rest'
5137
5182
  ]);
5138
- const controllers = restPresentationFolder.content.subfolderNames;
5139
- let controller = await interaction_select({
5183
+ const controllers = restControllers(boundedContextFolder.path);
5184
+ const rootSegments = rootStaticSegments(boundedContextFolder.path);
5185
+ let controllerChoice = await interaction_select({
5140
5186
  key: 'controllerSelection',
5141
5187
  message: 'Controller: ',
5142
5188
  choices: [
5189
+ {
5190
+ value: rootControllerChoice,
5191
+ name: `(root) — /${context}`
5192
+ },
5143
5193
  ...controllers,
5144
- 'New controller'
5194
+ newController
5145
5195
  ]
5146
5196
  });
5147
- if ('New controller' === controller) {
5148
- controller = await interaction_input({
5149
- key: 'controllerName',
5150
- message: 'Name: '
5151
- });
5152
- if (config_file_ConfigFile.backend.domainFirstPackages.includes('@domain-first/wire')) {
5153
- boundedContextFolder.subitem([
5154
- 'wiring',
5155
- 'presentation',
5156
- 'rest'
5157
- ]).file('index.ts').addLine(`export * from './${controller}'`);
5158
- const sharedRestFolder = boundedContextFolder.subitem([
5159
- '..',
5160
- '..',
5161
- 'presentation',
5162
- 'rest'
5163
- ]);
5164
- sharedRestFolder.file('wires.ts').addLine(`export * from '../../bounded-contexts/${boundedContextFolder.name}/wiring/presentation/rest/${controller}'`);
5165
- if (!sharedRestFolder.file('index.ts').exists) sharedRestFolder.createFile('index.ts', `
5166
- import * as wires from './wires'
5167
-
5168
- const restWires = Object.values(wires)
5169
-
5170
- export const restHandlers = restWires.map(x => x().handle)
5171
- `.trim());
5197
+ if (controllerChoice === rootControllerChoice && hasLegacyRootEndpoints(boundedContextFolder.path)) throw new RequestError('UPGRADE_REQUIRED', legacyRootEndpointsMessage(context));
5198
+ if (controllerChoice === newController) controllerChoice = await interaction_input({
5199
+ key: 'controllerName',
5200
+ message: 'Name: ',
5201
+ validate: (value)=>{
5202
+ if (!value.trim()) return 'Name is required; pick "(root)" for an endpoint at the context root';
5203
+ if (isPathParameterName(value) || value.includes('/') || /[()]/.test(value)) return 'A controller is one static path segment';
5204
+ if (value === context) return `"${value}" is the context itself: pick "(root)" for /${context}`;
5205
+ if (rootSegments.includes(value)) return `/${context}/${value} belongs to the context root ((root)/${value}): add the endpoint there`;
5206
+ return true;
5172
5207
  }
5173
- }
5208
+ });
5209
+ const controller = controllerChoice === rootControllerChoice ? void 0 : controllerChoice;
5210
+ const withWiring = config_file_ConfigFile.backend.domainFirstPackages.includes('@domain-first/wire');
5174
5211
  const method = await interaction_select({
5175
5212
  key: 'method',
5176
5213
  message: 'Method:',
@@ -5184,21 +5221,38 @@ export const restHandlers = restWires.map(x => x().handle)
5184
5221
  });
5185
5222
  const routeSegments = (await interaction_input({
5186
5223
  key: 'path',
5187
- message: 'Relative path (split by "/"):'
5224
+ message: 'Relative path (split by "/"):',
5225
+ validate: (value)=>{
5226
+ const [first] = value.split('/').filter((x)=>!!x);
5227
+ return controller || !first || first.startsWith(':') || !controllers.includes(first) ? true : `/${context}/${first} belongs to controller "${first}": add the endpoint there`;
5228
+ }
5188
5229
  })).split('/').filter((x)=>!!x);
5189
- const pathParameterAsName = (segment)=>segment.startsWith(':') ? `_${segment.slice(1)}_` : segment;
5190
- const controllerFolder = restPresentationFolder.subitem([
5191
- controller
5192
- ]);
5193
- const endpointFolder = controllerFolder.subitem(routeSegments.map(pathParameterAsName));
5194
- const nameBase = `${controller}-${routeSegments.length ? `${routeSegments.map(pathParameterAsName).join('-')}-` : ''}${method}-endpoint`;
5230
+ const group = controller ?? "(root)";
5231
+ const folderSegments = [
5232
+ group,
5233
+ ...routeSegments
5234
+ ];
5235
+ const routePath = [
5236
+ context,
5237
+ ...controller ? [
5238
+ controller
5239
+ ] : [],
5240
+ ...routeSegments
5241
+ ];
5242
+ const endpointFolder = restPresentationFolder.subitem(folderSegments.map(pathParameterAsName));
5243
+ const nameSegments = [
5244
+ controller ?? context,
5245
+ ...routeSegments
5246
+ ];
5247
+ const nameBase = `${[
5248
+ ...nameSegments.map(pathParameterAsName),
5249
+ method
5250
+ ].join('-')}-endpoint`;
5195
5251
  const endpointClassName = [
5196
- controller,
5197
- ...routeSegments,
5252
+ ...nameSegments,
5198
5253
  method,
5199
5254
  'endpoint'
5200
5255
  ].map((segment)=>segment.startsWith(':') ? pathParameterAsName(segment) : new UnknownFormatNaming(segment).ClassName).join('');
5201
- const withWiring = config_file_ConfigFile.backend.domainFirstPackages.includes('@domain-first/wire');
5202
5256
  const endpointGenerator = findRestEndpointGenerator(boundedContextFolder.subitem([
5203
5257
  '..',
5204
5258
  '..',
@@ -5218,8 +5272,8 @@ export class ${endpointClassName} {
5218
5272
 
5219
5273
  get handle() {
5220
5274
  return this.endpointGenerator(this.handler.handle, {
5221
- route: { method: '${method}', path: ['${boundedContextFolder.name}', '${controller}'${routeSegments.length ? `, ${routeSegments.map((x)=>`'${x}'`).join(', ')}` : ''}] },
5222
- tags: ['${boundedContextFolder.name}: ${controller}'],
5275
+ route: { method: '${method}', path: [${routePath.map((x)=>`'${x}'`).join(', ')}] },
5276
+ tags: ['${context}${controller ? `: ${controller}` : ''}'],
5223
5277
  })
5224
5278
  }
5225
5279
  }
@@ -5229,7 +5283,7 @@ export class ${endpointClassName} {
5229
5283
  'wiring',
5230
5284
  'presentation',
5231
5285
  'rest',
5232
- controller
5286
+ group
5233
5287
  ]);
5234
5288
  const wireNameBase = `wire-${nameBase}`;
5235
5289
  const wireNaming = new UnknownFormatNaming(wireNameBase);
@@ -5249,6 +5303,20 @@ export const wire${endpointClassName} = wireClass(
5249
5303
  )
5250
5304
  `.trim());
5251
5305
  wiringFolder.file('index.ts').addLineIfMissing(`export * from './${wireNaming.fileName}'`);
5306
+ boundedContextFolder.subitem([
5307
+ 'wiring',
5308
+ 'presentation',
5309
+ 'rest'
5310
+ ]).file('index.ts').addLineIfMissing(`export * from './${group}'`);
5311
+ sharedRestFolder(boundedContextFolder).file('wires.ts').addLineIfMissing(`export * from '../../bounded-contexts/${context}/wiring/presentation/rest/${group}'`);
5312
+ const restHandlersFile = sharedRestFolder(boundedContextFolder).file('index.ts');
5313
+ if (!restHandlersFile.exists) sharedRestFolder(boundedContextFolder).createFile('index.ts', `
5314
+ import * as wires from './wires'
5315
+
5316
+ const restWires = Object.values(wires)
5317
+
5318
+ export const restHandlers = restWires.map(x => x().handle)
5319
+ `.trim());
5252
5320
  }
5253
5321
  };
5254
5322
  const scaffold_new_implementation_kindTitles = {
@@ -5888,12 +5956,12 @@ const answersFor = (command, request, owner)=>{
5888
5956
  if ('error' === request.operation || 'error-namespace' === request.operation) defaults.errorAction = 'error-namespace' === request.operation ? 'New namespace' : request.namespace ? `New error in namespace ${request.namespace}` : 'New error without namespace';
5889
5957
  if ('endpoint' === request.operation) {
5890
5958
  delete answers.controller;
5891
- const existing = owner.subitem([
5892
- 'presentation',
5893
- 'rest'
5894
- ]).content.subfolderNames.includes(request.controller);
5895
- answers.controllerSelection = existing ? request.controller : 'New controller';
5896
- if (!existing) answers.controllerName = request.controller;
5959
+ if (void 0 === request.controller) answers.controllerSelection = rootControllerChoice;
5960
+ else {
5961
+ const existing = restControllers(owner.path).includes(request.controller);
5962
+ answers.controllerSelection = existing ? request.controller : 'New controller';
5963
+ if (!existing) answers.controllerName = request.controller;
5964
+ }
5897
5965
  }
5898
5966
  if ('move' === request.operation) defaults.targetContext = request.context;
5899
5967
  }
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { select as prompts_select } from "@inquirer/prompts";
3
3
  import { runCLI } from "./561.js";
4
- import { outdatedConfigMessage, ProjectRoot, ConfigFile, Folder } from "./870.js";
4
+ import { outdatedConfigMessage, ProjectRoot, ConfigFile, Folder } from "./780.js";
5
5
  import { scaffoldNewDomainService, scaffoldNewErrorUseCase, scaffoldNewAggregateUseCase, scaffoldNewQuery, scaffoldNewInfrastructureService, scaffoldNewHandlersRestEndpoint, savePendingChanges, moveBackendUnitUseCase, scaffoldNewCommand, scaffoldNewPresentationService, deleteBackendUnitUseCase, scaffoldNewApplicationService, scaffoldNewUseCase, scaffoldNewImplementation, wireDependency, scaffoldNewBoundedContextUseCase, scaffoldNewDomainObject, scaffoldNewApplicationPort } from "./287.js";
6
6
  const main = async ()=>{
7
7
  if (!ConfigFile.exists) {
package/dist/init.cjs CHANGED
@@ -1529,6 +1529,13 @@ const initializeUseCase = async (section)=>{
1529
1529
  return nextSteps;
1530
1530
  };
1531
1531
  Symbol('enter-implementation-type-once');
1532
+ new Set([
1533
+ 'get',
1534
+ 'post',
1535
+ 'put',
1536
+ 'patch',
1537
+ 'delete'
1538
+ ].map((method)=>`${method.toUpperCase()}.ts`));
1532
1539
  const help = `Usage: df-init [--section project|backend|frontend]
1533
1540
 
1534
1541
  Configures project, backend and frontend in order. Any section
package/dist/init.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { parseArgs } from "node:util";
3
- import { ConfigFile, outdatedConfigMessage } from "./870.js";
3
+ import { ConfigFile, outdatedConfigMessage } from "./780.js";
4
4
  import { printNextSteps, configurationSections, initializeUseCase, savePendingChanges } from "./287.js";
5
5
  const help = `Usage: df-init [--section project|backend|frontend]
6
6
 
@@ -1419,7 +1419,31 @@ const scaffoldReactPage = async (presentationReactFolder)=>{
1419
1419
  if (usesNext()) scaffoldNextRoute(nestedName, pageFile);
1420
1420
  };
1421
1421
  const openapiExtractorPath = "scripts/extract-open-api-data-from-endpoint.ts";
1422
- const rootFolderSegments = (rootFolder)=>rootFolder.split(/[\\/]/).map((segment)=>segment.trim()).filter((segment)=>!!segment && '.' !== segment).map((segment)=>`'${segment}',\n `).join('');
1422
+ const rootFolderSegments = (rootFolder, indent = 20)=>rootFolder.split(/[\\/]/).map((segment)=>segment.trim()).filter((segment)=>!!segment && '.' !== segment).map((segment)=>`'${segment}',\n${' '.repeat(indent)}`).join('');
1423
+ const endpointFileLookup = (rootFolder)=>` const [currentBoundedContext, ...relativePath] = routeMetadata.path;
1424
+
1425
+ if (!currentBoundedContext) {
1426
+ return undefined;
1427
+ }
1428
+
1429
+ // A controller is the first folder of the path; other routes
1430
+ // belong to the context root in presentation/rest/(root).
1431
+ const restFolder = path.join(
1432
+ ${rootFolderSegments(rootFolder, 16)}'bounded-contexts',
1433
+ currentBoundedContext,
1434
+ 'presentation',
1435
+ 'rest'
1436
+ );
1437
+ const endpointPath = [
1438
+ ...relativePath.map(pathParameterAsName),
1439
+ \`\${routeMetadata.method.toUpperCase()}.ts\`
1440
+ ];
1441
+ const endpointFile =
1442
+ project.getSourceFile(path.join(restFolder, ...endpointPath)) ??
1443
+ project.getSourceFile(
1444
+ path.join(restFolder, '(root)', ...endpointPath)
1445
+ );
1446
+ `;
1423
1447
  const openapiExtractor = (rootFolder)=>`import path from 'node:path';
1424
1448
  import {
1425
1449
  type DocNode,
@@ -1531,25 +1555,7 @@ export const createOpenApiDataFromEndpointLogicExtractor = ({
1531
1555
  path: readonly string[];
1532
1556
  }): OpenApiData | undefined => {
1533
1557
  try {
1534
- const [currentBoundedContext, controller, ...relativePath] =
1535
- routeMetadata.path;
1536
-
1537
- if (!currentBoundedContext || !controller) {
1538
- return undefined;
1539
- }
1540
-
1541
- const endpointFile = project.getSourceFile(
1542
- path.join(
1543
- ${rootFolderSegments(rootFolder)}'bounded-contexts',
1544
- currentBoundedContext,
1545
- 'presentation',
1546
- 'rest',
1547
- controller,
1548
- ...relativePath.map(pathParameterAsName),
1549
- \`\${routeMetadata.method.toUpperCase()}.ts\`
1550
- )
1551
- );
1552
-
1558
+ ${endpointFileLookup(rootFolder)}
1553
1559
  if (!endpointFile) {
1554
1560
  return undefined;
1555
1561
  }
@@ -3200,7 +3206,7 @@ const scaffoldRequestSchema = external_zod_default().discriminatedUnion('operati
3200
3206
  agent_requests_operation('endpoint', {
3201
3207
  ...common,
3202
3208
  ...agent_requests_owner,
3203
- controller: agent_requests_name,
3209
+ controller: agent_requests_name.optional().describe('Omit for the context-root controller (root): `/<context>/<path>`.'),
3204
3210
  method: external_zod_default()["enum"]([
3205
3211
  'get',
3206
3212
  'post',
@@ -4236,6 +4242,35 @@ const deleteReactModule = (projectRoot, reactFolder, module, route)=>{
4236
4242
  ]
4237
4243
  };
4238
4244
  };
4245
+ const rootControllerFolder = '(root)';
4246
+ const pathParameterAsName = (segment)=>segment.startsWith(':') ? `_${segment.slice(1)}_` : segment;
4247
+ const isPathParameterName = (name)=>name.startsWith(':') || /^_.+_$/.test(name);
4248
+ const endpointFileNames = new Set([
4249
+ 'get',
4250
+ 'post',
4251
+ 'put',
4252
+ 'patch',
4253
+ 'delete'
4254
+ ].map((method)=>`${method.toUpperCase()}.ts`));
4255
+ const rest_layout_entries = (folder)=>external_node_fs_default().existsSync(folder) ? external_node_fs_default().readdirSync(folder, {
4256
+ withFileTypes: true
4257
+ }) : [];
4258
+ const rest_layout_subfolders = (folder)=>rest_layout_entries(folder).filter((entry)=>entry.isDirectory()).map((entry)=>entry.name);
4259
+ const restFolders = (boundedContextFolder)=>({
4260
+ presentation: external_node_path_default().join(boundedContextFolder, 'presentation', 'rest'),
4261
+ wiring: external_node_path_default().join(boundedContextFolder, 'wiring', 'presentation', 'rest')
4262
+ });
4263
+ const restControllers = (boundedContextFolder)=>rest_layout_subfolders(restFolders(boundedContextFolder).presentation).filter((name)=>name !== rootControllerFolder && !isPathParameterName(name));
4264
+ const rootStaticSegments = (boundedContextFolder)=>rest_layout_subfolders(external_node_path_default().join(restFolders(boundedContextFolder).presentation, rootControllerFolder)).filter((name)=>!isPathParameterName(name));
4265
+ const legacyRootEndpoints = (boundedContextFolder)=>{
4266
+ const { presentation, wiring } = restFolders(boundedContextFolder);
4267
+ return {
4268
+ endpointFiles: rest_layout_entries(presentation).filter((entry)=>entry.isFile() && endpointFileNames.has(entry.name)).map((entry)=>external_node_path_default().join(presentation, entry.name)),
4269
+ parameterFolders: rest_layout_subfolders(presentation).filter(isPathParameterName).map((name)=>external_node_path_default().join(presentation, name)),
4270
+ wireFiles: rest_layout_entries(wiring).filter((entry)=>entry.isFile() && 'index.ts' !== entry.name && /\.ts$/.test(entry.name)).map((entry)=>external_node_path_default().join(wiring, entry.name))
4271
+ };
4272
+ };
4273
+ const hasLegacyRootEndpoints = (boundedContextFolder)=>Object.values(legacyRootEndpoints(boundedContextFolder)).some((paths)=>paths.length > 0);
4239
4274
  const systemImplementationChoices = (kind, crossContext = false)=>{
4240
4275
  if ('ports' === kind) return crossContext ? [
4241
4276
  {
@@ -5130,47 +5165,49 @@ export const ${names.namespace(ClassName)} = errorNamespace('${boundedContextFol
5130
5165
  const namespaceFile = currentErrorFolder.file(`${namespace}.ts`);
5131
5166
  namespaceFile.addLineIfMissing(errorClass(names.error(`${namespaceNaming.ClassName}${ClassName}`), exportedNamespace(namespaceFile, names.namespace(namespaceNaming.ClassName)), fileName), '\n\n');
5132
5167
  };
5168
+ const newController = 'New controller';
5169
+ const rootControllerChoice = 'No controller (context root)';
5170
+ const sharedRestFolder = (boundedContextFolder)=>boundedContextFolder.subitem([
5171
+ '..',
5172
+ '..',
5173
+ 'presentation',
5174
+ 'rest'
5175
+ ]);
5176
+ const legacyRootEndpointsMessage = (context)=>`${context} keeps its context-root endpoints in the 8.5 layout (presentation/rest/<METHOD>.ts, _param_/). Run npx df-upgrade to move them to presentation/rest/(root)/ first.`;
5133
5177
  const scaffoldNewHandlersRestEndpoint = async (boundedContextFolder)=>{
5178
+ const context = boundedContextFolder.name;
5134
5179
  const restPresentationFolder = boundedContextFolder.subitem([
5135
5180
  'presentation',
5136
5181
  'rest'
5137
5182
  ]);
5138
- const controllers = restPresentationFolder.content.subfolderNames;
5139
- let controller = await interaction_select({
5183
+ const controllers = restControllers(boundedContextFolder.path);
5184
+ const rootSegments = rootStaticSegments(boundedContextFolder.path);
5185
+ let controllerChoice = await interaction_select({
5140
5186
  key: 'controllerSelection',
5141
5187
  message: 'Controller: ',
5142
5188
  choices: [
5189
+ {
5190
+ value: rootControllerChoice,
5191
+ name: `(root) — /${context}`
5192
+ },
5143
5193
  ...controllers,
5144
- 'New controller'
5194
+ newController
5145
5195
  ]
5146
5196
  });
5147
- if ('New controller' === controller) {
5148
- controller = await interaction_input({
5149
- key: 'controllerName',
5150
- message: 'Name: '
5151
- });
5152
- if (config_file_ConfigFile.backend.domainFirstPackages.includes('@domain-first/wire')) {
5153
- boundedContextFolder.subitem([
5154
- 'wiring',
5155
- 'presentation',
5156
- 'rest'
5157
- ]).file('index.ts').addLine(`export * from './${controller}'`);
5158
- const sharedRestFolder = boundedContextFolder.subitem([
5159
- '..',
5160
- '..',
5161
- 'presentation',
5162
- 'rest'
5163
- ]);
5164
- sharedRestFolder.file('wires.ts').addLine(`export * from '../../bounded-contexts/${boundedContextFolder.name}/wiring/presentation/rest/${controller}'`);
5165
- if (!sharedRestFolder.file('index.ts').exists) sharedRestFolder.createFile('index.ts', `
5166
- import * as wires from './wires'
5167
-
5168
- const restWires = Object.values(wires)
5169
-
5170
- export const restHandlers = restWires.map(x => x().handle)
5171
- `.trim());
5197
+ if (controllerChoice === rootControllerChoice && hasLegacyRootEndpoints(boundedContextFolder.path)) throw new RequestError('UPGRADE_REQUIRED', legacyRootEndpointsMessage(context));
5198
+ if (controllerChoice === newController) controllerChoice = await interaction_input({
5199
+ key: 'controllerName',
5200
+ message: 'Name: ',
5201
+ validate: (value)=>{
5202
+ if (!value.trim()) return 'Name is required; pick "(root)" for an endpoint at the context root';
5203
+ if (isPathParameterName(value) || value.includes('/') || /[()]/.test(value)) return 'A controller is one static path segment';
5204
+ if (value === context) return `"${value}" is the context itself: pick "(root)" for /${context}`;
5205
+ if (rootSegments.includes(value)) return `/${context}/${value} belongs to the context root ((root)/${value}): add the endpoint there`;
5206
+ return true;
5172
5207
  }
5173
- }
5208
+ });
5209
+ const controller = controllerChoice === rootControllerChoice ? void 0 : controllerChoice;
5210
+ const withWiring = config_file_ConfigFile.backend.domainFirstPackages.includes('@domain-first/wire');
5174
5211
  const method = await interaction_select({
5175
5212
  key: 'method',
5176
5213
  message: 'Method:',
@@ -5184,21 +5221,38 @@ export const restHandlers = restWires.map(x => x().handle)
5184
5221
  });
5185
5222
  const routeSegments = (await interaction_input({
5186
5223
  key: 'path',
5187
- message: 'Relative path (split by "/"):'
5224
+ message: 'Relative path (split by "/"):',
5225
+ validate: (value)=>{
5226
+ const [first] = value.split('/').filter((x)=>!!x);
5227
+ return controller || !first || first.startsWith(':') || !controllers.includes(first) ? true : `/${context}/${first} belongs to controller "${first}": add the endpoint there`;
5228
+ }
5188
5229
  })).split('/').filter((x)=>!!x);
5189
- const pathParameterAsName = (segment)=>segment.startsWith(':') ? `_${segment.slice(1)}_` : segment;
5190
- const controllerFolder = restPresentationFolder.subitem([
5191
- controller
5192
- ]);
5193
- const endpointFolder = controllerFolder.subitem(routeSegments.map(pathParameterAsName));
5194
- const nameBase = `${controller}-${routeSegments.length ? `${routeSegments.map(pathParameterAsName).join('-')}-` : ''}${method}-endpoint`;
5230
+ const group = controller ?? "(root)";
5231
+ const folderSegments = [
5232
+ group,
5233
+ ...routeSegments
5234
+ ];
5235
+ const routePath = [
5236
+ context,
5237
+ ...controller ? [
5238
+ controller
5239
+ ] : [],
5240
+ ...routeSegments
5241
+ ];
5242
+ const endpointFolder = restPresentationFolder.subitem(folderSegments.map(pathParameterAsName));
5243
+ const nameSegments = [
5244
+ controller ?? context,
5245
+ ...routeSegments
5246
+ ];
5247
+ const nameBase = `${[
5248
+ ...nameSegments.map(pathParameterAsName),
5249
+ method
5250
+ ].join('-')}-endpoint`;
5195
5251
  const endpointClassName = [
5196
- controller,
5197
- ...routeSegments,
5252
+ ...nameSegments,
5198
5253
  method,
5199
5254
  'endpoint'
5200
5255
  ].map((segment)=>segment.startsWith(':') ? pathParameterAsName(segment) : new UnknownFormatNaming(segment).ClassName).join('');
5201
- const withWiring = config_file_ConfigFile.backend.domainFirstPackages.includes('@domain-first/wire');
5202
5256
  const endpointGenerator = findRestEndpointGenerator(boundedContextFolder.subitem([
5203
5257
  '..',
5204
5258
  '..',
@@ -5218,8 +5272,8 @@ export class ${endpointClassName} {
5218
5272
 
5219
5273
  get handle() {
5220
5274
  return this.endpointGenerator(this.handler.handle, {
5221
- route: { method: '${method}', path: ['${boundedContextFolder.name}', '${controller}'${routeSegments.length ? `, ${routeSegments.map((x)=>`'${x}'`).join(', ')}` : ''}] },
5222
- tags: ['${boundedContextFolder.name}: ${controller}'],
5275
+ route: { method: '${method}', path: [${routePath.map((x)=>`'${x}'`).join(', ')}] },
5276
+ tags: ['${context}${controller ? `: ${controller}` : ''}'],
5223
5277
  })
5224
5278
  }
5225
5279
  }
@@ -5229,7 +5283,7 @@ export class ${endpointClassName} {
5229
5283
  'wiring',
5230
5284
  'presentation',
5231
5285
  'rest',
5232
- controller
5286
+ group
5233
5287
  ]);
5234
5288
  const wireNameBase = `wire-${nameBase}`;
5235
5289
  const wireNaming = new UnknownFormatNaming(wireNameBase);
@@ -5249,6 +5303,20 @@ export const wire${endpointClassName} = wireClass(
5249
5303
  )
5250
5304
  `.trim());
5251
5305
  wiringFolder.file('index.ts').addLineIfMissing(`export * from './${wireNaming.fileName}'`);
5306
+ boundedContextFolder.subitem([
5307
+ 'wiring',
5308
+ 'presentation',
5309
+ 'rest'
5310
+ ]).file('index.ts').addLineIfMissing(`export * from './${group}'`);
5311
+ sharedRestFolder(boundedContextFolder).file('wires.ts').addLineIfMissing(`export * from '../../bounded-contexts/${context}/wiring/presentation/rest/${group}'`);
5312
+ const restHandlersFile = sharedRestFolder(boundedContextFolder).file('index.ts');
5313
+ if (!restHandlersFile.exists) sharedRestFolder(boundedContextFolder).createFile('index.ts', `
5314
+ import * as wires from './wires'
5315
+
5316
+ const restWires = Object.values(wires)
5317
+
5318
+ export const restHandlers = restWires.map(x => x().handle)
5319
+ `.trim());
5252
5320
  }
5253
5321
  };
5254
5322
  const scaffold_new_implementation_kindTitles = {
@@ -5888,12 +5956,12 @@ const answersFor = (command, request, owner)=>{
5888
5956
  if ('error' === request.operation || 'error-namespace' === request.operation) defaults.errorAction = 'error-namespace' === request.operation ? 'New namespace' : request.namespace ? `New error in namespace ${request.namespace}` : 'New error without namespace';
5889
5957
  if ('endpoint' === request.operation) {
5890
5958
  delete answers.controller;
5891
- const existing = owner.subitem([
5892
- 'presentation',
5893
- 'rest'
5894
- ]).content.subfolderNames.includes(request.controller);
5895
- answers.controllerSelection = existing ? request.controller : 'New controller';
5896
- if (!existing) answers.controllerName = request.controller;
5959
+ if (void 0 === request.controller) answers.controllerSelection = rootControllerChoice;
5960
+ else {
5961
+ const existing = restControllers(owner.path).includes(request.controller);
5962
+ answers.controllerSelection = existing ? request.controller : 'New controller';
5963
+ if (!existing) answers.controllerName = request.controller;
5964
+ }
5897
5965
  }
5898
5966
  if ('move' === request.operation) defaults.targetContext = request.context;
5899
5967
  }
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { parseArgs } from "node:util";
3
3
  import { runCLI } from "./561.js";
4
- import { ConfigFile, outdatedConfigMessage } from "./870.js";
4
+ import { ConfigFile, outdatedConfigMessage } from "./780.js";
5
5
  import { addIntegration } from "./287.js";
6
6
  runCLI('df-integration', async ()=>{
7
7
  const help = `Usage: df-integration
@@ -1,2 +1,14 @@
1
1
  export declare const openapiExtractorPath = "scripts/extract-open-api-data-from-endpoint.ts";
2
+ /**
3
+ * How 8.5.1 found the endpoint file, as scaffolded: context-root endpoints
4
+ * right in `presentation/rest/`. `df-upgrade` replaces it with
5
+ * `endpointFileLookup`.
6
+ */
7
+ export declare const legacyEndpointFileLookup: (rootFolder: string) => string;
8
+ /**
9
+ * A controller is the first folder of the route; any other route belongs
10
+ * to the context-root controller in `presentation/rest/(root)/`. The
11
+ * generator keeps the two apart, so at most one of the files exists.
12
+ */
13
+ export declare const endpointFileLookup: (rootFolder: string) => string;
2
14
  export declare const openapiExtractor: (rootFolder: string) => string;
package/dist/lint.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { parseArgs } from "node:util";
3
- import { configuredDependencyRules, ProjectRoot, outdatedConfigMessage, ConfigFile } from "./870.js";
3
+ import { configuredDependencyRules, ProjectRoot, outdatedConfigMessage, ConfigFile } from "./780.js";
4
4
  import { lintProject, formatViolations } from "./38.js";
5
5
  const help = `Usage: df-lint [rootFolder]
6
6