2021-04-26 15:45:02 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {join} from 'path';
|
2021-07-12 17:58:35 -04:00
|
|
|
import {exec} from 'shelljs';
|
2021-04-26 15:45:02 -04:00
|
|
|
|
|
|
|
import {error} from '../../utils/console';
|
|
|
|
|
|
|
|
import {Formatter} from './base-formatter';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formatter for running prettier against Typescript and Javascript files.
|
|
|
|
*/
|
|
|
|
export class Prettier extends Formatter {
|
2021-07-07 13:58:22 -04:00
|
|
|
override name = 'prettier';
|
2021-04-26 15:45:02 -04:00
|
|
|
|
2021-07-07 13:58:22 -04:00
|
|
|
override binaryFilePath = join(this.git.baseDir, 'node_modules/.bin/prettier');
|
2021-04-26 15:45:02 -04:00
|
|
|
|
2021-07-07 13:58:22 -04:00
|
|
|
override defaultFileMatcher = ['**/*.{t,j}s'];
|
2021-04-26 15:45:02 -04:00
|
|
|
|
|
|
|
/**
|
2021-04-28 10:10:24 -04:00
|
|
|
* The configuration path of the prettier config, obtained during construction to prevent needing
|
2021-04-26 15:45:02 -04:00
|
|
|
* to discover it repeatedly for each execution.
|
|
|
|
*/
|
2021-07-12 17:58:35 -04:00
|
|
|
private configPath =
|
|
|
|
this.config['prettier'] ? exec(`${this.binaryFilePath} --find-config-path .`).trim() : '';
|
2021-04-26 15:45:02 -04:00
|
|
|
|
2021-07-07 13:58:22 -04:00
|
|
|
override actions = {
|
2021-04-26 15:45:02 -04:00
|
|
|
check: {
|
|
|
|
commandFlags: `--config ${this.configPath} --check`,
|
|
|
|
callback:
|
2021-07-12 17:58:35 -04:00
|
|
|
(_: string, code: number, stdout: string) => {
|
2021-04-26 15:45:02 -04:00
|
|
|
return code !== 0;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
format: {
|
|
|
|
commandFlags: `--config ${this.configPath} --write`,
|
|
|
|
callback:
|
2021-07-12 17:58:35 -04:00
|
|
|
(file: string, code: number, _: string, stderr: string) => {
|
2021-04-26 15:45:02 -04:00
|
|
|
if (code !== 0) {
|
|
|
|
error(`Error running prettier on: ${file}`);
|
|
|
|
error(stderr);
|
|
|
|
error();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|