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

Commit c13f2e3

Browse files
alan-agius4vikerman
authored andcommitted
refactor: remove output path logic as it's handled in universal schematic
If the outputPath change is needed, this should be done at upstream in `angular/cli` instead https://github.com/angular/angular-cli/blob/102cd86a8ea33bf1c3dc8cb75b8727c2f239c3d5/packages/schematics/angular/universal/index.ts#L42
1 parent 5c9c79c commit c13f2e3

File tree

7 files changed

+77
-93
lines changed

7 files changed

+77
-93
lines changed

modules/common/schematics/add/index.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ describe('Add Schematic Rule', () => {
4141
expect(productionConfig.optimization).toBeDefined();
4242
});
4343

44+
it('should add scripts to package.json', async () => {
45+
const tree = await schematicRunner
46+
.callRule(addUniversalCommonRule(defaultOptions), appTree).toPromise();
47+
const {scripts} = JSON.parse(tree.read('package.json')!.toString());
48+
expect(scripts['build:ssr']).toBe('ng build --prod && ng run test-app:server:production');
49+
expect(scripts['serve:ssr']).toBe('node dist/test-app/server/main.js');
50+
});
51+
4452
it(`should update 'tsconfig.server.json' files with main file`, async () => {
4553
const tree = await schematicRunner
4654
.callRule(addUniversalCommonRule(defaultOptions), appTree).toPromise();

modules/common/schematics/add/index.ts

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import {
1212
SchematicsException,
1313
noop,
1414
} from '@angular-devkit/schematics';
15-
import {normalize, join, parseJsonAst, JsonParseMode} from '@angular-devkit/core';
16-
import {updateWorkspace} from '@schematics/angular/utility/workspace';
15+
import {parseJsonAst, JsonParseMode} from '@angular-devkit/core';
1716
import {
1817
findPropertyInAstObject,
1918
appendValueInAstArray,
2019
} from '@schematics/angular/utility/json-utils';
2120
import {Schema as UniversalOptions} from '@schematics/angular/universal/schema';
22-
import {stripTsExtension, getDistPaths, getProject} from '../utils';
21+
import {stripTsExtension, getOutputPath, getProject} from '../utils';
2322

2423
export interface AddUniversalOptions extends UniversalOptions {
2524
serverFileName?: string;
@@ -35,7 +34,6 @@ export function addUniversalCommonRule(options: AddUniversalOptions): Rule {
3534
: externalSchematic('@schematics/angular', 'universal', options),
3635
addScriptsRule(options),
3736
updateServerTsConfigRule(options),
38-
updateConfigFileRule(options),
3937
]);
4038
};
4139
}
@@ -48,7 +46,7 @@ function addScriptsRule(options: AddUniversalOptions): Rule {
4846
throw new SchematicsException('Could not find package.json');
4947
}
5048

51-
const {server: serverDist} = await getDistPaths(host, options.clientProject);
49+
const serverDist = await getOutputPath(host, options.clientProject, 'server');
5250
const pkg = JSON.parse(buffer.toString());
5351
pkg.scripts = {
5452
...pkg.scripts,
@@ -60,42 +58,6 @@ function addScriptsRule(options: AddUniversalOptions): Rule {
6058
};
6159
}
6260

63-
function updateConfigFileRule(options: AddUniversalOptions): Rule {
64-
return host => {
65-
return updateWorkspace((async workspace => {
66-
const clientProject = workspace.projects.get(options.clientProject);
67-
if (clientProject) {
68-
const buildTarget = clientProject.targets.get('build');
69-
const serverTarget = clientProject.targets.get('server');
70-
71-
// We have to check if the project config has a server target, because
72-
// if the Universal step in this schematic isn't run, it can't be guaranteed
73-
// to exist
74-
if (!serverTarget || !buildTarget) {
75-
return;
76-
}
77-
78-
const distPaths = await getDistPaths(host, options.clientProject);
79-
80-
serverTarget.options = {
81-
...serverTarget.options,
82-
outputPath: distPaths.server,
83-
};
84-
85-
serverTarget.options.main = join(
86-
normalize(clientProject.root),
87-
stripTsExtension(options.serverFileName) + '.ts',
88-
);
89-
90-
buildTarget.options = {
91-
...buildTarget.options,
92-
outputPath: distPaths.browser,
93-
};
94-
}
95-
})) as unknown as Rule;
96-
};
97-
}
98-
9961
function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
10062
return async host => {
10163
const clientProject = await getProject(host, options.clientProject);
@@ -140,3 +102,17 @@ function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
140102
}
141103
};
142104
}
105+
106+
export default function (options: UniversalOptions): Rule {
107+
return async host => {
108+
const clientProject = await getProject(host, options.clientProject);
109+
110+
return chain([
111+
clientProject.targets.has('server')
112+
? noop()
113+
: externalSchematic('@schematics/angular', 'universal', options),
114+
addScriptsRule(options),
115+
updateServerTsConfigRule(options),
116+
]);
117+
};
118+
}

