Skip to content

Commit 59dffc4

Browse files
sumitaroradond2clouds
authored andcommitted
feat(@angular/cli): update tslint on updating prefix (angular#5908)
Fixes angular#5794
1 parent 6c98432 commit 59dffc4

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

packages/@angular/cli/blueprints/component/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as chalk from 'chalk';
22
import * as fs from 'fs';
33
import * as path from 'path';
4+
import { oneLine } from 'common-tags';
45
import { NodeHost } from '../../lib/ast-tools';
56
import { CliConfig } from '../../models/config';
67
import { getAppFromConfig } from '../../utilities/app-utils';
@@ -218,6 +219,12 @@ export default Blueprint.extend({
218219
},
219220

220221
afterInstall: function (options: any) {
222+
const appConfig = getAppFromConfig(this.options.app);
223+
if (options.prefix && appConfig.prefix && appConfig.prefix !== options.prefix) {
224+
console.log(chalk.yellow(oneLine`You are using different prefix from app,
225+
you might get lint errors. Please update "tslint.json" accordingly.`));
226+
}
227+
221228
const returns: Array<any> = [];
222229
const className = stringUtils.classify(`${options.entity.name}Component`);
223230
const fileName = stringUtils.dasherize(`${options.entity.name}.component`);

packages/@angular/cli/blueprints/directive/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as chalk from 'chalk';
22
import * as path from 'path';
3+
import { oneLine } from 'common-tags';
34
import { NodeHost } from '../../lib/ast-tools';
45
import { CliConfig } from '../../models/config';
56
import { getAppFromConfig } from '../../utilities/app-utils';
@@ -130,6 +131,12 @@ export default Blueprint.extend({
130131
},
131132

132133
afterInstall: function (options: any) {
134+
const appConfig = getAppFromConfig(this.options.app);
135+
if (options.prefix && appConfig.prefix && appConfig.prefix !== options.prefix) {
136+
console.log(chalk.yellow(oneLine`You are using different prefix from app,
137+
you might get lint errors. Please update "tslint.json" accordingly.`));
138+
}
139+
133140
const returns: Array<any> = [];
134141
const className = stringUtils.classify(`${options.entity.name}Directive`);
135142
const fileName = stringUtils.dasherize(`${options.entity.name}.directive`);

packages/@angular/cli/commands/set.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import {CliConfig} from '../models/config';
1+
import * as fs from 'fs';
2+
import { CliConfig } from '../models/config';
3+
import { oneLine } from 'common-tags';
24

35
const SilentError = require('silent-error');
6+
const chalk = require('chalk');
47
const Command = require('../ember-cli/lib/models/command');
58

6-
79
export interface SetOptions {
810
global?: boolean;
911
}
@@ -67,13 +69,38 @@ const SetCommand = Command.extend({
6769
default: value = parseValue(rawValue, jsonPath);
6870
}
6971

72+
if (jsonPath.indexOf('prefix') > 0) {
73+
// update tslint if prefix is updated
74+
updateLintForPrefix(this.project.root + '/tslint.json', value);
75+
}
76+
7077
config.set(jsonPath, value);
7178
config.save();
7279
resolve();
7380
});
7481
}
7582
});
7683

84+
function updateLintForPrefix(filePath: string, prefix: string): void {
85+
const tsLint = JSON.parse(fs.readFileSync(filePath, 'utf8'));
86+
const componentLint = tsLint.rules['component-selector'][2];
87+
if (componentLint instanceof Array) {
88+
tsLint.rules['component-selector'][2].push(prefix);
89+
} else {
90+
tsLint.rules['component-selector'][2] = prefix;
91+
}
92+
93+
const directiveLint = tsLint.rules['directive-selector'][2];
94+
if (directiveLint instanceof Array) {
95+
tsLint.rules['directive-selector'][2].push(prefix);
96+
} else {
97+
tsLint.rules['directive-selector'][2] = prefix;
98+
}
99+
fs.writeFileSync(filePath, JSON.stringify(tsLint, null, 2));
100+
console.log(chalk.yellow(oneLine`we have updated tslint to match prefix,
101+
you may want to fix linting errors.`));
102+
}
103+
77104
function parseValue(rawValue: string, path: string) {
78105
try {
79106
return JSON.parse(rawValue);
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {ng} from '../../../utils/process';
2+
import {expectToFail} from '../../../utils/utils';
3+
import * as fs from 'fs';
4+
5+
export default function() {
6+
return Promise.resolve()
7+
.then(() => expectToFail(() => ng('set', 'apps.zzz.prefix')))
8+
.then(() => ng('set', 'apps.0.prefix' , 'new-prefix'))
9+
.then(() => ng('get', 'apps.0.prefix'))
10+
.then(({ stdout }) => {
11+
if (!stdout.match(/new-prefix/)) {
12+
throw new Error(`Expected "new-prefix", received "${JSON.stringify(stdout)}".`);
13+
}
14+
})
15+
.then(() => {
16+
const tsLint = JSON.parse(fs.readFileSync(process.cwd() + '/tslint.json', 'utf8'));
17+
if (tsLint.rules['component-selector'][2] !== 'new-prefix') {
18+
throw new Error(`Expected "new-prefix" Found: ${tsLint.rules['component-selector'][2]}.`);
19+
}
20+
});
21+
}

0 commit comments

Comments
 (0)