Skip to content

Commit 3f8c3d8

Browse files
committed
feat: add support for Angular v11
1 parent c4e5275 commit 3f8c3d8

File tree

6 files changed

+2800
-2987
lines changed

6 files changed

+2800
-2987
lines changed

src/deploy/actions.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { BuilderContext } from '@angular-devkit/architect';
1+
import {
2+
BuilderContext,
3+
targetFromTargetString,
4+
} from '@angular-devkit/architect';
25
import { json, logging } from '@angular-devkit/core';
36

47
import { Schema } from './schema';
8+
import { BuildTarget } from '../interfaces';
59

610
export default async function deploy(
711
engine: {
@@ -12,7 +16,7 @@ export default async function deploy(
1216
) => Promise<void>;
1317
},
1418
context: BuilderContext,
15-
projectRoot: string,
19+
buildTarget: BuildTarget,
1620
options: Schema
1721
) {
1822
if (options.noBuild) {
@@ -26,7 +30,7 @@ export default async function deploy(
2630
? options.configuration
2731
: 'production';
2832
const overrides = {
29-
...(options.baseHref && { baseHref: options.baseHref })
33+
...(options.baseHref && { baseHref: options.baseHref }),
3034
};
3135

3236
context.logger.info(
@@ -41,7 +45,7 @@ export default async function deploy(
4145
{
4246
target: 'build',
4347
project: context.target.project,
44-
configuration
48+
configuration,
4549
},
4650
overrides as json.JsonObject
4751
);
@@ -52,8 +56,17 @@ export default async function deploy(
5256
}
5357
}
5458

59+
const buildOptions = await context.getTargetOptions(
60+
targetFromTargetString(buildTarget.name)
61+
);
62+
if (!buildOptions.outputPath || typeof buildOptions.outputPath !== 'string') {
63+
throw new Error(
64+
`Cannot read the output path option of the Angular project '${buildTarget.name}' in angular.json`
65+
);
66+
}
67+
5568
await engine.run(
56-
projectRoot,
69+
buildOptions.outputPath,
5770
options,
5871
(context.logger as unknown) as logging.LoggerApi
5972
);

src/deploy/builder.ts

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,27 @@
11
import {
22
BuilderContext,
33
BuilderOutput,
4-
createBuilder
4+
createBuilder,
55
} from '@angular-devkit/architect';
6-
import { asWindowsPath, experimental, normalize } from '@angular-devkit/core';
7-
import { NodeJsSyncHost } from '@angular-devkit/core/node';
8-
import os from 'os';
9-
import * as path from 'path';
106

117
import * as engine from '../engine/engine';
128
import deploy from './actions';
139
import { Schema } from './schema';
1410

1511
// Call the createBuilder() function to create a builder. This mirrors
1612
// createJobHandler() but add typings specific to Architect Builders.
17-
export default createBuilder<any>(
13+
export default createBuilder(
1814
async (options: Schema, context: BuilderContext): Promise<BuilderOutput> => {
19-
try {
20-
// The project root is added to a BuilderContext.
21-
const root = normalize(context.workspaceRoot);
22-
const workspace = new experimental.workspace.Workspace(
23-
root,
24-
new NodeJsSyncHost()
25-
);
26-
await workspace
27-
.loadWorkspaceFromHost(normalize('angular.json'))
28-
.toPromise();
29-
30-
if (!context.target) {
31-
throw new Error('Cannot deploy the application without a target');
32-
}
33-
34-
const targets = workspace.getProjectTargets(context.target.project);
35-
36-
if (
37-
!targets ||
38-
!targets.build ||
39-
!targets.build.options ||
40-
!targets.build.options.outputPath
41-
) {
42-
throw new Error('Cannot find the project output directory');
43-
}
15+
if (!context.target) {
16+
throw new Error('Cannot deploy the application without a target');
17+
}
4418

45-
const isWin = os.platform() === 'win32';
46-
const workspaceRoot = !isWin
47-
? workspace.root
48-
: asWindowsPath(workspace.root);
19+
const buildTarget = {
20+
name: `${context.target.project}:build:production`,
21+
};
4922

50-
await deploy(
51-
engine,
52-
context,
53-
path.join(workspaceRoot, targets.build.options.outputPath),
54-
options
55-
);
23+
try {
24+
await deploy(engine, context, buildTarget, options);
5625
} catch (e) {
5726
context.logger.error('❌ An error occurred when trying to deploy:');
5827
context.logger.error(e.message);

src/interfaces.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,21 @@ export interface GHPages {
22
publish(dir: string, options: any, callback: (error: any) => void);
33
clean?(): void;
44
}
5+
6+
export interface WorkspaceProject {
7+
projectType?: string;
8+
architect?: Record<
9+
string,
10+
{ builder: string; options?: Record<string, any> }
11+
>;
12+
}
13+
14+
export interface Workspace {
15+
defaultProject?: string;
16+
projects: Record<string, WorkspaceProject>;
17+
}
18+
19+
export interface BuildTarget {
20+
name: string;
21+
options?: { [name: string]: any };
22+
}

src/ng-add.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
1-
import { experimental, JsonParseMode, parseJson } from '@angular-devkit/core';
1+
import { JsonParseMode, parseJson } from '@angular-devkit/core';
22
import {
33
SchematicContext,
44
SchematicsException,
5-
Tree
5+
Tree,
66
} from '@angular-devkit/schematics';
7+
import { Workspace } from './interfaces';
78

8-
function getWorkspace(
9-
host: Tree
10-
): { path: string; workspace: experimental.workspace.WorkspaceSchema } {
9+
function getWorkspace(host: Tree): { path: string; workspace: Workspace } {
1110
const possibleFiles = ['/angular.json', '/.angular.json'];
12-
const path = possibleFiles.filter(path => host.exists(path))[0];
11+
const path = possibleFiles.filter((path) => host.exists(path))[0];
1312

1413
const configBuffer = host.read(path);
1514
if (configBuffer === null) {
1615
throw new SchematicsException(`Could not find angular.json`);
1716
}
1817
const content = configBuffer.toString();
1918

20-
let workspace: experimental.workspace.WorkspaceSchema;
19+
let workspace: Workspace;
2120
try {
22-
workspace = (parseJson(
23-
content,
24-
JsonParseMode.Loose
25-
) as {}) as experimental.workspace.WorkspaceSchema;
21+
workspace = (parseJson(content, JsonParseMode.Loose) as {}) as Workspace;
2622
} catch (e) {
2723
throw new SchematicsException(`Could not parse angular.json: ` + e.message);
2824
}
2925

3026
return {
3127
path,
32-
workspace
28+
workspace,
3329
};
3430
}
3531
interface NgAddOptions {
@@ -78,7 +74,7 @@ export const ngAdd = (options: NgAddOptions) => (
7874

7975
project.architect['deploy'] = {
8076
builder: 'angular-cli-ghpages:deploy',
81-
options: {}
77+
options: {},
8278
};
8379

8480
tree.overwrite(workspacePath, JSON.stringify(workspace, null, 2));

0 commit comments

Comments
 (0)