@domain-first/project-structure 2.16.0 → 2.16.2

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/README.md CHANGED
@@ -209,8 +209,9 @@ folders. A widget moves with its whole folder (assets included), and a page with
209
209
  the files next to it that share its name, such as `<name>-page.module.css`. File
210
210
  names, `<Name>Page`, `<Name>Widget…` and `use<Name>UIModel` follow the new name,
211
211
  and so does a generated story title. Imports of the module are updated across
212
- the project, both relative ones and `tsconfig.json` `paths` aliases such as `@/`,
213
- and relative imports inside the module are adjusted to its new location. The CLI
212
+ the project, both relative ones and `tsconfig.json` `paths` aliases such as `@/`;
213
+ files importing it through barrels (`export * from` / `export { … } from`) get
214
+ the renamed classes too, and relative imports inside the module are adjusted to its new location. The CLI
214
215
  lists the files to move and to update and asks before writing; emptied folders
215
216
  are removed. Entering the same name renames files made before file names used
216
217
  the whole path, for example `pages/admin/users-page.tsx` to
package/dist/index.cjs CHANGED
@@ -1217,6 +1217,16 @@ const readAliases = (projectRoot)=>{
1217
1217
  }
1218
1218
  };
1219
1219
  const specifierPattern = /(\bfrom\s*|\bimport\s*\(\s*|\bimport\s+|\brequire\s*\(\s*|\bmock\s*\(\s*)(['"])([^'"\n]+)\2/g;
1220
+ const reExportPattern = /\bexport\s+(?:type\s+)?(?:\*(?:\s+as\s+[\w$]+)?|\{[^}]*\})\s*from\s*(['"])([^'"\n]+)\1/g;
1221
+ const importPaths = (file)=>{
1222
+ const withoutExtension = file.replace(codeExtension, '');
1223
+ return 'index' === external_node_path_default().basename(withoutExtension) ? [
1224
+ withoutExtension,
1225
+ external_node_path_default().dirname(withoutExtension)
1226
+ ] : [
1227
+ withoutExtension
1228
+ ];
1229
+ };
1220
1230
  const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
1221
1231
  const newName = new NestedName(rawName);
1222
1232
  const target = react_module_move_targetPath(reactFolder, module, rawName);
@@ -1263,13 +1273,35 @@ const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
1263
1273
  'Widgets',
1264
1274
  ...newName.classNameSegments
1265
1275
  ].join('/');
1276
+ const codeFiles = new Map(react_module_move_walk(projectRoot).filter((file)=>codeExtension.test(file)).map((file)=>[
1277
+ file,
1278
+ external_node_fs_default().readFileSync(file, 'utf8')
1279
+ ]));
1280
+ const barrels = new Set();
1281
+ const isBarrel = (resolved)=>barrels.has(resolved.replace(codeExtension, ''));
1282
+ for(let found = true; found;){
1283
+ found = false;
1284
+ for (const [file, content] of codeFiles){
1285
+ if (moves.has(file) || isBarrel(file)) continue;
1286
+ const reExportsModule = [
1287
+ ...content.matchAll(reExportPattern)
1288
+ ].some(([, , specifier])=>{
1289
+ const resolution = resolve(specifier, file);
1290
+ return !!resolution && (!!movedPath(resolution.resolved) || isBarrel(resolution.resolved));
1291
+ });
1292
+ if (reExportsModule) {
1293
+ for (const importPath of importPaths(file))barrels.add(importPath);
1294
+ found = true;
1295
+ }
1296
+ }
1297
+ }
1266
1298
  const rewrite = (content, oldFile, newFile)=>{
1267
1299
  let referencesModule = false;
1268
1300
  const rewritten = content.replace(specifierPattern, (match, keyword, quote, specifier)=>{
1269
1301
  const resolution = resolve(specifier, oldFile);
1270
1302
  if (!resolution) return match;
1271
1303
  const moved = movedPath(resolution.resolved);
1272
- if (moved) referencesModule = true;
1304
+ if (moved || isBarrel(resolution.resolved)) referencesModule = true;
1273
1305
  if (!moved && oldFile === newFile) return match;
1274
1306
  const next = specifierFor(newFile, moved ?? resolution.resolved, resolution.alias);
1275
1307
  return `${keyword}${quote}${next}${quote}`;
@@ -1278,14 +1310,10 @@ const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
1278
1310
  const renamed = rewritten.replace(classPattern, (_, use = '')=>`${use}${newClassName}`);
1279
1311
  return moves.has(oldFile) && 'widget' === module.kind ? renamed.replace(titlePattern, (_, quote)=>`${quote}${newTitle}${quote}`) : renamed;
1280
1312
  };
1313
+ for (const [file, newFile] of moves)if (!codeFiles.has(file) && newFile !== file) new File(file).moveTo(newFile, reactFolder);
1281
1314
  const updated = [];
1282
- for (const file of react_module_move_walk(projectRoot)){
1315
+ for (const [file, content] of codeFiles){
1283
1316
  const newFile = moves.get(file);
1284
- if (!codeExtension.test(file)) {
1285
- if (newFile && newFile !== file) new File(file).moveTo(newFile, reactFolder);
1286
- continue;
1287
- }
1288
- const content = external_node_fs_default().readFileSync(file, 'utf8');
1289
1317
  const next = rewrite(content, file, newFile ?? file);
1290
1318
  if (newFile && newFile !== file) {
1291
1319
  new File(newFile).data = next;
@@ -2455,12 +2483,18 @@ export const restHandlers = restWires.map(x => x().handle)
2455
2483
  const routeSegments = (await (0, prompts_namespaceObject.input)({
2456
2484
  message: 'Relative path (split by "/"):'
2457
2485
  })).split('/').filter((x)=>!!x);
2486
+ const pathParameterAsName = (segment)=>segment.startsWith(':') ? `_${segment.slice(1)}_` : segment;
2458
2487
  const controllerFolder = restPresentationFolder.subitem([
2459
2488
  controller
2460
2489
  ]);
2461
- const endpointFolder = controllerFolder.subitem(routeSegments);
2462
- const nameBase = `${controller}-${routeSegments.length ? `${routeSegments.join('-')}-` : ''}${method}-endpoint`;
2463
- const endpointNaming = new UnknownFormatNaming(nameBase);
2490
+ const endpointFolder = controllerFolder.subitem(routeSegments.map(pathParameterAsName));
2491
+ const nameBase = `${controller}-${routeSegments.length ? `${routeSegments.map(pathParameterAsName).join('-')}-` : ''}${method}-endpoint`;
2492
+ const endpointClassName = [
2493
+ controller,
2494
+ ...routeSegments,
2495
+ method,
2496
+ 'endpoint'
2497
+ ].map((segment)=>segment.startsWith(':') ? pathParameterAsName(segment) : new UnknownFormatNaming(segment).ClassName).join('');
2464
2498
  const withWiring = ConfigFile.Instance.data.domainFirstPackages.includes('@domain-first/wire');
2465
2499
  const { endpointGenerator, handler } = withWiring ? await promptEndpointDependencies(boundedContextFolder) : {
2466
2500
  endpointGenerator: void 0,
@@ -2471,7 +2505,7 @@ export const restHandlers = restWires.map(x => x().handle)
2471
2505
  ${endpointGenerator ? `import type { ${endpointGenerator.typeName} } from '${relativeImport(endpointFile, endpointGenerator.typeFile)}'` : "import { EndpointGenerator } from '@domain-first/handlers-rest'"}${handler ? `
2472
2506
  import type { ${handler.typeName} } from '${relativeImport(endpointFile, handler.typeFile)}'` : ''}
2473
2507
 
2474
- export class ${endpointNaming.ClassName} {
2508
+ export class ${endpointClassName} {
2475
2509
  constructor(
2476
2510
  private readonly endpointGenerator: ${endpointGenerator?.typeName ?? 'EndpointGenerator'},
2477
2511
  private readonly handler: ${handler?.typeName ?? 'never'}
@@ -2501,11 +2535,11 @@ export class ${endpointNaming.ClassName} {
2501
2535
  ].filter((x)=>!!x) : [];
2502
2536
  wiringFolder.createFile(`${wireNaming.fileName}.ts`, `
2503
2537
  import { wireClass } from '@domain-first/wire'
2504
- import { ${endpointNaming.ClassName} } from '${relativeImport(wireFile, endpointFile)}'
2538
+ import { ${endpointClassName} } from '${relativeImport(wireFile, endpointFile)}'
2505
2539
  ${dependencies.map((x)=>`import { ${x.wireName} } from '${relativeImport(wireFile, x.wireFile)}'`).join('\n')}
2506
2540
 
2507
- export const wire${endpointNaming.ClassName} = wireClass(
2508
- ${endpointNaming.ClassName},
2541
+ export const wire${endpointClassName} = wireClass(
2542
+ ${endpointClassName},
2509
2543
  [${dependencies.map((x)=>x.wireName).join(', ')}]
2510
2544
  )
2511
2545
  `.trim());
package/dist/index.js CHANGED
@@ -1187,6 +1187,16 @@ const readAliases = (projectRoot)=>{
1187
1187
  }
1188
1188
  };
1189
1189
  const specifierPattern = /(\bfrom\s*|\bimport\s*\(\s*|\bimport\s+|\brequire\s*\(\s*|\bmock\s*\(\s*)(['"])([^'"\n]+)\2/g;
1190
+ const reExportPattern = /\bexport\s+(?:type\s+)?(?:\*(?:\s+as\s+[\w$]+)?|\{[^}]*\})\s*from\s*(['"])([^'"\n]+)\1/g;
1191
+ const importPaths = (file)=>{
1192
+ const withoutExtension = file.replace(codeExtension, '');
1193
+ return 'index' === node_path.basename(withoutExtension) ? [
1194
+ withoutExtension,
1195
+ node_path.dirname(withoutExtension)
1196
+ ] : [
1197
+ withoutExtension
1198
+ ];
1199
+ };
1190
1200
  const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
1191
1201
  const newName = new NestedName(rawName);
1192
1202
  const target = react_module_move_targetPath(reactFolder, module, rawName);
@@ -1233,13 +1243,35 @@ const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
1233
1243
  'Widgets',
1234
1244
  ...newName.classNameSegments
1235
1245
  ].join('/');
1246
+ const codeFiles = new Map(react_module_move_walk(projectRoot).filter((file)=>codeExtension.test(file)).map((file)=>[
1247
+ file,
1248
+ node_fs.readFileSync(file, 'utf8')
1249
+ ]));
1250
+ const barrels = new Set();
1251
+ const isBarrel = (resolved)=>barrels.has(resolved.replace(codeExtension, ''));
1252
+ for(let found = true; found;){
1253
+ found = false;
1254
+ for (const [file, content] of codeFiles){
1255
+ if (moves.has(file) || isBarrel(file)) continue;
1256
+ const reExportsModule = [
1257
+ ...content.matchAll(reExportPattern)
1258
+ ].some(([, , specifier])=>{
1259
+ const resolution = resolve(specifier, file);
1260
+ return !!resolution && (!!movedPath(resolution.resolved) || isBarrel(resolution.resolved));
1261
+ });
1262
+ if (reExportsModule) {
1263
+ for (const importPath of importPaths(file))barrels.add(importPath);
1264
+ found = true;
1265
+ }
1266
+ }
1267
+ }
1236
1268
  const rewrite = (content, oldFile, newFile)=>{
1237
1269
  let referencesModule = false;
1238
1270
  const rewritten = content.replace(specifierPattern, (match, keyword, quote, specifier)=>{
1239
1271
  const resolution = resolve(specifier, oldFile);
1240
1272
  if (!resolution) return match;
1241
1273
  const moved = movedPath(resolution.resolved);
1242
- if (moved) referencesModule = true;
1274
+ if (moved || isBarrel(resolution.resolved)) referencesModule = true;
1243
1275
  if (!moved && oldFile === newFile) return match;
1244
1276
  const next = specifierFor(newFile, moved ?? resolution.resolved, resolution.alias);
1245
1277
  return `${keyword}${quote}${next}${quote}`;
@@ -1248,14 +1280,10 @@ const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
1248
1280
  const renamed = rewritten.replace(classPattern, (_, use = '')=>`${use}${newClassName}`);
1249
1281
  return moves.has(oldFile) && 'widget' === module.kind ? renamed.replace(titlePattern, (_, quote)=>`${quote}${newTitle}${quote}`) : renamed;
1250
1282
  };
1283
+ for (const [file, newFile] of moves)if (!codeFiles.has(file) && newFile !== file) new File(file).moveTo(newFile, reactFolder);
1251
1284
  const updated = [];
1252
- for (const file of react_module_move_walk(projectRoot)){
1285
+ for (const [file, content] of codeFiles){
1253
1286
  const newFile = moves.get(file);
1254
- if (!codeExtension.test(file)) {
1255
- if (newFile && newFile !== file) new File(file).moveTo(newFile, reactFolder);
1256
- continue;
1257
- }
1258
- const content = node_fs.readFileSync(file, 'utf8');
1259
1287
  const next = rewrite(content, file, newFile ?? file);
1260
1288
  if (newFile && newFile !== file) {
1261
1289
  new File(newFile).data = next;
@@ -2425,12 +2453,18 @@ export const restHandlers = restWires.map(x => x().handle)
2425
2453
  const routeSegments = (await input({
2426
2454
  message: 'Relative path (split by "/"):'
2427
2455
  })).split('/').filter((x)=>!!x);
2456
+ const pathParameterAsName = (segment)=>segment.startsWith(':') ? `_${segment.slice(1)}_` : segment;
2428
2457
  const controllerFolder = restPresentationFolder.subitem([
2429
2458
  controller
2430
2459
  ]);
2431
- const endpointFolder = controllerFolder.subitem(routeSegments);
2432
- const nameBase = `${controller}-${routeSegments.length ? `${routeSegments.join('-')}-` : ''}${method}-endpoint`;
2433
- const endpointNaming = new UnknownFormatNaming(nameBase);
2460
+ const endpointFolder = controllerFolder.subitem(routeSegments.map(pathParameterAsName));
2461
+ const nameBase = `${controller}-${routeSegments.length ? `${routeSegments.map(pathParameterAsName).join('-')}-` : ''}${method}-endpoint`;
2462
+ const endpointClassName = [
2463
+ controller,
2464
+ ...routeSegments,
2465
+ method,
2466
+ 'endpoint'
2467
+ ].map((segment)=>segment.startsWith(':') ? pathParameterAsName(segment) : new UnknownFormatNaming(segment).ClassName).join('');
2434
2468
  const withWiring = ConfigFile.Instance.data.domainFirstPackages.includes('@domain-first/wire');
2435
2469
  const { endpointGenerator, handler } = withWiring ? await promptEndpointDependencies(boundedContextFolder) : {
2436
2470
  endpointGenerator: void 0,
@@ -2441,7 +2475,7 @@ export const restHandlers = restWires.map(x => x().handle)
2441
2475
  ${endpointGenerator ? `import type { ${endpointGenerator.typeName} } from '${relativeImport(endpointFile, endpointGenerator.typeFile)}'` : "import { EndpointGenerator } from '@domain-first/handlers-rest'"}${handler ? `
2442
2476
  import type { ${handler.typeName} } from '${relativeImport(endpointFile, handler.typeFile)}'` : ''}
2443
2477
 
2444
- export class ${endpointNaming.ClassName} {
2478
+ export class ${endpointClassName} {
2445
2479
  constructor(
2446
2480
  private readonly endpointGenerator: ${endpointGenerator?.typeName ?? 'EndpointGenerator'},
2447
2481
  private readonly handler: ${handler?.typeName ?? 'never'}
@@ -2471,11 +2505,11 @@ export class ${endpointNaming.ClassName} {
2471
2505
  ].filter((x)=>!!x) : [];
2472
2506
  wiringFolder.createFile(`${wireNaming.fileName}.ts`, `
2473
2507
  import { wireClass } from '@domain-first/wire'
2474
- import { ${endpointNaming.ClassName} } from '${relativeImport(wireFile, endpointFile)}'
2508
+ import { ${endpointClassName} } from '${relativeImport(wireFile, endpointFile)}'
2475
2509
  ${dependencies.map((x)=>`import { ${x.wireName} } from '${relativeImport(wireFile, x.wireFile)}'`).join('\n')}
2476
2510
 
2477
- export const wire${endpointNaming.ClassName} = wireClass(
2478
- ${endpointNaming.ClassName},
2511
+ export const wire${endpointClassName} = wireClass(
2512
+ ${endpointClassName},
2479
2513
  [${dependencies.map((x)=>x.wireName).join(', ')}]
2480
2514
  )
2481
2515
  `.trim());
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@domain-first/project-structure",
3
3
  "description": "Build and manage your Domain-First project structure with CLI",
4
- "version": "2.16.0",
4
+ "version": "2.16.2",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",