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

Commit

Permalink
refactor: simply common add schematic
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
alan-agius4 authored and vikerman committed Oct 1, 2019
1 parent 26dfdf2 commit c5ed2d9
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 169 deletions.
1 change: 0 additions & 1 deletion modules/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"ng-update": {
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
},
"schematics": "./schematics/collection.json",
"repository": {
"type": "git",
"url": "https://github.com/angular/universal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {SchematicTestRunner} from '@angular-devkit/schematics/testing';

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

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

describe('Universal Schematic', () => {
const defaultOptions: UniversalOptions = {
describe('Add Schematic Rule', () => {
const defaultOptions: AddUniversalOptions = {
clientProject: 'bar',
serverFileName: 'server.ts',
};

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

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

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

const contents = JSON.parse(tree.readContent('/projects/bar/tsconfig.server.json'));
const contents = JSON.parse(tree.read('/projects/bar/tsconfig.server.json')!.toString());
expect(contents.files).toEqual([
'src/main.server.ts',
'server.ts',
]);
});

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

tree = await schematicRunner
.runSchematicAsync('install', defaultOptions, tree)
.toPromise();
const tree = await schematicRunner
.callRule(addUniversalCommonRule(defaultOptions), appTree).toPromise();

const contents = JSON.parse(tree.readContent('/projects/bar/tsconfig.server.json'));
const contents = JSON.parse(tree.read('/projects/bar/tsconfig.server.json')!.toString());
expect(contents.files).toEqual([
'src/main.server.ts',
'server.ts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,29 @@ import {
findPropertyInAstObject,
appendValueInAstArray,
} from '@schematics/angular/utility/json-utils';
import {Schema as UniversalOptions} from './schema';
import {stripTsExtension, getDistPaths, getClientProject} from './utils';
import {Schema as UniversalOptions} from '@schematics/angular/universal/schema';
import {stripTsExtension, getDistPaths, getClientProject} from '../utils';

function addScriptsRule(options: UniversalOptions): Rule {
export interface AddUniversalOptions extends UniversalOptions {
serverFileName?: string;
}

export function addUniversalCommonRule(options: AddUniversalOptions): Rule {
return async host => {
const clientProject = await getClientProject(host, options.clientProject);

return chain([
clientProject.targets.has('server')
? noop()
: externalSchematic('@schematics/angular', 'universal', options),
addScriptsRule(options),
updateServerTsConfigRule(options),
updateConfigFileRule(options),
]);
};
}

function addScriptsRule(options: AddUniversalOptions): Rule {
return async host => {
const pkgPath = '/package.json';
const buffer = host.read(pkgPath);
Expand All @@ -41,7 +60,7 @@ function addScriptsRule(options: UniversalOptions): Rule {
};
}

function updateConfigFileRule(options: UniversalOptions): Rule {
function updateConfigFileRule(options: AddUniversalOptions): Rule {
return host => {
return updateWorkspace((async workspace => {
const clientProject = workspace.projects.get(options.clientProject);
Expand Down Expand Up @@ -77,7 +96,7 @@ function updateConfigFileRule(options: UniversalOptions): Rule {
};
}

function updateServerTsConfigRule(options: UniversalOptions): Rule {
function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
return async host => {
const clientProject = await getClientProject(host, options.clientProject);
const serverTarget = clientProject.targets.get('server');
Expand Down Expand Up @@ -117,18 +136,3 @@ function updateServerTsConfigRule(options: UniversalOptions): Rule {
}
};
}

export default function (options: UniversalOptions): Rule {
return async host => {
const clientProject = await getClientProject(host, options.clientProject);

return chain([
clientProject.targets.has('server')
? noop()
: externalSchematic('@schematics/angular', 'universal', options),
addScriptsRule(options),
updateServerTsConfigRule(options),
updateConfigFileRule(options),
]);
};
}
10 changes: 0 additions & 10 deletions modules/common/schematics/collection.json

This file was deleted.

59 changes: 0 additions & 59 deletions modules/common/schematics/install/schema.json

This file was deleted.

46 changes: 0 additions & 46 deletions modules/common/schematics/install/schema.ts

This file was deleted.

3 changes: 1 addition & 2 deletions modules/common/schematics/testing/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {Observable} from 'rxjs';
import {switchMap} from 'rxjs/operators';

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

/** Create a base app used for testing. */
export function createTestApp(appOptions = {}): Observable<UnitTestTree> {
Expand Down
9 changes: 9 additions & 0 deletions modules/common/schematics/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './utils';
11 changes: 3 additions & 8 deletions modules/express-engine/schematics/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {strings} from '@angular-devkit/core';
import {
apply,
chain,
externalSchematic,
mergeWith,
Rule,
SchematicContext,
Expand All @@ -28,7 +27,8 @@ import {
getClientProject,
stripTsExtension,
getDistPaths,
} from '@nguniversal/common/schematics/install/utils';
} from '@nguniversal/common/schematics/utils';
import {addUniversalCommonRule} from '@nguniversal/common/schematics/add';

function addDependencies(options: UniversalOptions): Rule {
return (host: Tree, context: SchematicContext) => {
Expand Down Expand Up @@ -69,14 +69,9 @@ export default function (options: UniversalOptions): Rule {
move(clientProject.root)
]);

// Under bazel the collection needs to be resolved differently
const ngCommonUniversalCollection = process.env.BAZEL_TARGET
? require.resolve('nguniversal/modules/common/schematics/npm_package/collection.json')
: '@nguniversal/common';

return chain([
mergeWith(rootSource),
externalSchematic(ngCommonUniversalCollection, 'install', options),
addUniversalCommonRule(options),
addDependencies(options),
]);
};
Expand Down
11 changes: 3 additions & 8 deletions modules/hapi-engine/schematics/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {strings} from '@angular-devkit/core';
import {
apply,
chain,
externalSchematic,
mergeWith,
Rule,
SchematicContext,
Expand All @@ -28,7 +27,8 @@ import {
getClientProject,
stripTsExtension,
getDistPaths,
} from '@nguniversal/common/schematics/install/utils';
} from '@nguniversal/common/schematics/utils';
import {addUniversalCommonRule} from '@nguniversal/common/schematics/add';

function addDependencies(options: UniversalOptions): Rule {
return (host: Tree, context: SchematicContext) => {
Expand Down Expand Up @@ -74,14 +74,9 @@ export default function (options: UniversalOptions): Rule {
move(clientProject.root)
]);

// Under bazel the collection needs to be resolved differently
const ngCommonUniversalCollection = process.env.BAZEL_TARGET
? require.resolve('nguniversal/modules/common/schematics/npm_package/collection.json')
: '@nguniversal/common';

return chain([
mergeWith(rootSource),
externalSchematic(ngCommonUniversalCollection, 'install', options),
addUniversalCommonRule(options),
addDependencies(options),
]);
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"devDependencies": {
"@angular-devkit/architect": "^0.900.0-next.5",
"@angular-devkit/core": "^9.0.0-next.5",
"@angular-devkit/schematics": "^9.0.0-next.5",
"@angular/bazel": "^9.0.0-next.8",
"@bazel/bazel": "0.28.1",
"@bazel/buildifier": "^0.25.1",
Expand Down
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
rxjs "6.4.0"
source-map "0.7.3"

"@angular-devkit/[email protected]":
version "9.0.0-next.6"
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-9.0.0-next.6.tgz#f8e47cb966177ccdd69c568689aa01c80bd1bc6d"
integrity sha512-01sSgdLcd0vIHNyiNQi77UZc6R9Pdp79tj/oCtWrqomtNgtyp4uNis5gP77PUCAYErtDUYznUb3Fh4oXeMrinw==
dependencies:
ajv "6.10.2"
fast-json-stable-stringify "2.0.0"
magic-string "0.25.3"
rxjs "6.5.3"
source-map "0.7.3"

"@angular-devkit/[email protected]", "@angular-devkit/schematics@^8.0.0-beta.15":
version "8.0.0-beta.18"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-8.0.0-beta.18.tgz#97dece6eaddd331516a94da985f33add21b76f5c"
Expand All @@ -56,6 +67,14 @@
"@angular-devkit/core" "9.0.0-next.5"
rxjs "6.4.0"

"@angular-devkit/schematics@^9.0.0-next.5":
version "9.0.0-next.6"
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-9.0.0-next.6.tgz#6b3a80b6793d9d95c3e6f4ccab522d94bc8579bc"
integrity sha512-4O6mwmXVaC/zroP38IfetocrkeZpiztaqzyWeD7BhB/ZacfhY6Pa/+Yqu116UdKsY6DEhoHCSfRWSaj0YRnlsQ==
dependencies:
"@angular-devkit/core" "9.0.0-next.6"
rxjs "6.5.3"

"@angular/animations@^9.0.0-next.8":
version "9.0.0-next.8"
resolved "https://registry.yarnpkg.com/@angular/animations/-/animations-9.0.0-next.8.tgz#a0931bb11f6d07bdadb79e9c4ccbb34e5f4e97c3"
Expand Down Expand Up @@ -4241,7 +4260,7 @@ [email protected]:
dependencies:
tslib "^1.9.0"

rxjs@^6.5.3:
rxjs@6.5.3, rxjs@^6.5.3:
version "6.5.3"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.3.tgz#510e26317f4db91a7eb1de77d9dd9ba0a4899a3a"
integrity sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==
Expand Down

0 comments on commit c5ed2d9

Please sign in to comment.