Skip to content

Commit aa7ea02

Browse files
feat: add support for Angular v13 by @fmalcher (v1.0.0-rc.3)
* fix: remove deprecated JSON parser, use workspace tools * v1.0.0-rc.3 fixes #138 fixes #137 Co-authored-by: Johannes Hoppe <[email protected]>
1 parent 98f291e commit aa7ea02

File tree

7 files changed

+157
-150
lines changed

7 files changed

+157
-150
lines changed

src/__snapshots__/ng-add.spec.ts.snap

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ng-add generating files generates new files if starting from scratch 1`] = `
4+
"{
5+
\\"version\\": 1,
6+
\\"defaultProject\\": \\"THEPROJECT\\",
7+
\\"projects\\": {
8+
\\"THEPROJECT\\": {
9+
\\"projectType\\": \\"application\\",
10+
\\"root\\": \\"PROJECTROOT\\",
11+
\\"architect\\": {
12+
\\"build\\": {
13+
\\"options\\": {
14+
\\"outputPath\\": \\"dist/THEPROJECT\\"
15+
}
16+
},
17+
\\"deploy\\": {
18+
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
19+
}
20+
}
21+
},
22+
\\"OTHERPROJECT\\": {
23+
\\"projectType\\": \\"application\\",
24+
\\"root\\": \\"PROJECTROOT\\",
25+
\\"architect\\": {
26+
\\"build\\": {
27+
\\"options\\": {
28+
\\"outputPath\\": \\"dist/OTHERPROJECT\\"
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}"
35+
`;
36+
37+
exports[`ng-add generating files overrides existing files 1`] = `
38+
"{
39+
\\"version\\": 1,
40+
\\"defaultProject\\": \\"THEPROJECT\\",
41+
\\"projects\\": {
42+
\\"THEPROJECT\\": {
43+
\\"projectType\\": \\"application\\",
44+
\\"root\\": \\"PROJECTROOT\\",
45+
\\"architect\\": {
46+
\\"build\\": {
47+
\\"options\\": {
48+
\\"outputPath\\": \\"dist/THEPROJECT\\"
49+
}
50+
},
51+
\\"deploy\\": {
52+
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
53+
}
54+
}
55+
},
56+
\\"OTHERPROJECT\\": {
57+
\\"projectType\\": \\"application\\",
58+
\\"root\\": \\"PROJECTROOT\\",
59+
\\"architect\\": {
60+
\\"build\\": {
61+
\\"options\\": {
62+
\\"outputPath\\": \\"dist/OTHERPROJECT\\"
63+
}
64+
},
65+
\\"deploy\\": {
66+
\\"builder\\": \\"angular-cli-ghpages:deploy\\"
67+
}
68+
}
69+
}
70+
}
71+
}"
72+
`;

src/deploy/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
BuilderContext,
33
targetFromTargetString
44
} from '@angular-devkit/architect';
5-
import { json, logging } from '@angular-devkit/core';
5+
import { logging } from '@angular-devkit/core';
66

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

src/ng-add.spec.ts

Lines changed: 39 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { SchematicContext, Tree } from '@angular-devkit/schematics';
22

33
import { ngAdd } from './ng-add';
44

5-
const PROJECT_NAME = 'pie-ka-chu';
6-
const PROJECT_ROOT = 'pirojok';
7-
8-
const OTHER_PROJECT_NAME = 'pi-catch-you';
5+
const PROJECT_NAME = 'THEPROJECT';
6+
const PROJECT_ROOT = 'PROJECTROOT';
7+
const OTHER_PROJECT_NAME = 'OTHERPROJECT';
98

109
describe('ng-add', () => {
1110
describe('generating files', () => {
@@ -17,118 +16,124 @@ describe('ng-add', () => {
1716
});
1817

1918
it('generates new files if starting from scratch', async () => {
20-
const result = ngAdd({
19+
const result = await ngAdd({
2120
project: PROJECT_NAME
2221
})(tree, {} as SchematicContext);
2322

24-
expect(result.read('angular.json')!.toString()).toEqual(
25-
initialAngularJson
26-
);
23+
const actual = result.read('angular.json')!.toString();
24+
expect(prettifyJSON(actual)).toMatchSnapshot();
2725
});
2826

2927
it('overrides existing files', async () => {
30-
const tempTree = ngAdd({
28+
const tempTree = await ngAdd({
3129
project: PROJECT_NAME
3230
})(tree, {} as SchematicContext);
3331

34-
const result = ngAdd({
32+
const result = await ngAdd({
3533
project: OTHER_PROJECT_NAME
3634
})(tempTree, {} as SchematicContext);
3735

3836
const actual = result.read('angular.json')!.toString();
3937

40-
expect(actual).toEqual(overwriteAngularJson);
38+
expect(prettifyJSON(actual)).toMatchSnapshot();
4139
});
4240
});
4341

4442
describe('error handling', () => {
45-
it('fails if project not defined', () => {
43+
it('should fail if project not defined', async () => {
4644
const tree = Tree.empty();
4745
const angularJSON = generateAngularJson();
4846
delete angularJSON.defaultProject;
4947
tree.create('angular.json', JSON.stringify(angularJSON));
5048

51-
expect(() =>
49+
await expect(
5250
ngAdd({
5351
project: ''
5452
})(tree, {} as SchematicContext)
55-
).toThrowError(
53+
).rejects.toThrowError(
5654
'No Angular project selected and no default project in the workspace'
5755
);
5856
});
5957

60-
it('Should throw if angular.json not found', async () => {
61-
expect(() =>
58+
it('should throw if angular.json not found', async () => {
59+
await expect(
6260
ngAdd({
6361
project: PROJECT_NAME
6462
})(Tree.empty(), {} as SchematicContext)
65-
).toThrowError('Could not find angular.json');
63+
).rejects.toThrowError('Unable to determine format for workspace path.');
6664
});
6765

68-
it('Should throw if angular.json can not be parsed', async () => {
66+
it('should throw if angular.json can not be parsed', async () => {
6967
const tree = Tree.empty();
7068
tree.create('angular.json', 'hi');
7169

72-
expect(() =>
70+
await expect(
7371
ngAdd({
7472
project: PROJECT_NAME
7573
})(tree, {} as SchematicContext)
76-
).toThrowError('Could not parse angular.json');
74+
).rejects.toThrowError('Invalid JSON character: "h" at 0:0.');
7775
});
7876

79-
it('Should throw if specified project does not exist ', async () => {
77+
it('should throw if specified project does not exist', async () => {
8078
const tree = Tree.empty();
81-
tree.create('angular.json', JSON.stringify({ projects: {} }));
79+
tree.create('angular.json', JSON.stringify({ version: 1, projects: {} }));
8280

83-
expect(() =>
81+
await expect(
8482
ngAdd({
8583
project: PROJECT_NAME
8684
})(tree, {} as SchematicContext)
87-
).toThrowError(
85+
).rejects.toThrowError(
8886
'The specified Angular project is not defined in this workspace'
8987
);
9088
});
9189

92-
it('Should throw if specified project is not application', async () => {
90+
it('should throw if specified project is not application', async () => {
9391
const tree = Tree.empty();
9492
tree.create(
9593
'angular.json',
9694
JSON.stringify({
97-
projects: { [PROJECT_NAME]: { projectType: 'pokemon' } }
95+
version: 1,
96+
projects: { [PROJECT_NAME]: { projectType: 'invalid' } }
9897
})
9998
);
10099

101-
expect(() =>
100+
await expect(
102101
ngAdd({
103102
project: PROJECT_NAME
104103
})(tree, {} as SchematicContext)
105-
).toThrowError(
104+
).rejects.toThrowError(
106105
'Deploy requires an Angular project type of "application" in angular.json'
107106
);
108107
});
109108

110-
it('Should throw if app does not have architect configured', async () => {
109+
it('should throw if app does not have architect configured', async () => {
111110
const tree = Tree.empty();
112111
tree.create(
113112
'angular.json',
114113
JSON.stringify({
114+
version: 1,
115115
projects: { [PROJECT_NAME]: { projectType: 'application' } }
116116
})
117117
);
118118

119-
expect(() =>
119+
await expect(
120120
ngAdd({
121121
project: PROJECT_NAME
122122
})(tree, {} as SchematicContext)
123-
).toThrowError(
124-
'Cannot read the output path (architect.build.options.outputPath) of the Angular project "pie-ka-chu" in angular.json'
123+
).rejects.toThrowError(
124+
'Cannot read the output path (architect.build.options.outputPath) of the Angular project "THEPROJECT" in angular.json'
125125
);
126126
});
127127
});
128128
});
129129

130+
function prettifyJSON(json: string) {
131+
return JSON.stringify(JSON.parse(json), null, 2);
132+
}
133+
130134
function generateAngularJson() {
131135
return {
136+
version: 1,
132137
defaultProject: PROJECT_NAME as string | undefined,
133138
projects: {
134139
[PROJECT_NAME]: {
@@ -137,7 +142,7 @@ function generateAngularJson() {
137142
architect: {
138143
build: {
139144
options: {
140-
outputPath: 'dist/ikachu'
145+
outputPath: 'dist/' + PROJECT_NAME
141146
}
142147
}
143148
}
@@ -148,79 +153,11 @@ function generateAngularJson() {
148153
architect: {
149154
build: {
150155
options: {
151-
outputPath: 'dist/ikachu'
156+
outputPath: 'dist/' + OTHER_PROJECT_NAME
152157
}
153158
}
154159
}
155160
}
156161
}
157162
};
158163
}
159-
160-
const initialAngularJson = `{
161-
"defaultProject": "pie-ka-chu",
162-
"projects": {
163-
"pie-ka-chu": {
164-
"projectType": "application",
165-
"root": "pirojok",
166-
"architect": {
167-
"build": {
168-
"options": {
169-
"outputPath": "dist/ikachu"
170-
}
171-
},
172-
"deploy": {
173-
\"builder\": \"angular-cli-ghpages:deploy\",
174-
"options": {}
175-
}
176-
}
177-
},
178-
"pi-catch-you": {
179-
"projectType": "application",
180-
"root": "pirojok",
181-
"architect": {
182-
"build": {
183-
"options": {
184-
"outputPath": "dist/ikachu"
185-
}
186-
}
187-
}
188-
}
189-
}
190-
}`;
191-
192-
const overwriteAngularJson = `{
193-
"defaultProject": "pie-ka-chu",
194-
"projects": {
195-
"pie-ka-chu": {
196-
"projectType": "application",
197-
"root": "pirojok",
198-
"architect": {
199-
"build": {
200-
"options": {
201-
"outputPath": "dist/ikachu"
202-
}
203-
},
204-
"deploy": {
205-
\"builder\": \"angular-cli-ghpages:deploy\",
206-
"options": {}
207-
}
208-
}
209-
},
210-
"pi-catch-you": {
211-
"projectType": "application",
212-
"root": "pirojok",
213-
"architect": {
214-
"build": {
215-
"options": {
216-
"outputPath": "dist/ikachu"
217-
}
218-
},
219-
"deploy": {
220-
"builder": "angular-cli-ghpages:deploy",
221-
"options": {}
222-
}
223-
}
224-
}
225-
}
226-
}`;

0 commit comments

Comments
 (0)