modules/common/schematics/utils/utils.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Tree} from '@angular-devkit/schematics/src/tree/interface';
10-
import {workspaces, join, normalize} from '@angular-devkit/core';
10+
import {workspaces} from '@angular-devkit/core';
1111
import {getWorkspace} from '@schematics/angular/utility/workspace';
1212
import {SchematicsException} from '@angular-devkit/schematics';
1313

@@ -29,24 +29,24 @@ export function stripTsExtension(file: string): string {
2929
return file.replace(/\.ts$/, '');
3030
}
3131

32-
export async function getDistPaths(host: Tree, clientProjectName: string): Promise<{
33-
browser: string;
34-
server: string;
35-
}> {
32+
export async function getOutputPath(
33+
host: Tree,
34+
projectName: string,
35+
target: 'server' | 'build',
36+
): Promise<string> {
3637
// Generate new output paths
37-
const clientProject = await getProject(host, clientProjectName);
38-
const clientBuildTarget = clientProject.targets.get('build');
39-
if (!clientBuildTarget || !clientBuildTarget.options) {
40-
throw new SchematicsException(`Cannot find 'options' for ${clientProjectName} build target.`);
38+
const project = await getProject(host, projectName);
39+
const serverTarget = project.targets.get(target);
40+
if (!serverTarget || !serverTarget.options) {
41+
throw new SchematicsException
42+
(`Cannot find 'options' for ${projectName} ${target} target.`);
4143
}
4244

43-
const clientBuildOptions = clientBuildTarget.options;
44-
const clientOutputPath = normalize(
45-
typeof clientBuildOptions.outputPath === 'string' ? clientBuildOptions.outputPath : 'dist'
46-
);
45+
const { outputPath } = serverTarget.options;
46+
if (typeof outputPath !== 'string') {
47+
throw new SchematicsException
48+
(`outputPath for ${projectName} ${target} target is not a string.`);
49+
}
4750

48-
return {
49-
browser: join(clientOutputPath, 'browser'),
50-
server: join(clientOutputPath, 'server'),
51-
};
51+
return outputPath;
5252
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import {
2727
getProject,
2828
stripTsExtension,
29-
getDistPaths,
29+
getOutputPath,
3030
} from '@nguniversal/common/schematics/utils';
3131
import {addUniversalCommonRule} from '@nguniversal/common/schematics/add';
3232

@@ -57,14 +57,14 @@ function addDependencies(options: UniversalOptions): Rule {
5757
export default function (options: UniversalOptions): Rule {
5858
return async (host: Tree) => {
5959
const clientProject = await getProject(host, options.clientProject);
60-
const {browser} = await getDistPaths(host, options.clientProject);
60+
const browserDistDirectory = await getOutputPath(host, options.clientProject, 'build');
6161

6262
const rootSource = apply(url('./files'), [
6363
template({
6464
...strings,
6565
...options,
6666
stripTsExtension,
67-
browserDistDirectory: browser
67+
browserDistDirectory,
6868
}),
6969
move(clientProject.root)
7070
]);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import {
2727
getProject,
2828
stripTsExtension,
29-
getDistPaths,
29+
getOutputPath,
3030
} from '@nguniversal/common/schematics/utils';
3131
import {addUniversalCommonRule} from '@nguniversal/common/schematics/add';
3232

@@ -62,14 +62,14 @@ function addDependencies(options: UniversalOptions): Rule {
6262
export default function (options: UniversalOptions): Rule {
6363
return async (host: Tree) => {
6464
const clientProject = await getProject(host, options.clientProject);
65-
const {browser} = await getDistPaths(host, options.clientProject);
65+
const browserDistDirectory = await getOutputPath(host, options.clientProject, 'build');
6666

6767
const rootSource = apply(url('./files'), [
6868
template({
6969
...strings,
7070
...options,
7171
stripTsExtension,
72-
browserDistDirectory: browser
72+
browserDistDirectory,
7373
}),
7474
move(clientProject.root)
7575
]);

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
"typescript": "~3.5.3"
4848
},
4949
"devDependencies": {
50-
"@angular-devkit/architect": "^0.900.0-next.6",
51-
"@angular-devkit/core": "^9.0.0-next.6",
52-
"@angular-devkit/schematics": "^9.0.0-next.6",
50+
"@angular-devkit/architect": "^0.900.0-next.7",
51+
"@angular-devkit/core": "^9.0.0-next.7",
52+
"@angular-devkit/schematics": "^9.0.0-next.7",
5353
"@angular/bazel": "^9.0.0-next.8",
5454
"@bazel/bazel": "0.28.1",
5555
"@bazel/buildifier": "^0.25.1",
@@ -58,7 +58,7 @@
5858
"@bazel/jasmine": "^0.32.2",
5959
"@bazel/karma": "^0.32.2",
6060
"@bazel/typescript": "^0.32.2",
61-
"@schematics/angular": "^9.0.0-next.6",
61+
"@schematics/angular": "^9.0.0-next.7",
6262
"@types/express": "^4.0.39",
6363
"@types/fs-extra": "^4.0.5",
6464
"@types/hapi": "^17.0.12",

yarn.lock

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
"@angular-devkit/core" "8.0.0-beta.18"
1111
rxjs "6.4.0"
1212

13-
"@angular-devkit/architect@^0.900.0-next.6":
14-
version "0.900.0-next.6"
15-
resolved "https://registry.yarnpkg.com/@angular-devkit/architect/-/architect-0.900.0-next.6.tgz#dbfd2d533fb50c8253ef5bc8fd6f8e57439ef499"
16-
integrity sha512-82g5PUMuiPt7J6qGetrqyzIE3B3AoImx17UY/nsPffsLCt48xnMrnc7munayHBAVbE7XRDXYxVv1yrZX2msDzw==
13+
"@angular-devkit/architect@^0.900.0-next.7":
14+
version "0.900.0-next.7"
15+
resolved "https://registry.yarnpkg.com/@angular-devkit/architect/-/architect-0.900.0-next.7.tgz#0295edd0b601d127cb77526d8fd293b1a8beb315"
16+
integrity sha512-dJQTUM5LoiiKJBF/R5l4mTJTqTF16VvbwPUA+xCz0WBqvhqsoOSWfSvLUyO8I3Wk4/rGM9BhsSH3505nFZtnug==
1717
dependencies:
18-
"@angular-devkit/core" "9.0.0-next.6"
18+
"@angular-devkit/core" "9.0.0-next.7"
1919
rxjs "6.5.3"
2020

2121
"@angular-devkit/[email protected]", "@angular-devkit/core@^8.0.0-beta.15":
@@ -29,14 +29,14 @@
2929
rxjs "6.4.0"
3030
source-map "0.7.3"
3131

32-
"@angular-devkit/[email protected].6", "@angular-devkit/core@^9.0.0-next.6":
33-
version "9.0.0-next.6"
34-
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-9.0.0-next.6.tgz#f8e47cb966177ccdd69c568689aa01c80bd1bc6d"
35-
integrity sha512-01sSgdLcd0vIHNyiNQi77UZc6R9Pdp79tj/oCtWrqomtNgtyp4uNis5gP77PUCAYErtDUYznUb3Fh4oXeMrinw==
32+
"@angular-devkit/[email protected].7", "@angular-devkit/core@^9.0.0-next.7":
33+
version "9.0.0-next.7"
34+
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-9.0.0-next.7.tgz#3ec750cd4f5fe2ca598da1fd2726ee899346033c"
35+
integrity sha512-T/4+fKx9oB5ixa9TtV3/OtlEzXWw1uNT8kLkS5yOj/ZC3XcaGbBzOKns2ELIHgBFjHIeVFLIi49gplRTUy0mNw==
3636
dependencies:
3737
ajv "6.10.2"
3838
fast-json-stable-stringify "2.0.0"
39-
magic-string "0.25.3"
39+
magic-string "0.25.4"
4040
rxjs "6.5.3"
4141
source-map "0.7.3"
4242

@@ -48,12 +48,12 @@
4848
"@angular-devkit/core" "8.0.0-beta.18"
4949
rxjs "6.4.0"
5050

51-
"@angular-devkit/[email protected].6", "@angular-devkit/schematics@^9.0.0-next.6":
52-
version "9.0.0-next.6"
53-
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-9.0.0-next.6.tgz#6b3a80b6793d9d95c3e6f4ccab522d94bc8579bc"
54-
integrity sha512-4O6mwmXVaC/zroP38IfetocrkeZpiztaqzyWeD7BhB/ZacfhY6Pa/+Yqu116UdKsY6DEhoHCSfRWSaj0YRnlsQ==
51+
"@angular-devkit/[email protected].7", "@angular-devkit/schematics@^9.0.0-next.7":
52+
version "9.0.0-next.7"
53+
resolved "https://registry.yarnpkg.com/@angular-devkit/schematics/-/schematics-9.0.0-next.7.tgz#d353944ac034e6c3366578f5681c5244d8de1871"
54+
integrity sha512-8+EDWa150isxTkBZ93odUcD32XBJMDLt8eaJDPZJRwJ+HDuwiEmPdP9EyCcuddd3Xx715fv3xFv5CU45t4bHbg==
5555
dependencies:
56-
"@angular-devkit/core" "9.0.0-next.6"
56+
"@angular-devkit/core" "9.0.0-next.7"
5757
rxjs "6.5.3"
5858

5959
"@angular/animations@^9.0.0-next.8":
@@ -377,13 +377,13 @@
377377
"@angular-devkit/schematics" "8.0.0-beta.18"
378378
"@phenomnomnominal/tsquery" "3.0.0"
379379

380-
"@schematics/angular@^9.0.0-next.6":
381-
version "9.0.0-next.6"
382-
resolved "https://registry.yarnpkg.com/@schematics/angular/-/angular-9.0.0-next.6.tgz#282e01de4bcce08f7b7eefacc54d2cbb9ef40e75"
383-
integrity sha512-02DMivnka1LWHgyeGQqErJtlefRPHxkOJwdhYjB6lVAyNEX38hIpROQJVrgw2fEL+WDTPw9mwtgddNs0g2CtTg==
380+
"@schematics/angular@^9.0.0-next.7":
381+
version "9.0.0-next.7"
382+
resolved "https://registry.yarnpkg.com/@schematics/angular/-/angular-9.0.0-next.7.tgz#49b8e87afd0bc88ee0e3c0426ec55b60ea5c36a7"
383+
integrity sha512-H1u1Q+26xQR2RrYkshWyRSNa3lp/mn/akjr0szkr28B6Q8deUvu0Dz83sdCfbMBYhsPBe9XIEQBUNNkgZ2RjUQ==
384384
dependencies:
385-
"@angular-devkit/core" "9.0.0-next.6"
386-
"@angular-devkit/schematics" "9.0.0-next.6"
385+
"@angular-devkit/core" "9.0.0-next.7"
386+
"@angular-devkit/schematics" "9.0.0-next.7"
387387

388388
389389
version "1.0.33"
@@ -3161,10 +3161,10 @@ [email protected]:
31613161
dependencies:
31623162
sourcemap-codec "^1.4.4"
31633163

3164-
3165-
version "0.25.3"
3166-
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.3.tgz#34b8d2a2c7fec9d9bdf9929a3fd81d271ef35be9"
3167-
integrity sha512-6QK0OpF/phMz0Q2AxILkX2mFhi7m+WMwTRg0LQKq/WBB0cDP4rYH3Wp4/d3OTXlrPLVJT/RFqj8tFeAR4nk8AA==
3164+
3165+
version "0.25.4"
3166+
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.4.tgz#325b8a0a79fc423db109b77fd5a19183b7ba5143"
3167+
integrity sha512-oycWO9nEVAP2RVPbIoDoA4Y7LFIJ3xRYov93gAyJhZkET1tNuB0u7uWkZS2LpBWTJUWnmau/To8ECWRC+jKNfw==
31683168
dependencies:
31693169
sourcemap-codec "^1.4.4"
31703170

0 commit comments

Comments
 (0)