mirror of
https://github.com/zoriya/flood.git
synced 2026-05-27 09:01:58 +00:00
prettier: simply use prettier CLI
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user