Skip to content

Commit 8147d8e

Browse files
committed
feat(@angular/cli): update tslint on updating prefix
1 parent 2ce61f2 commit 8147d8e

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

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)