Skip to content

Commit b2b414f

Browse files
committed
feat(ui): support passing import path to publishable lib
1 parent 2570d9a commit b2b414f

File tree

2 files changed

+91
-26
lines changed

2 files changed

+91
-26
lines changed

libs/ddd/src/schematics/ui/ui.spec.ts

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ describe('ui', () => {
3838
const nxJson = readJsonInTree<NxJson>(tree, '/nx.json');
3939
expect(nxJson.projects).toEqual({
4040
'shared-ui-form-components': {
41-
tags: ['domain:shared', 'type:ui']
42-
}
41+
tags: ['domain:shared', 'type:ui'],
42+
},
4343
});
4444
});
4545

@@ -53,8 +53,8 @@ describe('ui', () => {
5353
const nxJson = readJsonInTree<NxJson>(tree, '/nx.json');
5454
expect(nxJson.projects).toEqual({
5555
'customer-ui-form-components': {
56-
tags: ['domain:customer', 'type:ui']
57-
}
56+
tags: ['domain:customer', 'type:ui'],
57+
},
5858
});
5959
});
6060

@@ -82,10 +82,12 @@ describe('ui', () => {
8282
);
8383

8484
const workspaceJson = readJsonInTree(tree, '/workspace.json');
85-
expect(workspaceJson.projects).toHaveProperty('customer-forms-ui-form-components');
86-
expect(workspaceJson.projects['customer-forms-ui-form-components'].root).toEqual(
87-
'libs/customer/forms/ui-form-components'
85+
expect(workspaceJson.projects).toHaveProperty(
86+
'customer-forms-ui-form-components'
8887
);
88+
expect(
89+
workspaceJson.projects['customer-forms-ui-form-components'].root
90+
).toEqual('libs/customer/forms/ui-form-components');
8991
});
9092

9193
it('should keep correct tags with a customized directory', async () => {
@@ -98,8 +100,51 @@ describe('ui', () => {
98100
const nxJson = readJsonInTree<NxJson>(tree, '/nx.json');
99101
expect(nxJson.projects).toEqual({
100102
'customer-forms-ui-form-components': {
101-
tags: ['domain:customer', 'type:ui']
102-
}
103+
tags: ['domain:customer', 'type:ui'],
104+
},
103105
});
104106
});
107+
108+
it('should add valid import path to publishable lib', async () => {
109+
const tree = await runSchematic<UiOptions>(
110+
'ui',
111+
{ name: 'form-components', shared: true, type: 'publishable' },
112+
appTree
113+
);
114+
115+
let ngPackage = readJsonInTree(
116+
tree,
117+
'libs/shared/ui-form-components/ng-package.json'
118+
);
119+
expect(ngPackage).toBeDefined();
120+
const packageJson = readJsonInTree(
121+
tree,
122+
'libs/shared/ui-form-components/package.json'
123+
);
124+
expect(packageJson.name).toEqual('@proj/shared-ui-form-components');
125+
});
126+
127+
it('should add valid import path to publishable lib with customized directory', async () => {
128+
const tree = await runSchematic<UiOptions>(
129+
'ui',
130+
{
131+
name: 'form-components',
132+
shared: true,
133+
type: 'publishable',
134+
directory: 'forms',
135+
},
136+
appTree
137+
);
138+
139+
let ngPackage = readJsonInTree(
140+
tree,
141+
'libs/shared/forms/ui-form-components/ng-package.json'
142+
);
143+
expect(ngPackage).toBeDefined();
144+
const packageJson = readJsonInTree(
145+
tree,
146+
'libs/shared/forms/ui-form-components/package.json'
147+
);
148+
expect(packageJson.name).toEqual('@proj/shared-forms-ui-form-components');
149+
});
105150
});

libs/ddd/src/schematics/ui/ui.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { strings } from '@angular-devkit/core';
2-
import { chain, externalSchematic, Rule } from '@angular-devkit/schematics';
2+
import {
3+
chain,
4+
externalSchematic,
5+
Rule,
6+
Tree
7+
} from '@angular-devkit/schematics';
8+
import { getNpmScope } from '@nrwl/workspace';
39
import { UiOptions } from './schema';
410

511
function validateInputs(options: UiOptions): void {
@@ -16,22 +22,36 @@ function validateInputs(options: UiOptions): void {
1622
}
1723
}
1824

19-
export default function(options: UiOptions): Rule {
20-
validateInputs(options);
25+
export default function (options: UiOptions): Rule {
26+
return (host: Tree) => {
27+
validateInputs(options);
2128

22-
const libName = strings.dasherize(options.name);
23-
const domain = options.shared ? 'shared' : options.domain;
24-
const libDir = options.directory ? `${domain}/${options.directory}` : domain;
29+
const libName = `ui-${strings.dasherize(options.name)}`;
30+
const domain = options.shared ? 'shared' : options.domain;
31+
const libDirectory = options.directory
32+
? `${domain}/${options.directory}`
33+
: domain;
34+
const isPublishableLib = options.type === 'publishable';
35+
const npmScope = getNpmScope(host);
36+
const projectName = `${libDirectory}-${libName}`.replace(
37+
new RegExp('/', 'g'),
38+
'-'
39+
);
40+
const importPath = isPublishableLib
41+
? `@${npmScope}/${projectName}`
42+
: undefined;
2543

26-
return chain([
27-
externalSchematic('@nrwl/angular', 'lib', {
28-
name: `ui-${libName}`,
29-
directory: libDir,
30-
tags: `domain:${domain},type:ui`,
31-
style: 'scss',
32-
prefix: options.name,
33-
publishable: options.type === 'publishable',
34-
buildable: options.type === 'buildable'
35-
})
36-
]);
44+
return chain([
45+
externalSchematic('@nrwl/angular', 'lib', {
46+
name: libName,
47+
tags: `domain:${domain},type:ui`,
48+
style: 'scss',
49+
prefix: options.name,
50+
publishable: isPublishableLib,
51+
buildable: options.type === 'buildable',
52+
directory: libDirectory,
53+
importPath,
54+
}),
55+
]);
56+
};
3757
}

0 commit comments

Comments
 (0)