prettier: simply use prettier CLI

This commit is contained in:
Jesse Chan
2020-09-25 00:20:55 +08:00
parent ae4629fd85
commit 6b42be395a
7 changed files with 45 additions and 103 deletions
-63
View File
@@ -1,11 +1,6 @@
const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const prettier = require('prettier');
const SOURCE_PATTERN = `{client,scripts,server,shared}${path.sep}!(assets){${path.sep},}{**${path.sep}*,*}.{js,jsx,ts,tsx,json,md,css,scss,sass,less}`;
const readFile = (filePath) => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (error, fileContent) => {
@@ -43,64 +38,6 @@ const formatFile = async (inputFilePath, outputFilePath) => {
return writtenFilePath;
};
const getSourceFilePaths = () => {
return new Promise((resolve, reject) => {
glob(SOURCE_PATTERN, (error, files) => {
if (error) {
reject(error);
return;
}
resolve(files.map((filePath) => path.join(process.cwd(), filePath)));
});
});
};
const formatSource = async () => {
console.log(chalk.reset('Formatting source files...'));
try {
const sourceFilePaths = await getSourceFilePaths();
const formattedPaths = await Promise.all(sourceFilePaths.map((filePath) => formatFile(filePath, filePath)));
console.log(chalk.green(`Formatted ${formattedPaths.length} files.`));
} catch (error) {
console.log(chalk.red('Problem formatting file:\n'), chalk.reset(error));
process.exit(1);
}
};
const check = async () => {
console.log(chalk.reset('Validating source file formatting...'));
try {
const sourceFilePaths = await getSourceFilePaths();
await Promise.all(
sourceFilePaths.map(async (filePath) => {
const fileContent = await readFile(filePath);
const prettierConfig = await prettier.resolveConfig(filePath);
const isCompliant = prettier.check(fileContent, {...prettierConfig, filepath: filePath});
if (!isCompliant) {
throw filePath;
}
}),
);
console.log(chalk.green('Finished validating file formatting.'));
} catch (error) {
console.log(chalk.red('Unformatted file found:\n'), chalk.reset(error));
process.exit(1);
}
};
const commands = {check, formatSource};
const desiredCommand = process.argv.slice(2)[0];
if (commands[desiredCommand] != null) {
commands[desiredCommand]();
}
module.exports = {
formatFile,
};