Skip to content

Commit 452a885

Browse files
committed
chore: migrate from configuration to buildTarget
- only unit tests are evaluated right now - no real tests were done yet
1 parent 17021ab commit 452a885

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

src/deploy/actions.spec.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ import {
66
Target
77
} from '@angular-devkit/architect/src';
88
import { JsonObject, logging } from '@angular-devkit/core';
9+
import { BuildTarget } from 'interfaces';
910

1011
import deploy from './actions';
1112

1213
let context: BuilderContext;
1314
const mockEngine = { run: (_: string, __: any, __2: any) => Promise.resolve() };
1415

1516
const PROJECT = 'pirojok-project';
17+
const BUILD_TARGET: BuildTarget = {
18+
name: `${PROJECT}:build:production`
19+
};
1620

1721
describe('Deploy Angular apps', () => {
1822
beforeEach(() => initMocks());
1923

2024
it('should invoke the builder', async () => {
2125
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
22-
await deploy(mockEngine, context, 'host', {});
26+
await deploy(mockEngine, context, BUILD_TARGET, {});
2327

2428
expect(spy).toHaveBeenCalledWith(
2529
{
@@ -33,7 +37,7 @@ describe('Deploy Angular apps', () => {
3337

3438
it('should invoke the builder with the baseHref', async () => {
3539
const spy = spyOn(context, 'scheduleTarget').and.callThrough();
36-
await deploy(mockEngine, context, 'host', { baseHref: '/folder' });
40+
await deploy(mockEngine, context, BUILD_TARGET, { baseHref: '/folder' });
3741

3842
expect(spy).toHaveBeenCalledWith(
3943
{
@@ -47,16 +51,16 @@ describe('Deploy Angular apps', () => {
4751

4852
it('should invoke engine.run', async () => {
4953
const spy = spyOn(mockEngine, 'run').and.callThrough();
50-
await deploy(mockEngine, context, 'host', {});
54+
await deploy(mockEngine, context, BUILD_TARGET, {});
5155

52-
expect(spy).toHaveBeenCalledWith('host', {}, context.logger);
56+
expect(spy).toHaveBeenCalledWith('dist/some-folder', {}, context.logger);
5357
});
5458

5559
describe('error handling', () => {
5660
it('throws if there is no target project', async () => {
5761
context.target = undefined;
5862
try {
59-
await deploy(mockEngine, context, 'host', {});
63+
await deploy(mockEngine, context, BUILD_TARGET, {});
6064
fail();
6165
} catch (e) {
6266
expect(e.message).toMatch(/Cannot execute the build target/);
@@ -73,7 +77,7 @@ describe('Deploy Angular apps', () => {
7377
result: Promise.resolve(createBuilderOutputMock(false))
7478
} as BuilderRun);
7579
try {
76-
await deploy(mockEngine, context, 'host', {});
80+
await deploy(mockEngine, context, BUILD_TARGET, {});
7781
fail();
7882
} catch (e) {
7983
expect(e.message).toEqual('Error while building the app.');
@@ -102,7 +106,10 @@ const initMocks = () => {
102106
validateOptions: _ => Promise.resolve({} as any),
103107
getBuilderNameForTarget: () => Promise.resolve(''),
104108
analytics: null as any,
105-
getTargetOptions: (_: Target) => Promise.resolve({}),
109+
getTargetOptions: (_: Target) =>
110+
Promise.resolve({
111+
outputPath: 'dist/some-folder'
112+
}),
106113
reportProgress: (_: number, __?: number, ___?: string) => {},
107114
reportStatus: (_: string) => {},
108115
reportRunning: () => {},

src/deploy/actions.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
BuilderContext,
3-
targetFromTargetString,
3+
targetFromTargetString
44
} from '@angular-devkit/architect';
55
import { json, logging } from '@angular-devkit/core';
66

@@ -19,35 +19,32 @@ export default async function deploy(
1919
buildTarget: BuildTarget,
2020
options: Schema
2121
) {
22+
// 1. BUILD
2223
if (options.noBuild) {
2324
context.logger.info(`📦 Skipping build`);
2425
} else {
2526
if (!context.target) {
2627
throw new Error('Cannot execute the build target');
2728
}
2829

29-
const configuration = options.configuration
30-
? options.configuration
31-
: 'production';
30+
// baseHref (from @angular-devkit/build-angular:browser)
31+
// can be overriden here directly from the deployment builder options,
32+
// since this feature is the most important switch when deploying the github
3233
const overrides = {
33-
...(options.baseHref && { baseHref: options.baseHref }),
34+
...(options.baseHref && { baseHref: options.baseHref })
3435
};
3536

36-
context.logger.info(
37-
`📦 Building "${
38-
context.target.project
39-
}". Configuration: "${configuration}".${
40-
options.baseHref ? ' Your base-href: "' + options.baseHref + '"' : ''
41-
}`
42-
);
37+
context.logger.info(`📦 Building "${context.target.project}"`);
38+
context.logger.info(`📦 Build target "${buildTarget.name}"`);
39+
40+
// options.baseHref ? ' Your base-href: "' + options.baseHref + '"' : ''
4341

4442
const build = await context.scheduleTarget(
43+
targetFromTargetString(buildTarget.name),
4544
{
46-
target: 'build',
47-
project: context.target.project,
48-
configuration,
49-
},
50-
overrides as json.JsonObject
45+
...buildTarget.options,
46+
...overrides
47+
}
5148
);
5249
const buildResult = await build.result;
5350

@@ -56,6 +53,7 @@ export default async function deploy(
5653
}
5754
}
5855

56+
// 2. DEPLOYMENT
5957
const buildOptions = await context.getTargetOptions(
6058
targetFromTargetString(buildTarget.name)
6159
);

src/deploy/builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
BuilderContext,
33
BuilderOutput,
4-
createBuilder,
4+
createBuilder
55
} from '@angular-devkit/architect';
66

77
import * as engine from '../engine/engine';
@@ -17,7 +17,7 @@ export default createBuilder(
1717
}
1818

1919
const buildTarget = {
20-
name: `${context.target.project}:build:production`,
20+
name: options.buildTarget || `${context.target.project}:build:production`
2121
};
2222

2323
try {

0 commit comments

Comments
 (0)