Skip to content

Commit d9166b0

Browse files
mrmekualexeagle
authored andcommitted
feat(@angular/cli): Add VsCode extension recommendations
1 parent a139243 commit d9166b0

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

packages/schematics/angular/migrations/migration-collection.json

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"version": "7.0.3",
3030
"factory": "./update-7/index#updateDevkitBuildNgPackagr",
3131
"description": "Update an Angular CLI project to version 7."
32+
},
33+
"migration-07": {
34+
"version": "8.0.0",
35+
"factory": "./update-vscode-recommendations/update-8.0.0",
36+
"description": "Update an Angular CLI project to version 8.0.0"
3237
}
3338
}
3439
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { Rule, Tree } from '@angular-devkit/schematics';
9+
10+
export function readJsonInTree<T>(host: Tree, path: string): T {
11+
if (!host.exists(path)) {
12+
throw new Error(`Cannot find ${path}`);
13+
}
14+
15+
const json = host.read(path);
16+
if (!json) {
17+
throw new Error(`Cannot read ${path}`);
18+
}
19+
20+
return JSON.parse(json.toString('utf-8'));
21+
}
22+
23+
export function serializeJson<T>(json: T): string {
24+
return `${JSON.stringify(json, null, 2)}\n`;
25+
}
26+
27+
export function updateJsonInTree<T, O = T>(
28+
path: string,
29+
callback: (json: T) => O,
30+
): Rule {
31+
return (host: Tree): Tree => {
32+
if (!host.exists(path)) {
33+
host.create(path, serializeJson(callback({} as T)));
34+
35+
return host;
36+
}
37+
host.overwrite(path, serializeJson(callback(readJsonInTree(host, path))));
38+
39+
return host;
40+
};
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { Rule, chain } from '@angular-devkit/schematics';
9+
import { updateJsonInTree } from './ast-utils';
10+
11+
export default function (): Rule {
12+
return chain([addExtensionRecommendations]);
13+
}
14+
15+
const addExtensionRecommendations = updateJsonInTree(
16+
'.vscode/extensions.json',
17+
(json: { recommendations?: string[] }) => {
18+
['angular.ng-template', 'ms-vscode.vscode-typescript-tslint-plugin'].forEach(extension => {
19+
json.recommendations = json.recommendations || [];
20+
if (!json.recommendations.includes(extension)) {
21+
json.recommendations.push(extension);
22+
}
23+
});
24+
25+
return json;
26+
},
27+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { Tree } from '@angular-devkit/schematics';
9+
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
10+
import { readJsonInTree, serializeJson, updateJsonInTree } from './ast-utils';
11+
12+
describe('Update 8.0.0', () => {
13+
let initialTree: Tree;
14+
let schematicRunner: SchematicTestRunner;
15+
16+
beforeEach(() => {
17+
initialTree = Tree.empty();
18+
19+
initialTree.create(
20+
'package.json',
21+
serializeJson({
22+
devDependencies: { '@angular/cli': '7.1.0', typescript: '~3.1.0' },
23+
}),
24+
);
25+
26+
schematicRunner = new SchematicTestRunner(
27+
'migrations',
28+
require.resolve('../migration-collection.json'),
29+
);
30+
});
31+
32+
it('should add vscode extension recommendations', async () => {
33+
const result = await schematicRunner
34+
.runSchematicAsync('migration-07', {}, initialTree)
35+
.toPromise();
36+
37+
expect(readJsonInTree(result, '.vscode/extensions.json')).toEqual({
38+
recommendations: ['angular.ng-template', 'ms-vscode.vscode-typescript-tslint-plugin'],
39+
});
40+
});
41+
42+
it('should add to existing vscode extension recommendations', async () => {
43+
initialTree = await schematicRunner
44+
.callRule(
45+
updateJsonInTree('.vscode/extensions.json', () => ({
46+
recommendations: [
47+
'eamodio.gitlens', 'angular.ng-template', 'ms-vscode.vscode-typescript-tslint-plugin'],
48+
})),
49+
initialTree,
50+
)
51+
.toPromise();
52+
53+
const result = await schematicRunner
54+
.runSchematicAsync('migration-07', {}, initialTree)
55+
.toPromise();
56+
57+
expect(readJsonInTree(result, '.vscode/extensions.json')).toEqual({
58+
recommendations: [
59+
'eamodio.gitlens', 'angular.ng-template', 'ms-vscode.vscode-typescript-tslint-plugin'],
60+
});
61+
});
62+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"angular.ng-template",
4+
"ms-vscode.vscode-typescript-tslint-plugin
5+
]
6+
}

0 commit comments

Comments
 (0)