Skip to content

Commit

Permalink
feat(@angular/cli): update tslint on updating prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitarora committed Apr 11, 2017
1 parent 2ce61f2 commit 8147d8e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/@angular/cli/commands/set.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {CliConfig} from '../models/config';
import * as fs from 'fs';
import { CliConfig } from '../models/config';
import { oneLine } from 'common-tags';

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


export interface SetOptions {
global?: boolean;
}
Expand Down Expand Up @@ -67,13 +69,38 @@ const SetCommand = Command.extend({
default: value = parseValue(rawValue, jsonPath);
}

if (jsonPath.indexOf('prefix') > 0) {
// update tslint if prefix is updated
updateLintForPrefix(this.project.root + '/tslint.json', value);
}

config.set(jsonPath, value);
config.save();
resolve();
});
}
});

function updateLintForPrefix(filePath: string, prefix: string): void {
const tsLint = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const componentLint = tsLint.rules['component-selector'][2];
if (componentLint instanceof Array) {
tsLint.rules['component-selector'][2].push(prefix);
} else {
tsLint.rules['component-selector'][2] = prefix;
}

const directiveLint = tsLint.rules['directive-selector'][2];
if (directiveLint instanceof Array) {
tsLint.rules['directive-selector'][2].push(prefix);
} else {
tsLint.rules['directive-selector'][2] = prefix;
}
fs.writeFileSync(filePath, JSON.stringify(tsLint, null, 2));
console.log(chalk.yellow(oneLine`we have updated tslint to match prefix,
you may want to fix linting errors.`));
}

function parseValue(rawValue: string, path: string) {
try {
return JSON.parse(rawValue);
Expand Down
21 changes: 21 additions & 0 deletions tests/e2e/tests/commands/set/set-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';
import * as fs from 'fs';

export default function() {
return Promise.resolve()
.then(() => expectToFail(() => ng('set', 'apps.zzz.prefix')))
.then(() => ng('set', 'apps.0.prefix' , 'new-prefix'))
.then(() => ng('get', 'apps.0.prefix'))
.then(({ stdout }) => {
if (!stdout.match(/new-prefix/)) {
throw new Error(`Expected "new-prefix", received "${JSON.stringify(stdout)}".`);
}
})
.then(() => {
const tsLint = JSON.parse(fs.readFileSync(process.cwd() + '/tslint.json', 'utf8'));
if (tsLint.rules['component-selector'][2] !== 'new-prefix') {
throw new Error(`Expected "new-prefix" Found: ${tsLint.rules['component-selector'][2]}.`);
}
});
}

0 comments on commit 8147d8e

Please sign in to comment.