|
1 |
| -import {CliConfig} from '../models/config'; |
| 1 | +import * as fs from 'fs'; |
| 2 | +import { CliConfig } from '../models/config'; |
| 3 | +import { oneLine } from 'common-tags'; |
2 | 4 |
|
3 | 5 | const SilentError = require('silent-error');
|
| 6 | +const chalk = require('chalk'); |
4 | 7 | const Command = require('../ember-cli/lib/models/command');
|
5 | 8 |
|
6 | 9 |
|
@@ -67,13 +70,42 @@ const SetCommand = Command.extend({
|
67 | 70 | default: value = parseValue(rawValue, jsonPath);
|
68 | 71 | }
|
69 | 72 |
|
70 |
| - config.set(jsonPath, value); |
71 |
| - config.save(); |
| 73 | + if (jsonPath.indexOf('prefix') > 0) { |
| 74 | + // update tslint if prefix is updated |
| 75 | + updateLintForPrefix(this.project.root + '/tslint.json', value); |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + config.set(jsonPath, value); |
| 80 | + config.save(); |
| 81 | + } catch (error) { |
| 82 | + throw new SilentError(error.message); |
| 83 | + } |
72 | 84 | resolve();
|
73 | 85 | });
|
74 | 86 | }
|
75 | 87 | });
|
76 | 88 |
|
| 89 | +function updateLintForPrefix(filePath: string, prefix: string): void { |
| 90 | + const tsLint = JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 91 | + const componentLint = tsLint.rules['component-selector'][2]; |
| 92 | + if (componentLint instanceof Array) { |
| 93 | + tsLint.rules['component-selector'][2].push(prefix); |
| 94 | + } else { |
| 95 | + tsLint.rules['component-selector'][2] = prefix; |
| 96 | + } |
| 97 | + |
| 98 | + const directiveLint = tsLint.rules['directive-selector'][2]; |
| 99 | + if (directiveLint instanceof Array) { |
| 100 | + tsLint.rules['directive-selector'][2].push(prefix); |
| 101 | + } else { |
| 102 | + tsLint.rules['directive-selector'][2] = prefix; |
| 103 | + } |
| 104 | + fs.writeFileSync(filePath, JSON.stringify(tsLint, null, 2)); |
| 105 | + console.log(chalk.yellow(oneLine`we have updated tslint to match prefix, |
| 106 | + you may want to fix linting errors.`)); |
| 107 | +} |
| 108 | + |
77 | 109 | function parseValue(rawValue: string, path: string) {
|
78 | 110 | try {
|
79 | 111 | return JSON.parse(rawValue);
|
|
0 commit comments