/
run-prettier.ts
43 lines (41 loc) · 1.19 KB
/
run-prettier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import chalk from "chalk";
import * as fs from "fs";
import * as prettier from "prettier";
/**
*
* Runs prettier and later prints the output configuration
*
* @param {String} outputPath - Path to write the config to
* @param {Node} source - AST to write at the given path
* @param {Function} cb - executes a callback after execution if supplied
* @returns {Void} Writes a file at given location and prints messages accordingly
*/
export default function runPrettier(outputPath: string, source: string, cb?: Function): void {
function validateConfig(): void | Function {
let prettySource: string;
let error: object;
try {
prettySource = prettier.format(source, {
filepath: outputPath,
parser: "babel",
singleQuote: true,
tabWidth: 1,
useTabs: true,
});
} catch (err) {
process.stdout.write(
`\n${chalk.yellow(
`WARNING: Could not apply prettier to ${outputPath}` +
" due validation error, but the file has been created\n",
)}`,
);
prettySource = source;
error = err;
}
if (cb) {
return cb(error);
}
return fs.writeFileSync(outputPath, prettySource, "utf8");
}
return fs.writeFile(outputPath, source, "utf8", validateConfig);
}