@domain-first/project-structure 2.18.1 → 3.0.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/README.md +36 -2
- package/dist/index.cjs +554 -510
- package/dist/index.js +554 -510
- package/dist/use-cases/rest-endpoint-generator.d.ts +17 -0
- package/dist/use-cases/scaffold-new-presentation-service.d.ts +5 -0
- package/dist/use-cases/wire-dependency.d.ts +3 -8
- package/dist/wiring.d.ts +0 -4
- package/package.json +1 -1
- package/skills/df-ps-implement/SKILL.md +2 -2
- package/skills/df-ps-scaffold/SKILL.md +1 -1
- package/skills/df-ps-scaffold/references/generators.md +5 -5
package/dist/index.js
CHANGED
|
@@ -1154,76 +1154,6 @@ const addIntegration = async ()=>{
|
|
|
1154
1154
|
if (sections.length) console.log(sections.join('\n\n'));
|
|
1155
1155
|
}
|
|
1156
1156
|
};
|
|
1157
|
-
const initializeUseCase = async ()=>{
|
|
1158
|
-
const rootFolderName = await input({
|
|
1159
|
-
message: 'Root folder (use "/" for inner folders):',
|
|
1160
|
-
default: 'src'
|
|
1161
|
-
});
|
|
1162
|
-
const domainFirstPackages = await prompts_checkbox({
|
|
1163
|
-
message: '@domain-first packages',
|
|
1164
|
-
choices: [
|
|
1165
|
-
'@domain-first/types',
|
|
1166
|
-
'@domain-first/errors',
|
|
1167
|
-
'@domain-first/handlers',
|
|
1168
|
-
'@domain-first/wire',
|
|
1169
|
-
'@domain-first/handlers-rest'
|
|
1170
|
-
]
|
|
1171
|
-
});
|
|
1172
|
-
const defaultPersistenceLayerImplementation = await input({
|
|
1173
|
-
message: 'Default persistence layer implementation:'
|
|
1174
|
-
});
|
|
1175
|
-
const useReact = await prompts_confirm({
|
|
1176
|
-
message: 'Use React'
|
|
1177
|
-
});
|
|
1178
|
-
let storybookFramework;
|
|
1179
|
-
const withStorybook = useReact ? await prompts_confirm({
|
|
1180
|
-
message: 'Use Storybook'
|
|
1181
|
-
}) : false;
|
|
1182
|
-
if (withStorybook) storybookFramework = await input({
|
|
1183
|
-
message: 'Storybook framework package:',
|
|
1184
|
-
default: '@storybook/react-vite'
|
|
1185
|
-
});
|
|
1186
|
-
let testingLibrary;
|
|
1187
|
-
const withTests = await prompts_confirm({
|
|
1188
|
-
message: 'Scaffold unit tests'
|
|
1189
|
-
});
|
|
1190
|
-
if (withTests) testingLibrary = await input({
|
|
1191
|
-
message: 'Unit testing library:',
|
|
1192
|
-
default: 'vitest'
|
|
1193
|
-
});
|
|
1194
|
-
ConfigFile.write({
|
|
1195
|
-
rootFolder: rootFolderName,
|
|
1196
|
-
domainFirstPackages,
|
|
1197
|
-
defaultPersistenceLayerImplementation,
|
|
1198
|
-
useReact,
|
|
1199
|
-
storybookFramework,
|
|
1200
|
-
testingLibrary
|
|
1201
|
-
});
|
|
1202
|
-
const { path } = new ProjectRoot();
|
|
1203
|
-
const rootFolder = new Folder(path);
|
|
1204
|
-
if (domainFirstPackages.includes('@domain-first/wire')) rootFolder.subitem([
|
|
1205
|
-
rootFolderName,
|
|
1206
|
-
'shared',
|
|
1207
|
-
'wiring'
|
|
1208
|
-
]).createFile('env-branched-wire.ts', `
|
|
1209
|
-
import { branchedWire } from '@domain-first/wire'
|
|
1210
|
-
|
|
1211
|
-
export const envBranchedWire = branchedWire(() => {
|
|
1212
|
-
return process.env.NODE_ENV ?? 'development' as 'test' | 'development' | 'production'
|
|
1213
|
-
});
|
|
1214
|
-
`.trim());
|
|
1215
|
-
if (domainFirstPackages.includes('@domain-first/errors')) rootFolder.subitem([
|
|
1216
|
-
rootFolderName,
|
|
1217
|
-
'shared'
|
|
1218
|
-
]).subitem([
|
|
1219
|
-
'domain',
|
|
1220
|
-
'errors'
|
|
1221
|
-
]).createFile('index.ts', `
|
|
1222
|
-
import { errorNamespace } from '@domain-first/errors'
|
|
1223
|
-
|
|
1224
|
-
export const SharedErrors = errorNamespace('shared')
|
|
1225
|
-
`.trim());
|
|
1226
|
-
};
|
|
1227
1157
|
class UnknownFormatNaming {
|
|
1228
1158
|
rawSource;
|
|
1229
1159
|
camelCase;
|
|
@@ -1252,328 +1182,65 @@ class UnknownFormatNaming {
|
|
|
1252
1182
|
return this.fileName.replaceAll('-', ' ');
|
|
1253
1183
|
}
|
|
1254
1184
|
}
|
|
1255
|
-
const
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
const ignoredFolders = new Set([
|
|
1278
|
-
'node_modules',
|
|
1279
|
-
'.git',
|
|
1280
|
-
'.next',
|
|
1281
|
-
'dist',
|
|
1282
|
-
'build',
|
|
1283
|
-
'out',
|
|
1284
|
-
'coverage',
|
|
1285
|
-
'storybook-static'
|
|
1286
|
-
]);
|
|
1287
|
-
const codeExtension = /\.(?:tsx?|mts|cts|jsx?|mjs|cjs|mdx)$/;
|
|
1288
|
-
const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1289
|
-
const react_module_move_walk = (folder)=>{
|
|
1290
|
-
if (!node_fs.existsSync(folder)) return [];
|
|
1291
|
-
return node_fs.readdirSync(folder, {
|
|
1292
|
-
withFileTypes: true
|
|
1293
|
-
}).flatMap((entry)=>{
|
|
1294
|
-
const entryPath = node_path.join(folder, entry.name);
|
|
1295
|
-
if (entry.isDirectory()) return ignoredFolders.has(entry.name) ? [] : react_module_move_walk(entryPath);
|
|
1296
|
-
return entry.isFile() ? [
|
|
1297
|
-
entryPath
|
|
1298
|
-
] : [];
|
|
1299
|
-
});
|
|
1300
|
-
};
|
|
1301
|
-
const toPosix = (value)=>value.split(node_path.sep).join('/');
|
|
1302
|
-
const nestedName = (folders, fileBase)=>{
|
|
1303
|
-
const prefix = `${folders.join('-')}-`;
|
|
1304
|
-
const last = folders.length && fileBase.startsWith(prefix) ? fileBase.slice(prefix.length) : fileBase;
|
|
1305
|
-
return [
|
|
1306
|
-
...folders,
|
|
1307
|
-
last
|
|
1308
|
-
].join('/');
|
|
1309
|
-
};
|
|
1310
|
-
const declaredClassName = (filePath, suffix, name)=>{
|
|
1311
|
-
const content = node_fs.readFileSync(filePath, 'utf8');
|
|
1312
|
-
const declared = content.match(new RegExp(`export\\s+(?:const|function)\\s+(\\w+)${suffix}\\b`))?.[1];
|
|
1313
|
-
return declared ?? new NestedName(name).ClassName;
|
|
1314
|
-
};
|
|
1315
|
-
const findReactModules = (reactFolder, kind)=>{
|
|
1316
|
-
const modules = 'page' === kind ? react_module_move_walk(node_path.join(reactFolder, 'pages')).filter((file)=>file.endsWith('-page.tsx')).map((file)=>{
|
|
1317
|
-
const folders = node_path.relative(node_path.join(reactFolder, 'pages'), node_path.dirname(file)).split(node_path.sep).filter((segment)=>!!segment);
|
|
1318
|
-
const fileBase = node_path.basename(file, '-page.tsx');
|
|
1319
|
-
const name = nestedName(folders, fileBase);
|
|
1320
|
-
return {
|
|
1321
|
-
kind,
|
|
1322
|
-
name,
|
|
1323
|
-
path: file,
|
|
1324
|
-
fileBase,
|
|
1325
|
-
ClassName: declaredClassName(file, 'Page', name)
|
|
1326
|
-
};
|
|
1327
|
-
}) : react_module_move_walk(node_path.join(reactFolder, 'widgets')).filter((file)=>file.endsWith('-widget.tsx')).map((file)=>{
|
|
1328
|
-
const folder = node_path.dirname(file);
|
|
1329
|
-
const name = toPosix(node_path.relative(node_path.join(reactFolder, 'widgets'), folder));
|
|
1330
|
-
return {
|
|
1331
|
-
kind,
|
|
1332
|
-
name,
|
|
1333
|
-
path: folder,
|
|
1334
|
-
fileBase: node_path.basename(file, '-widget.tsx'),
|
|
1335
|
-
ClassName: declaredClassName(file, 'Widget', name)
|
|
1336
|
-
};
|
|
1337
|
-
});
|
|
1338
|
-
return modules.sort((a, b)=>a.name.localeCompare(b.name));
|
|
1185
|
+
const wiring_layers = [
|
|
1186
|
+
'domain',
|
|
1187
|
+
'application',
|
|
1188
|
+
'infrastructure',
|
|
1189
|
+
'presentation'
|
|
1190
|
+
];
|
|
1191
|
+
const visibleLayers = {
|
|
1192
|
+
domain: [
|
|
1193
|
+
'domain'
|
|
1194
|
+
],
|
|
1195
|
+
application: [
|
|
1196
|
+
'domain',
|
|
1197
|
+
'application'
|
|
1198
|
+
],
|
|
1199
|
+
infrastructure: [
|
|
1200
|
+
'domain',
|
|
1201
|
+
'infrastructure'
|
|
1202
|
+
],
|
|
1203
|
+
presentation: [
|
|
1204
|
+
'application',
|
|
1205
|
+
'presentation'
|
|
1206
|
+
]
|
|
1339
1207
|
};
|
|
1340
|
-
const
|
|
1341
|
-
|
|
1342
|
-
|
|
1208
|
+
const branchedTypeFiles = {
|
|
1209
|
+
repositories: (fileBase)=>[
|
|
1210
|
+
'domain',
|
|
1211
|
+
'aggregates',
|
|
1212
|
+
fileBase.replace(/-repository$/, ''),
|
|
1213
|
+
`${fileBase}.ts`
|
|
1214
|
+
],
|
|
1215
|
+
commands: (fileBase)=>[
|
|
1216
|
+
'application',
|
|
1217
|
+
'commands',
|
|
1218
|
+
'execution',
|
|
1219
|
+
`${fileBase}.ts`
|
|
1220
|
+
],
|
|
1221
|
+
queries: (fileBase)=>[
|
|
1222
|
+
'application',
|
|
1223
|
+
'queries',
|
|
1224
|
+
'execution',
|
|
1225
|
+
`${fileBase}.ts`
|
|
1226
|
+
],
|
|
1227
|
+
'application-ports': (fileBase)=>[
|
|
1228
|
+
'application',
|
|
1229
|
+
'ports',
|
|
1230
|
+
`${fileBase}.ts`
|
|
1231
|
+
]
|
|
1343
1232
|
};
|
|
1344
|
-
const
|
|
1345
|
-
const
|
|
1346
|
-
|
|
1347
|
-
const target = react_module_move_targetPath(reactFolder, module, rawName);
|
|
1348
|
-
if (target !== module.path && node_fs.existsSync(target)) return `${node_path.relative(reactFolder, target)} already exists`;
|
|
1349
|
-
if (target.startsWith(`${module.path}${node_path.sep}`)) return 'A widget cannot be moved into itself';
|
|
1350
|
-
return true;
|
|
1233
|
+
const layerOf = (file, contextPath)=>{
|
|
1234
|
+
const [firstSegment] = node_path.relative(contextPath, file).split(node_path.sep);
|
|
1235
|
+
return wiring_layers.find((layer)=>layer === firstSegment);
|
|
1351
1236
|
};
|
|
1352
|
-
const
|
|
1353
|
-
const
|
|
1354
|
-
const
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
const { compilerOptions = {} } = JSON.parse(stripJsonComments(node_fs.readFileSync(tsconfig, 'utf8')));
|
|
1358
|
-
const base = node_path.resolve(projectRoot, compilerOptions.baseUrl ?? '.');
|
|
1359
|
-
return Object.entries(compilerOptions.paths ?? {}).filter(([pattern, targets])=>pattern.endsWith('*') && targets[0]?.endsWith('*')).map(([pattern, [target]])=>({
|
|
1360
|
-
prefix: pattern.slice(0, -1),
|
|
1361
|
-
target: node_path.resolve(base, target.slice(0, -1))
|
|
1362
|
-
}));
|
|
1363
|
-
} catch {
|
|
1364
|
-
return [];
|
|
1237
|
+
const namedImports = (source, fromFile)=>{
|
|
1238
|
+
const result = new Map();
|
|
1239
|
+
for (const [, names, importPath] of source.matchAll(/import\s*(?:type\s*)?\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g))for (const name of names.split(',')){
|
|
1240
|
+
const cleanName = name.replace(/^\s*type\s+/, '').trim();
|
|
1241
|
+
if (cleanName) result.set(cleanName, `${node_path.resolve(node_path.dirname(fromFile), importPath)}.ts`);
|
|
1365
1242
|
}
|
|
1366
|
-
|
|
1367
|
-
const specifierPattern = /(\bfrom\s*|\bimport\s*\(\s*|\bimport\s+|\brequire\s*\(\s*|\bmock\s*\(\s*)(['"])([^'"\n]+)\2/g;
|
|
1368
|
-
const reExportPattern = /\bexport\s+(?:type\s+)?(?:\*(?:\s+as\s+[\w$]+)?|\{[^}]*\})\s*from\s*(['"])([^'"\n]+)\1/g;
|
|
1369
|
-
const importPaths = (file)=>{
|
|
1370
|
-
const withoutExtension = file.replace(codeExtension, '');
|
|
1371
|
-
return 'index' === node_path.basename(withoutExtension) ? [
|
|
1372
|
-
withoutExtension,
|
|
1373
|
-
node_path.dirname(withoutExtension)
|
|
1374
|
-
] : [
|
|
1375
|
-
withoutExtension
|
|
1376
|
-
];
|
|
1377
|
-
};
|
|
1378
|
-
const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
|
|
1379
|
-
const newName = new NestedName(rawName);
|
|
1380
|
-
const target = react_module_move_targetPath(reactFolder, module, rawName);
|
|
1381
|
-
const newClassName = newName.ClassName;
|
|
1382
|
-
const renameFile = (name)=>name.replace(new RegExp(`^(use-)?${escapeRegExp(module.fileBase)}(?=[-.])`), (_, use = '')=>`${use}${newName.fileName}`);
|
|
1383
|
-
const oldFolder = 'page' === module.kind ? node_path.dirname(module.path) : module.path;
|
|
1384
|
-
const newFolder = 'page' === module.kind ? node_path.dirname(target) : target;
|
|
1385
|
-
const ownsPath = (candidate)=>'page' === module.kind ? node_path.dirname(candidate) === oldFolder && new RegExp(`^${escapeRegExp(module.fileBase)}-page(?:\\.|$)`).test(node_path.basename(candidate)) : candidate === oldFolder || candidate.startsWith(`${oldFolder}${node_path.sep}`);
|
|
1386
|
-
const movedPath = (candidate)=>{
|
|
1387
|
-
if (!ownsPath(candidate)) return;
|
|
1388
|
-
const segments = node_path.relative(oldFolder, candidate).split(node_path.sep);
|
|
1389
|
-
const last = segments.length - 1;
|
|
1390
|
-
segments[last] = renameFile(segments[last]);
|
|
1391
|
-
return node_path.join(newFolder, ...segments);
|
|
1392
|
-
};
|
|
1393
|
-
const moduleFiles = 'page' === module.kind ? node_fs.readdirSync(oldFolder).map((name)=>node_path.join(oldFolder, name)).filter((file)=>node_fs.statSync(file).isFile() && ownsPath(file)) : react_module_move_walk(oldFolder);
|
|
1394
|
-
const moves = new Map(moduleFiles.map((file)=>[
|
|
1395
|
-
file,
|
|
1396
|
-
movedPath(file) ?? file
|
|
1397
|
-
]));
|
|
1398
|
-
const aliases = readAliases(projectRoot);
|
|
1399
|
-
const resolve = (specifier, fromFile)=>{
|
|
1400
|
-
if (specifier.startsWith('.')) return {
|
|
1401
|
-
resolved: node_path.resolve(node_path.dirname(fromFile), specifier),
|
|
1402
|
-
alias: void 0
|
|
1403
|
-
};
|
|
1404
|
-
const alias = aliases.find(({ prefix })=>specifier.startsWith(prefix));
|
|
1405
|
-
return alias ? {
|
|
1406
|
-
resolved: node_path.resolve(alias.target, specifier.slice(alias.prefix.length)),
|
|
1407
|
-
alias
|
|
1408
|
-
} : void 0;
|
|
1409
|
-
};
|
|
1410
|
-
const specifierFor = (fromFile, toPath, alias)=>{
|
|
1411
|
-
const aliased = alias && toPosix(node_path.relative(alias.target, toPath));
|
|
1412
|
-
if (aliased && !aliased.startsWith('..')) return `${alias.prefix}${aliased}`;
|
|
1413
|
-
const relative = toPosix(node_path.relative(node_path.dirname(fromFile), toPath));
|
|
1414
|
-
return relative.startsWith('.') ? relative : `./${relative}`;
|
|
1415
|
-
};
|
|
1416
|
-
const suffixes = 'page' === module.kind ? 'Page' : 'Widget|UIModel';
|
|
1417
|
-
const classPattern = new RegExp(`(?<![\\w$])(use)?${escapeRegExp(module.ClassName)}(?=${suffixes})`, 'g');
|
|
1418
|
-
const oldTitle = `Widgets/${module.name.split('/').map((segment)=>new NestedName(segment).ClassName).join('/')}`;
|
|
1419
|
-
const titlePattern = new RegExp(`(['"\`])${escapeRegExp(oldTitle)}\\1`, 'g');
|
|
1420
|
-
const newTitle = [
|
|
1421
|
-
'Widgets',
|
|
1422
|
-
...newName.classNameSegments
|
|
1423
|
-
].join('/');
|
|
1424
|
-
const codeFiles = new Map(react_module_move_walk(projectRoot).filter((file)=>codeExtension.test(file)).map((file)=>[
|
|
1425
|
-
file,
|
|
1426
|
-
node_fs.readFileSync(file, 'utf8')
|
|
1427
|
-
]));
|
|
1428
|
-
const barrels = new Set();
|
|
1429
|
-
const isBarrel = (resolved)=>barrels.has(resolved.replace(codeExtension, ''));
|
|
1430
|
-
for(let found = true; found;){
|
|
1431
|
-
found = false;
|
|
1432
|
-
for (const [file, content] of codeFiles){
|
|
1433
|
-
if (moves.has(file) || isBarrel(file)) continue;
|
|
1434
|
-
const reExportsModule = [
|
|
1435
|
-
...content.matchAll(reExportPattern)
|
|
1436
|
-
].some(([, , specifier])=>{
|
|
1437
|
-
const resolution = resolve(specifier, file);
|
|
1438
|
-
return !!resolution && (!!movedPath(resolution.resolved) || isBarrel(resolution.resolved));
|
|
1439
|
-
});
|
|
1440
|
-
if (reExportsModule) {
|
|
1441
|
-
for (const importPath of importPaths(file))barrels.add(importPath);
|
|
1442
|
-
found = true;
|
|
1443
|
-
}
|
|
1444
|
-
}
|
|
1445
|
-
}
|
|
1446
|
-
const rewrite = (content, oldFile, newFile)=>{
|
|
1447
|
-
let referencesModule = false;
|
|
1448
|
-
const rewritten = content.replace(specifierPattern, (match, keyword, quote, specifier)=>{
|
|
1449
|
-
const resolution = resolve(specifier, oldFile);
|
|
1450
|
-
if (!resolution) return match;
|
|
1451
|
-
const moved = movedPath(resolution.resolved);
|
|
1452
|
-
if (moved || isBarrel(resolution.resolved)) referencesModule = true;
|
|
1453
|
-
if (!moved && oldFile === newFile) return match;
|
|
1454
|
-
const next = specifierFor(newFile, moved ?? resolution.resolved, resolution.alias);
|
|
1455
|
-
return `${keyword}${quote}${next}${quote}`;
|
|
1456
|
-
});
|
|
1457
|
-
if (!referencesModule && !moves.has(oldFile)) return content;
|
|
1458
|
-
const renamed = rewritten.replace(classPattern, (_, use = '')=>`${use}${newClassName}`);
|
|
1459
|
-
return moves.has(oldFile) && 'widget' === module.kind ? renamed.replace(titlePattern, (_, quote)=>`${quote}${newTitle}${quote}`) : renamed;
|
|
1460
|
-
};
|
|
1461
|
-
for (const [file, newFile] of moves)if (!codeFiles.has(file) && newFile !== file) new File(file).moveTo(newFile, reactFolder);
|
|
1462
|
-
const updated = [];
|
|
1463
|
-
for (const [file, content] of codeFiles){
|
|
1464
|
-
const newFile = moves.get(file);
|
|
1465
|
-
const next = rewrite(content, file, newFile ?? file);
|
|
1466
|
-
if (newFile && newFile !== file) {
|
|
1467
|
-
new File(newFile).data = next;
|
|
1468
|
-
new File(file).delete(reactFolder);
|
|
1469
|
-
} else if (next !== content) {
|
|
1470
|
-
new File(file).update(next);
|
|
1471
|
-
if (!newFile) updated.push(file);
|
|
1472
|
-
}
|
|
1473
|
-
}
|
|
1474
|
-
return {
|
|
1475
|
-
moves: [
|
|
1476
|
-
...moves
|
|
1477
|
-
].filter(([from, to])=>from !== to),
|
|
1478
|
-
updated
|
|
1479
|
-
};
|
|
1480
|
-
};
|
|
1481
|
-
const moveReactModuleUseCase = async (presentationReactFolder, kind)=>{
|
|
1482
|
-
const reactFolder = presentationReactFolder.path;
|
|
1483
|
-
const modules = findReactModules(reactFolder, kind);
|
|
1484
|
-
if (!modules.length) return void console.log(`No ${kind}s generated by df-ps were found.`);
|
|
1485
|
-
const module = await prompts_select({
|
|
1486
|
-
message: 'page' === kind ? 'Page:' : 'Widget:',
|
|
1487
|
-
choices: modules.map((value)=>({
|
|
1488
|
-
name: value.name,
|
|
1489
|
-
value,
|
|
1490
|
-
description: node_path.relative(reactFolder, value.path)
|
|
1491
|
-
}))
|
|
1492
|
-
});
|
|
1493
|
-
const name = await input({
|
|
1494
|
-
message: 'New name (folders split by "/"):',
|
|
1495
|
-
default: module.name,
|
|
1496
|
-
validate: (value)=>validateMove(reactFolder, module, value)
|
|
1497
|
-
});
|
|
1498
|
-
const root = new ProjectRoot().path;
|
|
1499
|
-
const { moves, updated } = moveReactModule(root, reactFolder, module, name);
|
|
1500
|
-
if (!moves.length && !updated.length) return void console.log('Nothing to change.');
|
|
1501
|
-
const relative = (file)=>node_path.relative(root, file);
|
|
1502
|
-
console.log([
|
|
1503
|
-
'Files to move:',
|
|
1504
|
-
...moves.map(([from, to])=>` ${relative(from)} → ${relative(to)}`),
|
|
1505
|
-
...updated.length ? [
|
|
1506
|
-
'Files with updated imports:',
|
|
1507
|
-
...updated.map((file)=>` ${relative(file)}`)
|
|
1508
|
-
] : []
|
|
1509
|
-
].join('\n'));
|
|
1510
|
-
if (!await prompts_confirm({
|
|
1511
|
-
message: 'Apply?',
|
|
1512
|
-
default: true
|
|
1513
|
-
})) {
|
|
1514
|
-
discardPendingChanges();
|
|
1515
|
-
console.log('Cancelled: nothing was written.');
|
|
1516
|
-
}
|
|
1517
|
-
};
|
|
1518
|
-
const wiring_layers = [
|
|
1519
|
-
'domain',
|
|
1520
|
-
'application',
|
|
1521
|
-
'infrastructure',
|
|
1522
|
-
'presentation'
|
|
1523
|
-
];
|
|
1524
|
-
const visibleLayers = {
|
|
1525
|
-
domain: [
|
|
1526
|
-
'domain'
|
|
1527
|
-
],
|
|
1528
|
-
application: [
|
|
1529
|
-
'domain',
|
|
1530
|
-
'application'
|
|
1531
|
-
],
|
|
1532
|
-
infrastructure: [
|
|
1533
|
-
'domain',
|
|
1534
|
-
'infrastructure'
|
|
1535
|
-
],
|
|
1536
|
-
presentation: [
|
|
1537
|
-
'application',
|
|
1538
|
-
'presentation'
|
|
1539
|
-
]
|
|
1540
|
-
};
|
|
1541
|
-
const branchedTypeFiles = {
|
|
1542
|
-
repositories: (fileBase)=>[
|
|
1543
|
-
'domain',
|
|
1544
|
-
'aggregates',
|
|
1545
|
-
fileBase.replace(/-repository$/, ''),
|
|
1546
|
-
`${fileBase}.ts`
|
|
1547
|
-
],
|
|
1548
|
-
commands: (fileBase)=>[
|
|
1549
|
-
'application',
|
|
1550
|
-
'commands',
|
|
1551
|
-
'execution',
|
|
1552
|
-
`${fileBase}.ts`
|
|
1553
|
-
],
|
|
1554
|
-
queries: (fileBase)=>[
|
|
1555
|
-
'application',
|
|
1556
|
-
'queries',
|
|
1557
|
-
'execution',
|
|
1558
|
-
`${fileBase}.ts`
|
|
1559
|
-
],
|
|
1560
|
-
'application-ports': (fileBase)=>[
|
|
1561
|
-
'application',
|
|
1562
|
-
'ports',
|
|
1563
|
-
`${fileBase}.ts`
|
|
1564
|
-
]
|
|
1565
|
-
};
|
|
1566
|
-
const layerOf = (file, contextPath)=>{
|
|
1567
|
-
const [firstSegment] = node_path.relative(contextPath, file).split(node_path.sep);
|
|
1568
|
-
return wiring_layers.find((layer)=>layer === firstSegment);
|
|
1569
|
-
};
|
|
1570
|
-
const namedImports = (source, fromFile)=>{
|
|
1571
|
-
const result = new Map();
|
|
1572
|
-
for (const [, names, importPath] of source.matchAll(/import\s*(?:type\s*)?\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g))for (const name of names.split(',')){
|
|
1573
|
-
const cleanName = name.replace(/^\s*type\s+/, '').trim();
|
|
1574
|
-
if (cleanName) result.set(cleanName, `${node_path.resolve(node_path.dirname(fromFile), importPath)}.ts`);
|
|
1575
|
-
}
|
|
1576
|
-
return result;
|
|
1243
|
+
return result;
|
|
1577
1244
|
};
|
|
1578
1245
|
const isEndpointWire = (wiringRelativePath)=>wiringRelativePath.startsWith([
|
|
1579
1246
|
'presentation',
|
|
@@ -1661,7 +1328,6 @@ const kindRules = {
|
|
|
1661
1328
|
};
|
|
1662
1329
|
const wiring_implementationType = (consumer, implementation)=>consumer.branched ? node_path.basename(node_path.dirname(implementation.file)) : void 0;
|
|
1663
1330
|
const isSuggestedFor = (candidate, implementation)=>'infrastructure' === candidate.layer && `-${node_path.basename(candidate.typeFile, '.ts')}-`.includes(`-${implementation}-`);
|
|
1664
|
-
const isSuggestedEndpointGenerator = (candidate)=>candidate.typeName.toLowerCase().includes('endpoint');
|
|
1665
1331
|
const canDependOn = (consumer, candidate)=>visibleLayers[consumer.implementationsLayer].includes(candidate.layer) && (kindRules[consumer.kind]?.(candidate, consumer) ?? true);
|
|
1666
1332
|
const availableDependencies = (consumer, implementation, candidates)=>{
|
|
1667
1333
|
const wired = wiredDependencyNames(new File(consumer.wireFile).data, implementation.className);
|
|
@@ -1876,78 +1542,504 @@ const contextAndSharedModules = (boundedContextFolder)=>{
|
|
|
1876
1542
|
}))
|
|
1877
1543
|
];
|
|
1878
1544
|
};
|
|
1879
|
-
const promptDependencies = async (boundedContextFolder, consumer, implementations = consumer.implementations)=>{
|
|
1880
|
-
const options = contextAndSharedModules(boundedContextFolder);
|
|
1881
|
-
let asked = false;
|
|
1882
|
-
for (const implementation of implementations){
|
|
1883
|
-
const available = availableDependencies(consumer, implementation, options.map((x)=>x.module));
|
|
1884
|
-
const candidates = options.filter((x)=>available.includes(x.module));
|
|
1885
|
-
if (!candidates.length) continue;
|
|
1886
|
-
asked = true;
|
|
1887
|
-
const type = wiring_implementationType(consumer, implementation);
|
|
1888
|
-
const suggested = type ? candidates.filter((x)=>isSuggestedFor(x.module, type)) : [];
|
|
1889
|
-
const dependencies = await prompts_checkbox({
|
|
1890
|
-
message: consumer.branched ? `Dependencies for ${implementation.className}:` : 'Dependencies:',
|
|
1891
|
-
choices: groupedChoices(candidates, suggested),
|
|
1892
|
-
pageSize: groupedPageSize,
|
|
1893
|
-
loop: false
|
|
1545
|
+
const promptDependencies = async (boundedContextFolder, consumer, implementations = consumer.implementations)=>{
|
|
1546
|
+
const options = contextAndSharedModules(boundedContextFolder);
|
|
1547
|
+
let asked = false;
|
|
1548
|
+
for (const implementation of implementations){
|
|
1549
|
+
const available = availableDependencies(consumer, implementation, options.map((x)=>x.module));
|
|
1550
|
+
const candidates = options.filter((x)=>available.includes(x.module));
|
|
1551
|
+
if (!candidates.length) continue;
|
|
1552
|
+
asked = true;
|
|
1553
|
+
const type = wiring_implementationType(consumer, implementation);
|
|
1554
|
+
const suggested = type ? candidates.filter((x)=>isSuggestedFor(x.module, type)) : [];
|
|
1555
|
+
const dependencies = await prompts_checkbox({
|
|
1556
|
+
message: consumer.branched ? `Dependencies for ${implementation.className}:` : 'Dependencies:',
|
|
1557
|
+
choices: groupedChoices(candidates, suggested),
|
|
1558
|
+
pageSize: groupedPageSize,
|
|
1559
|
+
loop: false
|
|
1560
|
+
});
|
|
1561
|
+
if (dependencies.length) wireDependencies(consumer, implementation, dependencies.map((x)=>x.module));
|
|
1562
|
+
}
|
|
1563
|
+
return asked;
|
|
1564
|
+
};
|
|
1565
|
+
const wireNewModuleDependencies = async (boundedContextFolder, wireFile)=>{
|
|
1566
|
+
const consumer = readWiredModule(wireFile.path, boundedContextFolder.path);
|
|
1567
|
+
if (consumer) await promptDependencies(boundedContextFolder, consumer);
|
|
1568
|
+
};
|
|
1569
|
+
const wireDependency = async (boundedContextFolder)=>{
|
|
1570
|
+
const modules = findWiredModules(boundedContextFolder.path);
|
|
1571
|
+
if (!modules.length) return void console.log('No wired modules here yet.');
|
|
1572
|
+
const { module: consumer, implementation } = await prompts_select({
|
|
1573
|
+
message: 'Module:',
|
|
1574
|
+
choices: groupedChoices(modules.flatMap((x)=>x.implementations.map((implementation)=>{
|
|
1575
|
+
const type = wiring_implementationType(x, implementation);
|
|
1576
|
+
return {
|
|
1577
|
+
module: x,
|
|
1578
|
+
label: type ? `${x.typeName} (${type})` : x.typeName,
|
|
1579
|
+
implementation
|
|
1580
|
+
};
|
|
1581
|
+
}))),
|
|
1582
|
+
pageSize: groupedPageSize,
|
|
1583
|
+
loop: false
|
|
1584
|
+
});
|
|
1585
|
+
if (!await promptDependencies(boundedContextFolder, consumer, [
|
|
1586
|
+
implementation
|
|
1587
|
+
])) console.log('No dependencies available for this module.');
|
|
1588
|
+
};
|
|
1589
|
+
const promptEndpointHandler = async (boundedContextFolder)=>{
|
|
1590
|
+
const handlers = findWiredModules(boundedContextFolder.path).filter((x)=>[
|
|
1591
|
+
'queries',
|
|
1592
|
+
'commands',
|
|
1593
|
+
'use-cases'
|
|
1594
|
+
].includes(x.kind)).map((x)=>({
|
|
1595
|
+
module: x,
|
|
1596
|
+
label: x.typeName
|
|
1597
|
+
}));
|
|
1598
|
+
return handlers.length ? (await prompts_select({
|
|
1599
|
+
message: 'Handler:',
|
|
1600
|
+
choices: groupedChoices(handlers),
|
|
1601
|
+
pageSize: groupedPageSize,
|
|
1602
|
+
loop: false
|
|
1603
|
+
})).module : void 0;
|
|
1604
|
+
};
|
|
1605
|
+
const createPresentationService = (boundedContextFolder, name, classFile)=>{
|
|
1606
|
+
const naming = new UnknownFormatNaming(name);
|
|
1607
|
+
const className = `${naming.ClassName}PresentationService`;
|
|
1608
|
+
const scaffoldUnitTests = Boolean(ConfigFile.Instance.data.testingLibrary);
|
|
1609
|
+
const withWiring = ConfigFile.Instance.data.domainFirstPackages.includes('@domain-first/wire');
|
|
1610
|
+
const wiringFolder = boundedContextFolder.subitem([
|
|
1611
|
+
'wiring',
|
|
1612
|
+
'presentation-services'
|
|
1613
|
+
]);
|
|
1614
|
+
const presentationServicesFolder = boundedContextFolder.subitem([
|
|
1615
|
+
'presentation',
|
|
1616
|
+
'services'
|
|
1617
|
+
]);
|
|
1618
|
+
presentationServicesFolder.createFile(`${naming.fileName}-presentation-service.ts`, classFile?.(className) ?? `
|
|
1619
|
+
export class ${className} {
|
|
1620
|
+
|
|
1621
|
+
}
|
|
1622
|
+
`.trim());
|
|
1623
|
+
if (scaffoldUnitTests) presentationServicesFolder.createFile(`${naming.fileName}-presentation-service.spec.ts`, `
|
|
1624
|
+
import { describe, test, expect, beforeEach } from '${ConfigFile.Instance.data.testingLibrary}'
|
|
1625
|
+
${withWiring ? `import { type ${naming.ClassName}PresentationService } from './${naming.fileName}-presentation-service'
|
|
1626
|
+
import { wire${naming.ClassName}PresentationService } from '../../wiring/presentation-services/wire-${naming.fileName}-presentation-service'` : `import { ${naming.ClassName}PresentationService } from './${naming.fileName}-presentation-service'`}
|
|
1627
|
+
|
|
1628
|
+
let ${naming.variableName}PresentationService: ${naming.ClassName}PresentationService
|
|
1629
|
+
|
|
1630
|
+
beforeEach(() => {
|
|
1631
|
+
${naming.variableName}PresentationService = ${withWiring ? `wire${naming.ClassName}PresentationService()` : `new ${naming.ClassName}PresentationService()`}
|
|
1632
|
+
})
|
|
1633
|
+
|
|
1634
|
+
describe('${naming.withSpaces}', () => {
|
|
1635
|
+
test('can be ${withWiring ? 'wired' : 'created'}', () => {
|
|
1636
|
+
expect(${naming.variableName}PresentationService).toBeDefined()
|
|
1637
|
+
})
|
|
1638
|
+
})
|
|
1639
|
+
`.trim());
|
|
1640
|
+
if (withWiring) return wiringFolder.createFile(`wire-${naming.fileName}-presentation-service.ts`, `
|
|
1641
|
+
import { wireClass } from '@domain-first/wire'
|
|
1642
|
+
import { ${naming.ClassName}PresentationService } from '../../presentation/services/${naming.fileName}-presentation-service'
|
|
1643
|
+
|
|
1644
|
+
export const wire${naming.ClassName}PresentationService = wireClass(
|
|
1645
|
+
${naming.ClassName}PresentationService,
|
|
1646
|
+
[]
|
|
1647
|
+
)
|
|
1648
|
+
`.trim());
|
|
1649
|
+
};
|
|
1650
|
+
const scaffoldNewPresentationService = async (boundedContextFolder)=>{
|
|
1651
|
+
const name = await input({
|
|
1652
|
+
message: 'Name: '
|
|
1653
|
+
});
|
|
1654
|
+
const wireFile = createPresentationService(boundedContextFolder, name);
|
|
1655
|
+
if (wireFile) await wireNewModuleDependencies(boundedContextFolder, wireFile);
|
|
1656
|
+
};
|
|
1657
|
+
const rest_endpoint_generator_name = 'rest-endpoint-generator';
|
|
1658
|
+
const scaffoldRestEndpointGenerator = (sharedFolder)=>createPresentationService(sharedFolder, rest_endpoint_generator_name, (className)=>`
|
|
1659
|
+
import {
|
|
1660
|
+
type Adapter,
|
|
1661
|
+
type EndpointGenerator,
|
|
1662
|
+
type ErrorStatuses,
|
|
1663
|
+
createEndpoint,
|
|
1664
|
+
InputMappingError,
|
|
1665
|
+
RequestParsingError
|
|
1666
|
+
} from '@domain-first/handlers-rest'
|
|
1667
|
+
|
|
1668
|
+
export class ${className} {
|
|
1669
|
+
get withoutGuards(): EndpointGenerator {
|
|
1670
|
+
return createEndpoint({
|
|
1671
|
+
errorStatuses: {
|
|
1672
|
+
400: {
|
|
1673
|
+
checks: this.invalidRequestChecks,
|
|
1674
|
+
description: 'Invalid request'
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
})(this.adapter) as EndpointGenerator
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
/**
|
|
1681
|
+
* Default checks of an invalid request, to spread into a generator's
|
|
1682
|
+
* own checks.
|
|
1683
|
+
*/
|
|
1684
|
+
private get invalidRequestChecks(): ErrorStatuses[number]['checks'] {
|
|
1685
|
+
return [RequestParsingError.is, InputMappingError.is]
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
/**
|
|
1689
|
+
* The adapter of your HTTP framework.
|
|
1690
|
+
*/
|
|
1691
|
+
private get adapter(): Adapter<any[], any> {
|
|
1692
|
+
throw new Error('REST adapter is not set')
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
`.trim());
|
|
1696
|
+
const findRestEndpointGenerator = (sharedFolder)=>{
|
|
1697
|
+
const typeFile = node_path.join(sharedFolder.path, 'presentation', 'services', `${rest_endpoint_generator_name}-presentation-service.ts`);
|
|
1698
|
+
if (!new File(typeFile).exists) return;
|
|
1699
|
+
return {
|
|
1700
|
+
typeName: 'RestEndpointGeneratorPresentationService',
|
|
1701
|
+
typeFile,
|
|
1702
|
+
wireName: 'wireRestEndpointGeneratorPresentationService',
|
|
1703
|
+
wireFile: node_path.join(sharedFolder.path, 'wiring', 'presentation-services', `wire-${rest_endpoint_generator_name}-presentation-service.ts`)
|
|
1704
|
+
};
|
|
1705
|
+
};
|
|
1706
|
+
const initializeUseCase = async ()=>{
|
|
1707
|
+
const rootFolderName = await input({
|
|
1708
|
+
message: 'Root folder (use "/" for inner folders):',
|
|
1709
|
+
default: 'src'
|
|
1710
|
+
});
|
|
1711
|
+
const domainFirstPackages = await prompts_checkbox({
|
|
1712
|
+
message: '@domain-first packages',
|
|
1713
|
+
choices: [
|
|
1714
|
+
'@domain-first/types',
|
|
1715
|
+
'@domain-first/errors',
|
|
1716
|
+
'@domain-first/handlers',
|
|
1717
|
+
'@domain-first/wire',
|
|
1718
|
+
'@domain-first/handlers-rest'
|
|
1719
|
+
]
|
|
1720
|
+
});
|
|
1721
|
+
const defaultPersistenceLayerImplementation = await input({
|
|
1722
|
+
message: 'Default persistence layer implementation:'
|
|
1723
|
+
});
|
|
1724
|
+
const useReact = await prompts_confirm({
|
|
1725
|
+
message: 'Use React'
|
|
1726
|
+
});
|
|
1727
|
+
let storybookFramework;
|
|
1728
|
+
const withStorybook = useReact ? await prompts_confirm({
|
|
1729
|
+
message: 'Use Storybook'
|
|
1730
|
+
}) : false;
|
|
1731
|
+
if (withStorybook) storybookFramework = await input({
|
|
1732
|
+
message: 'Storybook framework package:',
|
|
1733
|
+
default: '@storybook/react-vite'
|
|
1734
|
+
});
|
|
1735
|
+
let testingLibrary;
|
|
1736
|
+
const withTests = await prompts_confirm({
|
|
1737
|
+
message: 'Scaffold unit tests'
|
|
1738
|
+
});
|
|
1739
|
+
if (withTests) testingLibrary = await input({
|
|
1740
|
+
message: 'Unit testing library:',
|
|
1741
|
+
default: 'vitest'
|
|
1742
|
+
});
|
|
1743
|
+
ConfigFile.write({
|
|
1744
|
+
rootFolder: rootFolderName,
|
|
1745
|
+
domainFirstPackages,
|
|
1746
|
+
defaultPersistenceLayerImplementation,
|
|
1747
|
+
useReact,
|
|
1748
|
+
storybookFramework,
|
|
1749
|
+
testingLibrary
|
|
1750
|
+
});
|
|
1751
|
+
const { path } = new ProjectRoot();
|
|
1752
|
+
const rootFolder = new Folder(path);
|
|
1753
|
+
if (domainFirstPackages.includes('@domain-first/wire')) rootFolder.subitem([
|
|
1754
|
+
rootFolderName,
|
|
1755
|
+
'shared',
|
|
1756
|
+
'wiring'
|
|
1757
|
+
]).createFile('env-branched-wire.ts', `
|
|
1758
|
+
import { branchedWire } from '@domain-first/wire'
|
|
1759
|
+
|
|
1760
|
+
export const envBranchedWire = branchedWire(() => {
|
|
1761
|
+
return process.env.NODE_ENV ?? 'development' as 'test' | 'development' | 'production'
|
|
1762
|
+
});
|
|
1763
|
+
`.trim());
|
|
1764
|
+
if (domainFirstPackages.includes('@domain-first/errors')) rootFolder.subitem([
|
|
1765
|
+
rootFolderName,
|
|
1766
|
+
'shared'
|
|
1767
|
+
]).subitem([
|
|
1768
|
+
'domain',
|
|
1769
|
+
'errors'
|
|
1770
|
+
]).createFile('index.ts', `
|
|
1771
|
+
import { errorNamespace } from '@domain-first/errors'
|
|
1772
|
+
|
|
1773
|
+
export const SharedErrors = errorNamespace('shared')
|
|
1774
|
+
`.trim());
|
|
1775
|
+
if (domainFirstPackages.includes('@domain-first/handlers-rest')) scaffoldRestEndpointGenerator(rootFolder.subitem([
|
|
1776
|
+
rootFolderName,
|
|
1777
|
+
'shared'
|
|
1778
|
+
]));
|
|
1779
|
+
};
|
|
1780
|
+
const splitSegments = (rawSource)=>rawSource.split(/[\\/]/).map((segment)=>segment.trim()).filter((segment)=>!!segment);
|
|
1781
|
+
class NestedName {
|
|
1782
|
+
folders;
|
|
1783
|
+
folderName;
|
|
1784
|
+
fileName;
|
|
1785
|
+
ClassName;
|
|
1786
|
+
classNameSegments;
|
|
1787
|
+
constructor(rawSource){
|
|
1788
|
+
const segments = splitSegments(rawSource);
|
|
1789
|
+
this.folders = segments.slice(0, -1);
|
|
1790
|
+
this.folderName = new UnknownFormatNaming(segments.at(-1) ?? '').fileName;
|
|
1791
|
+
this.fileName = segments.map((segment)=>new UnknownFormatNaming(segment).fileName).join('-');
|
|
1792
|
+
this.classNameSegments = segments.map((segment)=>new UnknownFormatNaming(segment).ClassName);
|
|
1793
|
+
this.ClassName = this.classNameSegments.join('');
|
|
1794
|
+
}
|
|
1795
|
+
static validate = (rawSource)=>{
|
|
1796
|
+
const segments = splitSegments(rawSource);
|
|
1797
|
+
if (!segments.length) return 'Name is required';
|
|
1798
|
+
if (segments.some((segment)=>'.' === segment || '..' === segment)) return 'Use a path relative to the folder, without "." or ".."';
|
|
1799
|
+
return true;
|
|
1800
|
+
};
|
|
1801
|
+
}
|
|
1802
|
+
const ignoredFolders = new Set([
|
|
1803
|
+
'node_modules',
|
|
1804
|
+
'.git',
|
|
1805
|
+
'.next',
|
|
1806
|
+
'dist',
|
|
1807
|
+
'build',
|
|
1808
|
+
'out',
|
|
1809
|
+
'coverage',
|
|
1810
|
+
'storybook-static'
|
|
1811
|
+
]);
|
|
1812
|
+
const codeExtension = /\.(?:tsx?|mts|cts|jsx?|mjs|cjs|mdx)$/;
|
|
1813
|
+
const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
1814
|
+
const react_module_move_walk = (folder)=>{
|
|
1815
|
+
if (!node_fs.existsSync(folder)) return [];
|
|
1816
|
+
return node_fs.readdirSync(folder, {
|
|
1817
|
+
withFileTypes: true
|
|
1818
|
+
}).flatMap((entry)=>{
|
|
1819
|
+
const entryPath = node_path.join(folder, entry.name);
|
|
1820
|
+
if (entry.isDirectory()) return ignoredFolders.has(entry.name) ? [] : react_module_move_walk(entryPath);
|
|
1821
|
+
return entry.isFile() ? [
|
|
1822
|
+
entryPath
|
|
1823
|
+
] : [];
|
|
1824
|
+
});
|
|
1825
|
+
};
|
|
1826
|
+
const toPosix = (value)=>value.split(node_path.sep).join('/');
|
|
1827
|
+
const nestedName = (folders, fileBase)=>{
|
|
1828
|
+
const prefix = `${folders.join('-')}-`;
|
|
1829
|
+
const last = folders.length && fileBase.startsWith(prefix) ? fileBase.slice(prefix.length) : fileBase;
|
|
1830
|
+
return [
|
|
1831
|
+
...folders,
|
|
1832
|
+
last
|
|
1833
|
+
].join('/');
|
|
1834
|
+
};
|
|
1835
|
+
const declaredClassName = (filePath, suffix, name)=>{
|
|
1836
|
+
const content = node_fs.readFileSync(filePath, 'utf8');
|
|
1837
|
+
const declared = content.match(new RegExp(`export\\s+(?:const|function)\\s+(\\w+)${suffix}\\b`))?.[1];
|
|
1838
|
+
return declared ?? new NestedName(name).ClassName;
|
|
1839
|
+
};
|
|
1840
|
+
const findReactModules = (reactFolder, kind)=>{
|
|
1841
|
+
const modules = 'page' === kind ? react_module_move_walk(node_path.join(reactFolder, 'pages')).filter((file)=>file.endsWith('-page.tsx')).map((file)=>{
|
|
1842
|
+
const folders = node_path.relative(node_path.join(reactFolder, 'pages'), node_path.dirname(file)).split(node_path.sep).filter((segment)=>!!segment);
|
|
1843
|
+
const fileBase = node_path.basename(file, '-page.tsx');
|
|
1844
|
+
const name = nestedName(folders, fileBase);
|
|
1845
|
+
return {
|
|
1846
|
+
kind,
|
|
1847
|
+
name,
|
|
1848
|
+
path: file,
|
|
1849
|
+
fileBase,
|
|
1850
|
+
ClassName: declaredClassName(file, 'Page', name)
|
|
1851
|
+
};
|
|
1852
|
+
}) : react_module_move_walk(node_path.join(reactFolder, 'widgets')).filter((file)=>file.endsWith('-widget.tsx')).map((file)=>{
|
|
1853
|
+
const folder = node_path.dirname(file);
|
|
1854
|
+
const name = toPosix(node_path.relative(node_path.join(reactFolder, 'widgets'), folder));
|
|
1855
|
+
return {
|
|
1856
|
+
kind,
|
|
1857
|
+
name,
|
|
1858
|
+
path: folder,
|
|
1859
|
+
fileBase: node_path.basename(file, '-widget.tsx'),
|
|
1860
|
+
ClassName: declaredClassName(file, 'Widget', name)
|
|
1861
|
+
};
|
|
1862
|
+
});
|
|
1863
|
+
return modules.sort((a, b)=>a.name.localeCompare(b.name));
|
|
1864
|
+
};
|
|
1865
|
+
const react_module_move_targetPath = (reactFolder, module, rawName)=>{
|
|
1866
|
+
const { folders, folderName, fileName } = new NestedName(rawName);
|
|
1867
|
+
return 'page' === module.kind ? node_path.join(reactFolder, 'pages', ...folders, `${fileName}-page.tsx`) : node_path.join(reactFolder, 'widgets', ...folders, folderName);
|
|
1868
|
+
};
|
|
1869
|
+
const validateMove = (reactFolder, module, rawName)=>{
|
|
1870
|
+
const nameError = NestedName.validate(rawName);
|
|
1871
|
+
if (true !== nameError) return nameError;
|
|
1872
|
+
const target = react_module_move_targetPath(reactFolder, module, rawName);
|
|
1873
|
+
if (target !== module.path && node_fs.existsSync(target)) return `${node_path.relative(reactFolder, target)} already exists`;
|
|
1874
|
+
if (target.startsWith(`${module.path}${node_path.sep}`)) return 'A widget cannot be moved into itself';
|
|
1875
|
+
return true;
|
|
1876
|
+
};
|
|
1877
|
+
const stripJsonComments = (source)=>source.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, text)=>text ? text : '').replace(/,(\s*[}\]])/g, '$1');
|
|
1878
|
+
const readAliases = (projectRoot)=>{
|
|
1879
|
+
const tsconfig = node_path.join(projectRoot, 'tsconfig.json');
|
|
1880
|
+
if (!node_fs.existsSync(tsconfig)) return [];
|
|
1881
|
+
try {
|
|
1882
|
+
const { compilerOptions = {} } = JSON.parse(stripJsonComments(node_fs.readFileSync(tsconfig, 'utf8')));
|
|
1883
|
+
const base = node_path.resolve(projectRoot, compilerOptions.baseUrl ?? '.');
|
|
1884
|
+
return Object.entries(compilerOptions.paths ?? {}).filter(([pattern, targets])=>pattern.endsWith('*') && targets[0]?.endsWith('*')).map(([pattern, [target]])=>({
|
|
1885
|
+
prefix: pattern.slice(0, -1),
|
|
1886
|
+
target: node_path.resolve(base, target.slice(0, -1))
|
|
1887
|
+
}));
|
|
1888
|
+
} catch {
|
|
1889
|
+
return [];
|
|
1890
|
+
}
|
|
1891
|
+
};
|
|
1892
|
+
const specifierPattern = /(\bfrom\s*|\bimport\s*\(\s*|\bimport\s+|\brequire\s*\(\s*|\bmock\s*\(\s*)(['"])([^'"\n]+)\2/g;
|
|
1893
|
+
const reExportPattern = /\bexport\s+(?:type\s+)?(?:\*(?:\s+as\s+[\w$]+)?|\{[^}]*\})\s*from\s*(['"])([^'"\n]+)\1/g;
|
|
1894
|
+
const importPaths = (file)=>{
|
|
1895
|
+
const withoutExtension = file.replace(codeExtension, '');
|
|
1896
|
+
return 'index' === node_path.basename(withoutExtension) ? [
|
|
1897
|
+
withoutExtension,
|
|
1898
|
+
node_path.dirname(withoutExtension)
|
|
1899
|
+
] : [
|
|
1900
|
+
withoutExtension
|
|
1901
|
+
];
|
|
1902
|
+
};
|
|
1903
|
+
const moveReactModule = (projectRoot, reactFolder, module, rawName)=>{
|
|
1904
|
+
const newName = new NestedName(rawName);
|
|
1905
|
+
const target = react_module_move_targetPath(reactFolder, module, rawName);
|
|
1906
|
+
const newClassName = newName.ClassName;
|
|
1907
|
+
const renameFile = (name)=>name.replace(new RegExp(`^(use-)?${escapeRegExp(module.fileBase)}(?=[-.])`), (_, use = '')=>`${use}${newName.fileName}`);
|
|
1908
|
+
const oldFolder = 'page' === module.kind ? node_path.dirname(module.path) : module.path;
|
|
1909
|
+
const newFolder = 'page' === module.kind ? node_path.dirname(target) : target;
|
|
1910
|
+
const ownsPath = (candidate)=>'page' === module.kind ? node_path.dirname(candidate) === oldFolder && new RegExp(`^${escapeRegExp(module.fileBase)}-page(?:\\.|$)`).test(node_path.basename(candidate)) : candidate === oldFolder || candidate.startsWith(`${oldFolder}${node_path.sep}`);
|
|
1911
|
+
const movedPath = (candidate)=>{
|
|
1912
|
+
if (!ownsPath(candidate)) return;
|
|
1913
|
+
const segments = node_path.relative(oldFolder, candidate).split(node_path.sep);
|
|
1914
|
+
const last = segments.length - 1;
|
|
1915
|
+
segments[last] = renameFile(segments[last]);
|
|
1916
|
+
return node_path.join(newFolder, ...segments);
|
|
1917
|
+
};
|
|
1918
|
+
const moduleFiles = 'page' === module.kind ? node_fs.readdirSync(oldFolder).map((name)=>node_path.join(oldFolder, name)).filter((file)=>node_fs.statSync(file).isFile() && ownsPath(file)) : react_module_move_walk(oldFolder);
|
|
1919
|
+
const moves = new Map(moduleFiles.map((file)=>[
|
|
1920
|
+
file,
|
|
1921
|
+
movedPath(file) ?? file
|
|
1922
|
+
]));
|
|
1923
|
+
const aliases = readAliases(projectRoot);
|
|
1924
|
+
const resolve = (specifier, fromFile)=>{
|
|
1925
|
+
if (specifier.startsWith('.')) return {
|
|
1926
|
+
resolved: node_path.resolve(node_path.dirname(fromFile), specifier),
|
|
1927
|
+
alias: void 0
|
|
1928
|
+
};
|
|
1929
|
+
const alias = aliases.find(({ prefix })=>specifier.startsWith(prefix));
|
|
1930
|
+
return alias ? {
|
|
1931
|
+
resolved: node_path.resolve(alias.target, specifier.slice(alias.prefix.length)),
|
|
1932
|
+
alias
|
|
1933
|
+
} : void 0;
|
|
1934
|
+
};
|
|
1935
|
+
const specifierFor = (fromFile, toPath, alias)=>{
|
|
1936
|
+
const aliased = alias && toPosix(node_path.relative(alias.target, toPath));
|
|
1937
|
+
if (aliased && !aliased.startsWith('..')) return `${alias.prefix}${aliased}`;
|
|
1938
|
+
const relative = toPosix(node_path.relative(node_path.dirname(fromFile), toPath));
|
|
1939
|
+
return relative.startsWith('.') ? relative : `./${relative}`;
|
|
1940
|
+
};
|
|
1941
|
+
const suffixes = 'page' === module.kind ? 'Page' : 'Widget|UIModel';
|
|
1942
|
+
const classPattern = new RegExp(`(?<![\\w$])(use)?${escapeRegExp(module.ClassName)}(?=${suffixes})`, 'g');
|
|
1943
|
+
const oldTitle = `Widgets/${module.name.split('/').map((segment)=>new NestedName(segment).ClassName).join('/')}`;
|
|
1944
|
+
const titlePattern = new RegExp(`(['"\`])${escapeRegExp(oldTitle)}\\1`, 'g');
|
|
1945
|
+
const newTitle = [
|
|
1946
|
+
'Widgets',
|
|
1947
|
+
...newName.classNameSegments
|
|
1948
|
+
].join('/');
|
|
1949
|
+
const codeFiles = new Map(react_module_move_walk(projectRoot).filter((file)=>codeExtension.test(file)).map((file)=>[
|
|
1950
|
+
file,
|
|
1951
|
+
node_fs.readFileSync(file, 'utf8')
|
|
1952
|
+
]));
|
|
1953
|
+
const barrels = new Set();
|
|
1954
|
+
const isBarrel = (resolved)=>barrels.has(resolved.replace(codeExtension, ''));
|
|
1955
|
+
for(let found = true; found;){
|
|
1956
|
+
found = false;
|
|
1957
|
+
for (const [file, content] of codeFiles){
|
|
1958
|
+
if (moves.has(file) || isBarrel(file)) continue;
|
|
1959
|
+
const reExportsModule = [
|
|
1960
|
+
...content.matchAll(reExportPattern)
|
|
1961
|
+
].some(([, , specifier])=>{
|
|
1962
|
+
const resolution = resolve(specifier, file);
|
|
1963
|
+
return !!resolution && (!!movedPath(resolution.resolved) || isBarrel(resolution.resolved));
|
|
1964
|
+
});
|
|
1965
|
+
if (reExportsModule) {
|
|
1966
|
+
for (const importPath of importPaths(file))barrels.add(importPath);
|
|
1967
|
+
found = true;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
const rewrite = (content, oldFile, newFile)=>{
|
|
1972
|
+
let referencesModule = false;
|
|
1973
|
+
const rewritten = content.replace(specifierPattern, (match, keyword, quote, specifier)=>{
|
|
1974
|
+
const resolution = resolve(specifier, oldFile);
|
|
1975
|
+
if (!resolution) return match;
|
|
1976
|
+
const moved = movedPath(resolution.resolved);
|
|
1977
|
+
if (moved || isBarrel(resolution.resolved)) referencesModule = true;
|
|
1978
|
+
if (!moved && oldFile === newFile) return match;
|
|
1979
|
+
const next = specifierFor(newFile, moved ?? resolution.resolved, resolution.alias);
|
|
1980
|
+
return `${keyword}${quote}${next}${quote}`;
|
|
1894
1981
|
});
|
|
1895
|
-
if (
|
|
1982
|
+
if (!referencesModule && !moves.has(oldFile)) return content;
|
|
1983
|
+
const renamed = rewritten.replace(classPattern, (_, use = '')=>`${use}${newClassName}`);
|
|
1984
|
+
return moves.has(oldFile) && 'widget' === module.kind ? renamed.replace(titlePattern, (_, quote)=>`${quote}${newTitle}${quote}`) : renamed;
|
|
1985
|
+
};
|
|
1986
|
+
for (const [file, newFile] of moves)if (!codeFiles.has(file) && newFile !== file) new File(file).moveTo(newFile, reactFolder);
|
|
1987
|
+
const updated = [];
|
|
1988
|
+
for (const [file, content] of codeFiles){
|
|
1989
|
+
const newFile = moves.get(file);
|
|
1990
|
+
const next = rewrite(content, file, newFile ?? file);
|
|
1991
|
+
if (newFile && newFile !== file) {
|
|
1992
|
+
new File(newFile).data = next;
|
|
1993
|
+
new File(file).delete(reactFolder);
|
|
1994
|
+
} else if (next !== content) {
|
|
1995
|
+
new File(file).update(next);
|
|
1996
|
+
if (!newFile) updated.push(file);
|
|
1997
|
+
}
|
|
1896
1998
|
}
|
|
1897
|
-
return asked;
|
|
1898
|
-
};
|
|
1899
|
-
const wireNewModuleDependencies = async (boundedContextFolder, wireFile)=>{
|
|
1900
|
-
const consumer = readWiredModule(wireFile.path, boundedContextFolder.path);
|
|
1901
|
-
if (consumer) await promptDependencies(boundedContextFolder, consumer);
|
|
1902
|
-
};
|
|
1903
|
-
const wireDependency = async (boundedContextFolder)=>{
|
|
1904
|
-
const modules = findWiredModules(boundedContextFolder.path);
|
|
1905
|
-
if (!modules.length) return void console.log('No wired modules here yet.');
|
|
1906
|
-
const { module: consumer, implementation } = await prompts_select({
|
|
1907
|
-
message: 'Module:',
|
|
1908
|
-
choices: groupedChoices(modules.flatMap((x)=>x.implementations.map((implementation)=>{
|
|
1909
|
-
const type = wiring_implementationType(x, implementation);
|
|
1910
|
-
return {
|
|
1911
|
-
module: x,
|
|
1912
|
-
label: type ? `${x.typeName} (${type})` : x.typeName,
|
|
1913
|
-
implementation
|
|
1914
|
-
};
|
|
1915
|
-
}))),
|
|
1916
|
-
pageSize: groupedPageSize,
|
|
1917
|
-
loop: false
|
|
1918
|
-
});
|
|
1919
|
-
if (!await promptDependencies(boundedContextFolder, consumer, [
|
|
1920
|
-
implementation
|
|
1921
|
-
])) console.log('No dependencies available for this module.');
|
|
1922
|
-
};
|
|
1923
|
-
const promptEndpointDependencies = async (boundedContextFolder)=>{
|
|
1924
|
-
const options = contextAndSharedModules(boundedContextFolder);
|
|
1925
|
-
const generators = options.filter((x)=>'presentation-services' === x.module.kind);
|
|
1926
|
-
const handlers = findWiredModules(boundedContextFolder.path).filter((x)=>[
|
|
1927
|
-
'queries',
|
|
1928
|
-
'commands',
|
|
1929
|
-
'use-cases'
|
|
1930
|
-
].includes(x.kind)).map((x)=>({
|
|
1931
|
-
module: x,
|
|
1932
|
-
label: x.typeName
|
|
1933
|
-
}));
|
|
1934
|
-
const endpointGenerator = generators.length ? (await prompts_select({
|
|
1935
|
-
message: 'Endpoint generator:',
|
|
1936
|
-
choices: groupedChoices(generators, generators.filter((x)=>isSuggestedEndpointGenerator(x.module))),
|
|
1937
|
-
pageSize: groupedPageSize,
|
|
1938
|
-
loop: false
|
|
1939
|
-
})).module : void 0;
|
|
1940
|
-
const handler = handlers.length ? (await prompts_select({
|
|
1941
|
-
message: 'Handler:',
|
|
1942
|
-
choices: groupedChoices(handlers),
|
|
1943
|
-
pageSize: groupedPageSize,
|
|
1944
|
-
loop: false
|
|
1945
|
-
})).module : void 0;
|
|
1946
1999
|
return {
|
|
1947
|
-
|
|
1948
|
-
|
|
2000
|
+
moves: [
|
|
2001
|
+
...moves
|
|
2002
|
+
].filter(([from, to])=>from !== to),
|
|
2003
|
+
updated
|
|
1949
2004
|
};
|
|
1950
2005
|
};
|
|
2006
|
+
const moveReactModuleUseCase = async (presentationReactFolder, kind)=>{
|
|
2007
|
+
const reactFolder = presentationReactFolder.path;
|
|
2008
|
+
const modules = findReactModules(reactFolder, kind);
|
|
2009
|
+
if (!modules.length) return void console.log(`No ${kind}s generated by df-ps were found.`);
|
|
2010
|
+
const module = await prompts_select({
|
|
2011
|
+
message: 'page' === kind ? 'Page:' : 'Widget:',
|
|
2012
|
+
choices: modules.map((value)=>({
|
|
2013
|
+
name: value.name,
|
|
2014
|
+
value,
|
|
2015
|
+
description: node_path.relative(reactFolder, value.path)
|
|
2016
|
+
}))
|
|
2017
|
+
});
|
|
2018
|
+
const name = await input({
|
|
2019
|
+
message: 'New name (folders split by "/"):',
|
|
2020
|
+
default: module.name,
|
|
2021
|
+
validate: (value)=>validateMove(reactFolder, module, value)
|
|
2022
|
+
});
|
|
2023
|
+
const root = new ProjectRoot().path;
|
|
2024
|
+
const { moves, updated } = moveReactModule(root, reactFolder, module, name);
|
|
2025
|
+
if (!moves.length && !updated.length) return void console.log('Nothing to change.');
|
|
2026
|
+
const relative = (file)=>node_path.relative(root, file);
|
|
2027
|
+
console.log([
|
|
2028
|
+
'Files to move:',
|
|
2029
|
+
...moves.map(([from, to])=>` ${relative(from)} → ${relative(to)}`),
|
|
2030
|
+
...updated.length ? [
|
|
2031
|
+
'Files with updated imports:',
|
|
2032
|
+
...updated.map((file)=>` ${relative(file)}`)
|
|
2033
|
+
] : []
|
|
2034
|
+
].join('\n'));
|
|
2035
|
+
if (!await prompts_confirm({
|
|
2036
|
+
message: 'Apply?',
|
|
2037
|
+
default: true
|
|
2038
|
+
})) {
|
|
2039
|
+
discardPendingChanges();
|
|
2040
|
+
console.log('Cancelled: nothing was written.');
|
|
2041
|
+
}
|
|
2042
|
+
};
|
|
1951
2043
|
const scaffoldNewAggregateUseCase = async (boundedContextFolder)=>{
|
|
1952
2044
|
const aggregateName = await input({
|
|
1953
2045
|
message: 'Aggregate name:'
|
|
@@ -2644,10 +2736,12 @@ export const restHandlers = restWires.map(x => x().handle)
|
|
|
2644
2736
|
'endpoint'
|
|
2645
2737
|
].map((segment)=>segment.startsWith(':') ? pathParameterAsName(segment) : new UnknownFormatNaming(segment).ClassName).join('');
|
|
2646
2738
|
const withWiring = ConfigFile.Instance.data.domainFirstPackages.includes('@domain-first/wire');
|
|
2647
|
-
const
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2739
|
+
const endpointGenerator = findRestEndpointGenerator(boundedContextFolder.subitem([
|
|
2740
|
+
'..',
|
|
2741
|
+
'..',
|
|
2742
|
+
'shared'
|
|
2743
|
+
]));
|
|
2744
|
+
const handler = withWiring ? await promptEndpointHandler(boundedContextFolder) : void 0;
|
|
2651
2745
|
const endpointFile = node_path.join(endpointFolder.path, `${method.toUpperCase()}.ts`);
|
|
2652
2746
|
endpointFolder.createFile(`${method.toUpperCase()}.ts`, `
|
|
2653
2747
|
${endpointGenerator ? `import type { ${endpointGenerator.typeName} } from '${relativeImport(endpointFile, endpointGenerator.typeFile)}'` : "import { EndpointGenerator } from '@domain-first/handlers-rest'"}${handler ? `
|
|
@@ -2744,56 +2838,6 @@ export const wire${naming.ClassName}InfrastructureService = wireClass(
|
|
|
2744
2838
|
await wireNewModuleDependencies(boundedContextFolder, wireFile);
|
|
2745
2839
|
}
|
|
2746
2840
|
};
|
|
2747
|
-
const scaffoldNewPresentationService = async (boundedContextFolder)=>{
|
|
2748
|
-
const name = await input({
|
|
2749
|
-
message: 'Name: '
|
|
2750
|
-
});
|
|
2751
|
-
const naming = new UnknownFormatNaming(name);
|
|
2752
|
-
const scaffoldUnitTests = Boolean(ConfigFile.Instance.data.testingLibrary);
|
|
2753
|
-
const withWiring = ConfigFile.Instance.data.domainFirstPackages.includes('@domain-first/wire');
|
|
2754
|
-
const wiringFolder = boundedContextFolder.subitem([
|
|
2755
|
-
'wiring',
|
|
2756
|
-
'presentation-services'
|
|
2757
|
-
]);
|
|
2758
|
-
const presentationServicesFolder = boundedContextFolder.subitem([
|
|
2759
|
-
'presentation',
|
|
2760
|
-
'services'
|
|
2761
|
-
]);
|
|
2762
|
-
presentationServicesFolder.createFile(`${naming.fileName}-presentation-service.ts`, `
|
|
2763
|
-
export class ${naming.ClassName}PresentationService {
|
|
2764
|
-
|
|
2765
|
-
}
|
|
2766
|
-
`.trim());
|
|
2767
|
-
if (scaffoldUnitTests) presentationServicesFolder.createFile(`${naming.fileName}-presentation-service.spec.ts`, `
|
|
2768
|
-
import { describe, test, expect, beforeEach } from '${ConfigFile.Instance.data.testingLibrary}'
|
|
2769
|
-
${withWiring ? `import { type ${naming.ClassName}PresentationService } from './${naming.fileName}-presentation-service'
|
|
2770
|
-
import { wire${naming.ClassName}PresentationService } from '../../wiring/presentation-services/wire-${naming.fileName}-presentation-service'` : `import { ${naming.ClassName}PresentationService } from './${naming.fileName}-presentation-service'`}
|
|
2771
|
-
|
|
2772
|
-
let ${naming.variableName}PresentationService: ${naming.ClassName}PresentationService
|
|
2773
|
-
|
|
2774
|
-
beforeEach(() => {
|
|
2775
|
-
${naming.variableName}PresentationService = ${withWiring ? `wire${naming.ClassName}PresentationService()` : `new ${naming.ClassName}PresentationService()`}
|
|
2776
|
-
})
|
|
2777
|
-
|
|
2778
|
-
describe('${naming.withSpaces}', () => {
|
|
2779
|
-
test('can be ${withWiring ? 'wired' : 'created'}', () => {
|
|
2780
|
-
expect(${naming.variableName}PresentationService).toBeDefined()
|
|
2781
|
-
})
|
|
2782
|
-
})
|
|
2783
|
-
`.trim());
|
|
2784
|
-
if (withWiring) {
|
|
2785
|
-
const wireFile = wiringFolder.createFile(`wire-${naming.fileName}-presentation-service.ts`, `
|
|
2786
|
-
import { wireClass } from '@domain-first/wire'
|
|
2787
|
-
import { ${naming.ClassName}PresentationService } from '../../presentation/services/${naming.fileName}-presentation-service'
|
|
2788
|
-
|
|
2789
|
-
export const wire${naming.ClassName}PresentationService = wireClass(
|
|
2790
|
-
${naming.ClassName}PresentationService,
|
|
2791
|
-
[]
|
|
2792
|
-
)
|
|
2793
|
-
`.trim());
|
|
2794
|
-
await wireNewModuleDependencies(boundedContextFolder, wireFile);
|
|
2795
|
-
}
|
|
2796
|
-
};
|
|
2797
2841
|
const scaffoldNewQuery = async (boundedContextFolder)=>{
|
|
2798
2842
|
const queriesFolder = boundedContextFolder.subitem([
|
|
2799
2843
|
'application',
|