Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit c5ed2d9

Browse files
alan-agius4vikerman
authored andcommitted
refactor: simply common add schematic
With this change, we don't export an actual schematic but rather we export a common rule. This will simply schema changes as we don't have to keep 6 schemas in sync (json and ts)
1 parent 26dfdf2 commit c5ed2d9

File tree

13 files changed

+73
-169
lines changed

13 files changed

+73
-169
lines changed

modules/common/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"ng-update": {
1515
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
1616
},
17-
"schematics": "./schematics/collection.json",
1817
"repository": {
1918
"type": "git",
2019
"url": "https://github.com/angular/universal"

modules/common/schematics/install/index.spec.ts renamed to modules/common/schematics/add/index.spec.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
1010

1111
import {collectionPath, createTestApp} from '../testing/test-app';
1212

13-
import {Schema as UniversalOptions} from './schema';
13+
import {addUniversalCommonRule, AddUniversalOptions} from './index';
1414

15-
describe('Universal Schematic', () => {
16-
const defaultOptions: UniversalOptions = {
15+
describe('Add Schematic Rule', () => {
16+
const defaultOptions: AddUniversalOptions = {
1717
clientProject: 'bar',
18+
serverFileName: 'server.ts',
1819
};
1920

2021
let schematicRunner: SchematicTestRunner;
@@ -27,9 +28,8 @@ describe('Universal Schematic', () => {
2728

2829
it('should update angular.json', async () => {
2930
const tree = await schematicRunner
30-
.runSchematicAsync('install', defaultOptions, appTree)
31-
.toPromise();
32-
const contents = JSON.parse(tree.readContent('angular.json'));
31+
.callRule(addUniversalCommonRule(defaultOptions), appTree).toPromise();
32+
const contents = JSON.parse(tree.read('angular.json')!.toString());
3333
const architect = contents.projects.bar.architect;
3434
expect(architect.build.configurations.production).toBeDefined();
3535
expect(architect.build.options.outputPath).toBe('dist/bar/browser');
@@ -43,26 +43,24 @@ describe('Universal Schematic', () => {
4343

4444
it(`should update 'tsconfig.server.json' files with main file`, async () => {
4545
const tree = await schematicRunner
46-
.runSchematicAsync('install', defaultOptions, appTree)
47-
.toPromise();
46+
.callRule(addUniversalCommonRule(defaultOptions), appTree).toPromise();
4847

49-
const contents = JSON.parse(tree.readContent('/projects/bar/tsconfig.server.json'));
48+
const contents = JSON.parse(tree.read('/projects/bar/tsconfig.server.json')!.toString());
5049
expect(contents.files).toEqual([
5150
'src/main.server.ts',
5251
'server.ts',
5352
]);
5453
});
5554

5655
it(`should work when server target already exists`, async () => {
57-
let tree = await schematicRunner
56+
appTree = await schematicRunner
5857
.runExternalSchematicAsync('@schematics/angular', 'universal', defaultOptions, appTree)
5958
.toPromise();
6059

61-
tree = await schematicRunner
62-
.runSchematicAsync('install', defaultOptions, tree)
63-
.toPromise();
60+
const tree = await schematicRunner
61+
.callRule(addUniversalCommonRule(defaultOptions), appTree).toPromise();
6462

65-
const contents = JSON.parse(tree.readContent('/projects/bar/tsconfig.server.json'));
63+
const contents = JSON.parse(tree.read('/projects/bar/tsconfig.server.json')!.toString());
6664
expect(contents.files).toEqual([
6765
'src/main.server.ts',
6866
'server.ts',

modules/common/schematics/install/index.ts renamed to modules/common/schematics/add/index.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@ import {
1818
findPropertyInAstObject,
1919
appendValueInAstArray,
2020
} from '@schematics/angular/utility/json-utils';
21-
import {Schema as UniversalOptions} from './schema';
22-
import {stripTsExtension, getDistPaths, getClientProject} from './utils';
21+
import {Schema as UniversalOptions} from '@schematics/angular/universal/schema';
22+
import {stripTsExtension, getDistPaths, getClientProject} from '../utils';
2323

24-
function addScriptsRule(options: UniversalOptions): Rule {
24+
export interface AddUniversalOptions extends UniversalOptions {
25+
serverFileName?: string;
26+
}
27+
28+
export function addUniversalCommonRule(options: AddUniversalOptions): Rule {
29+
return async host => {
30+
const clientProject = await getClientProject(host, options.clientProject);
31+
32+
return chain([
33+
clientProject.targets.has('server')
34+
? noop()
35+
: externalSchematic('@schematics/angular', 'universal', options),
36+
addScriptsRule(options),
37+
updateServerTsConfigRule(options),
38+
updateConfigFileRule(options),
39+
]);
40+
};
41+
}
42+
43+
function addScriptsRule(options: AddUniversalOptions): Rule {
2544
return async host => {
2645
const pkgPath = '/package.json';
2746
const buffer = host.read(pkgPath);
@@ -41,7 +60,7 @@ function addScriptsRule(options: UniversalOptions): Rule {
4160
};
4261
}
4362

44-
function updateConfigFileRule(options: UniversalOptions): Rule {
63+
function updateConfigFileRule(options: AddUniversalOptions): Rule {
4564
return host => {
4665
return updateWorkspace((async workspace => {
4766
const clientProject = workspace.projects.get(options.clientProject);
@@ -77,7 +96,7 @@ function updateConfigFileRule(options: UniversalOptions): Rule {
7796
};
7897
}
7998

80-
function updateServerTsConfigRule(options: UniversalOptions): Rule {
99+
function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
81100
return async host => {
82101
const clientProject = await getClientProject(host, options.clientProject);
83102
const serverTarget = clientProject.targets.get('server');
@@ -117,18 +136,3 @@ function updateServerTsConfigRule(options: UniversalOptions): Rule {
117136
}
118137
};
119138
}
120-
121-
export default function (options: UniversalOptions): Rule {
122-
return async host => {
123-
const clientProject = await getClientProject(host, options.clientProject);
124-
125-
return chain([
126-
clientProject.targets.has('server')
127-
? noop()
128-
: externalSchematic('@schematics/angular', 'universal', options),
129-
addScriptsRule(options),
130-
updateServerTsConfigRule(options),
131-
updateConfigFileRule(options),
132-
]);
133-
};
134-
}

modules/common/schematics/collection.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

modules/common/schematics/install/schema.json

Lines changed: 0 additions & 59 deletions
This file was deleted.

modules/common/schematics/install/schema.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

modules/common/schematics/testing/test-app.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {Observable} from 'rxjs';
1111
import {switchMap} from 'rxjs/operators';
1212

1313
/** Path to the collection file for the NgUniversal schematics */
14-
export const collectionPath =
15-
require.resolve('nguniversal/modules/common/schematics/collection.json');
14+
export const collectionPath = require.resolve('@schematics/angular/collection.json');
1615

1716
/** Create a base app used for testing. */
1817
export function createTestApp(appOptions = {}): Observable<UnitTestTree> {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './utils';

modules/express-engine/schematics/install/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {strings} from '@angular-devkit/core';
99
import {
1010
apply,
1111
chain,
12-
externalSchematic,
1312
mergeWith,
1413
Rule,
1514
SchematicContext,
@@ -28,7 +27,8 @@ import {
2827
getClientProject,
2928
stripTsExtension,
3029
getDistPaths,
31-
} from '@nguniversal/common/schematics/install/utils';
30+
} from '@nguniversal/common/schematics/utils';
31+
import {addUniversalCommonRule} from '@nguniversal/common/schematics/add';
3232

3333
function addDependencies(options: UniversalOptions): Rule {
3434
return (host: Tree, context: SchematicContext) => {
@@ -69,14 +69,9 @@ export default function (options: UniversalOptions): Rule {
6969
move(clientProject.root)
7070
]);
7171

72-
// Under bazel the collection needs to be resolved differently
73-
const ngCommonUniversalCollection = process.env.BAZEL_TARGET
74-
? require.resolve('nguniversal/modules/common/schematics/npm_package/collection.json')
75-
: '@nguniversal/common';
76-
7772
return chain([
7873
mergeWith(rootSource),
79-
externalSchematic(ngCommonUniversalCollection, 'install', options),
74+
addUniversalCommonRule(options),
8075
addDependencies(options),
8176
]);
8277
};

0 commit comments

Comments
 (0)