Skip to content

feat: add support for Angular v11 #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/deploy/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { BuilderContext } from '@angular-devkit/architect';
import {
BuilderContext,
targetFromTargetString,
} from '@angular-devkit/architect';
import { json, logging } from '@angular-devkit/core';

import { Schema } from './schema';
import { BuildTarget } from '../interfaces';

export default async function deploy(
engine: {
Expand All @@ -12,7 +16,7 @@ export default async function deploy(
) => Promise<void>;
},
context: BuilderContext,
projectRoot: string,
buildTarget: BuildTarget,
options: Schema
) {
if (options.noBuild) {
Expand All @@ -26,7 +30,7 @@ export default async function deploy(
? options.configuration
: 'production';
const overrides = {
...(options.baseHref && { baseHref: options.baseHref })
...(options.baseHref && { baseHref: options.baseHref }),
};

context.logger.info(
Expand All @@ -41,7 +45,7 @@ export default async function deploy(
{
target: 'build',
project: context.target.project,
configuration
configuration,
},
overrides as json.JsonObject
);
Expand All @@ -52,8 +56,17 @@ export default async function deploy(
}
}

const buildOptions = await context.getTargetOptions(
targetFromTargetString(buildTarget.name)
);
if (!buildOptions.outputPath || typeof buildOptions.outputPath !== 'string') {
throw new Error(
`Cannot read the output path option of the Angular project '${buildTarget.name}' in angular.json`
);
}

await engine.run(
projectRoot,
buildOptions.outputPath,
options,
(context.logger as unknown) as logging.LoggerApi
);
Expand Down
51 changes: 10 additions & 41 deletions src/deploy/builder.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,27 @@
import {
BuilderContext,
BuilderOutput,
createBuilder
createBuilder,
} from '@angular-devkit/architect';
import { asWindowsPath, experimental, normalize } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import os from 'os';
import * as path from 'path';

import * as engine from '../engine/engine';
import deploy from './actions';
import { Schema } from './schema';

// Call the createBuilder() function to create a builder. This mirrors
// createJobHandler() but add typings specific to Architect Builders.
export default createBuilder<any>(
export default createBuilder(
async (options: Schema, context: BuilderContext): Promise<BuilderOutput> => {
try {
// The project root is added to a BuilderContext.
const root = normalize(context.workspaceRoot);
const workspace = new experimental.workspace.Workspace(
root,
new NodeJsSyncHost()
);
await workspace
.loadWorkspaceFromHost(normalize('angular.json'))
.toPromise();

if (!context.target) {
throw new Error('Cannot deploy the application without a target');
}

const targets = workspace.getProjectTargets(context.target.project);

if (
!targets ||
!targets.build ||
!targets.build.options ||
!targets.build.options.outputPath
) {
throw new Error('Cannot find the project output directory');
}
if (!context.target) {
throw new Error('Cannot deploy the application without a target');
}

const isWin = os.platform() === 'win32';
const workspaceRoot = !isWin
? workspace.root
: asWindowsPath(workspace.root);
const buildTarget = {
name: `${context.target.project}:build:production`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will extend this to

const buildTarget = { name: options.buildTarget || `${context.target.project}:build:production` };

as seen here: angular/angularfire@7e1918a#diff-77ce940ab1bf28b9fd490f028b74d4a439968ace1f095f72216d64f51bed7c59R25

};

await deploy(
engine,
context,
path.join(workspaceRoot, targets.build.options.outputPath),
options
);
try {
await deploy(engine, context, buildTarget, options);
} catch (e) {
context.logger.error('❌ An error occurred when trying to deploy:');
context.logger.error(e.message);
Expand Down
18 changes: 18 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,21 @@ export interface GHPages {
publish(dir: string, options: any, callback: (error: any) => void);
clean?(): void;
}

export interface WorkspaceProject {
projectType?: string;
architect?: Record<
string,
{ builder: string; options?: Record<string, any> }
>;
}

export interface Workspace {
defaultProject?: string;
projects: Record<string, WorkspaceProject>;
}

export interface BuildTarget {
name: string;
options?: { [name: string]: any };
}
22 changes: 9 additions & 13 deletions src/ng-add.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
import { experimental, JsonParseMode, parseJson } from '@angular-devkit/core';
import { JsonParseMode, parseJson } from '@angular-devkit/core';
import {
SchematicContext,
SchematicsException,
Tree
Tree,
} from '@angular-devkit/schematics';
import { Workspace } from './interfaces';

function getWorkspace(
host: Tree
): { path: string; workspace: experimental.workspace.WorkspaceSchema } {
function getWorkspace(host: Tree): { path: string; workspace: Workspace } {
const possibleFiles = ['/angular.json', '/.angular.json'];
const path = possibleFiles.filter(path => host.exists(path))[0];
const path = possibleFiles.filter((path) => host.exists(path))[0];

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

let workspace: experimental.workspace.WorkspaceSchema;
let workspace: Workspace;
try {
workspace = (parseJson(
content,
JsonParseMode.Loose
) as {}) as experimental.workspace.WorkspaceSchema;
workspace = (parseJson(content, JsonParseMode.Loose) as {}) as Workspace;
} catch (e) {
throw new SchematicsException(`Could not parse angular.json: ` + e.message);
}

return {
path,
workspace
workspace,
};
}
interface NgAddOptions {
Expand Down Expand Up @@ -78,7 +74,7 @@ export const ngAdd = (options: NgAddOptions) => (

project.architect['deploy'] = {
builder: 'angular-cli-ghpages:deploy',
options: {}
options: {},
};

tree.overwrite(workspacePath, JSON.stringify(workspace, null, 2));
Expand Down
Loading