Skip to content

Commit 8c212fa

Browse files
committed
improvement(ddd): adding more imput validation
1 parent 529f7d6 commit 8c212fa

File tree

6 files changed

+93
-35
lines changed

6 files changed

+93
-35
lines changed

libs/ddd/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ nx g @xmlking/nxp-ddd:entity user --domain booking
5959
nx g @xmlking/nxp-ddd:entity seat --domain boarding
6060
```
6161

62+
### Post action
63+
64+
After generation all modules, you might have to remove `pathMatch: 'full'` and adjust your routes in some module's Router config .<br/>
65+
Optionally add `<router-outlet></router-outlet>` in shell component's HTML
66+
6267
## Running unit tests
6368

6469
Run `nx test ddd` to execute the unit tests via [Jest](https://jestjs.io).

libs/ddd/src/schematics/domain/index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@ import { DomainOptions } from './schema';
1414

1515
export default function (options: DomainOptions): Rule {
1616
return (host: Tree) => {
17+
const workspace = getWorkspace(host);
18+
// getting project name
19+
if (!options.app) {
20+
if (workspace.defaultProject) {
21+
options.app = workspace.defaultProject;
22+
} else {
23+
throw new SchematicsException('No Angular project selected and no default project in the workspace');
24+
}
25+
}
26+
27+
// Validating project name
28+
const project = workspace.projects[options.app];
29+
if (!project) {
30+
throw new SchematicsException('The specified Angular project is not defined in this workspace');
31+
}
32+
33+
// Checking if it is application
34+
if (project.projectType !== 'application') {
35+
throw new SchematicsException(`Domain requires an Angular project type of "application" in angular.json`);
36+
}
37+
38+
const appName = options.app;
39+
const appFolderName = dasherize(appName);
40+
const appModulePath = `apps/${appFolderName}/src/app/app.module.ts`;
41+
42+
// Checking if appModulePath exists
43+
if (!host.exists(appModulePath)) {
44+
throw new SchematicsException(`Specified app ${options.app} does not exist: ${appModulePath} expected!`);
45+
}
46+
1747
const domainName = dasherize(options.name);
1848
const domainFolderName = domainName;
1949
const domainPath = `libs/${domainFolderName}/domain/src/lib`;
@@ -31,14 +61,6 @@ export default function (options: DomainOptions): Rule {
3161
};
3262
}
3363

34-
const appName = options.app ?? getWorkspace(host)?.defaultProject;
35-
const appFolderName = dasherize(appName);
36-
const appModulePath = `apps/${appFolderName}/src/app/app.module.ts`;
37-
38-
if (!host.exists(appModulePath)) {
39-
throw new SchematicsException(`Specified app ${options.app} does not exist: ${appModulePath} expected!`);
40-
}
41-
4264
return chain([
4365
externalSchematic('@nrwl/angular', 'lib', {
4466
name: `shell${suffix}`,

libs/ddd/src/schematics/feature/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { normalize } from '@angular-devkit/core';
2-
import { dasherize } from '@angular-devkit/core/src/utils/strings';
2+
import { capitalize, dasherize } from '@angular-devkit/core/src/utils/strings';
33
import {
44
chain,
55
externalSchematic,
@@ -10,10 +10,34 @@ import {
1010
SchematicsException,
1111
Tree,
1212
} from '@angular-devkit/schematics';
13+
import { getWorkspace } from '@schematics/angular/utility/config';
1314
import { FeatureOptions } from './schema';
1415

1516
export default function (options: FeatureOptions): Rule {
1617
return (host: Tree) => {
18+
const workspace = getWorkspace(host);
19+
const domainProjectName = dasherize(
20+
`${options.domain}Shell${options.platform ? capitalize(options.platform) : ''}`
21+
);
22+
const domainShellProjectName = dasherize(
23+
`${options.domain}Domain${options.platform ? capitalize(options.platform) : ''}`
24+
);
25+
26+
// Validating domain name
27+
const domainProject = workspace.projects[domainProjectName];
28+
if (!domainProject) {
29+
throw new SchematicsException('The specified domain is not defined in this workspace');
30+
} else if (domainProject.projectType !== 'library') {
31+
throw new SchematicsException(`${domainProjectName} should be Angular project type of "library" in angular.json`);
32+
}
33+
34+
const domainShellProject = workspace.projects[domainShellProjectName];
35+
if (!domainShellProject) {
36+
throw new SchematicsException('The specified domain shell is not defined in this workspace');
37+
} else if (domainShellProject.projectType !== 'library') {
38+
`${domainShellProjectName} should be Angular project type of "library" in angular.json`;
39+
}
40+
1741
const domainName = dasherize(options.domain);
1842
const domainFolderName = domainName;
1943

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1-
import { chain, Rule } from '@angular-devkit/schematics';
2-
import { addNgxsToPackageJson, initLintingRules } from '../rules';
1+
import { chain, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
2+
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
3+
import { addDepsToPackageJson } from '@nrwl/workspace/src/utils/ast-utils';
4+
import { initLintingRules } from '../rules';
35

4-
export default function(): Rule {
5-
return chain([initLintingRules(), addNgxsToPackageJson()]);
6+
const ngxsVersion = '^3.6.2';
7+
const dddVersion = '^0.2.0';
8+
export function addPackageJsonDependencies(): Rule {
9+
return addDepsToPackageJson(
10+
{
11+
'@ngxs/devtools-plugin': ngxsVersion,
12+
'@ngxs/form-plugin': ngxsVersion,
13+
'@ngxs/logger-plugin': ngxsVersion,
14+
'@ngxs/router-plugin': ngxsVersion,
15+
'@ngxs/storage-plugin': ngxsVersion,
16+
'@ngxs/store': ngxsVersion,
17+
},
18+
// always add the package under dev dependencies
19+
{ '@xmlking/nxp-ddd': dddVersion }
20+
);
21+
}
22+
23+
export function installPackageJsonDependencies(): Rule {
24+
return (host: Tree, context: SchematicContext) => {
25+
context.addTask(new NodePackageInstallTask());
26+
context.logger.log('info', `🔍 Installing packages...`);
27+
28+
return host;
29+
};
30+
}
31+
32+
export default function (): Rule {
33+
return chain([initLintingRules(), addPackageJsonDependencies(), installPackageJsonDependencies()]);
634
}

libs/ddd/src/schematics/rules/add-ngxs-to-package-json.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
export { addExportsToBarrel } from './add-exports-to-barrel';
22
export { addNgxsImportsToModule } from './add-ngxs-imports-to-module';
3-
export { addNgxsToPackageJson } from './add-ngxs-to-package-json';
43
export { readWorkspaceName } from './read-workspace-name';
5-
export {
6-
addDomainToLintingRules,
7-
initLintingRules
8-
} from './update-linting-rules';
4+
export { addDomainToLintingRules, initLintingRules } from './update-linting-rules';

0 commit comments

Comments
 (0)