const formatFile = async (inputFilePath, outputFilePath) => { const fileContent = await readFile(inputFilePath); const prettierConfig = await prettier.resolveConfig(inputFilePath); const writtenFilePath = await writeFile( outputFilePath, prettier.format(fileContent, {...prettierConfig, filepath: inputFilePath}), ); return writtenFilePath; }
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); } }
files.forEach(file => { const options = prettier.resolveConfig.sync(file, { config: prettierConfigPath, }); try { const fileInfo = prettier.getFileInfo.sync(file); if (fileInfo.ignored) { return; } const input = fs.readFileSync(file, 'utf8'); const withParserOptions = { ...options, parser: fileInfo.inferredParser, }; const isPrettier = prettier.check(input, withParserOptions); if (!isPrettier) { console.log(`\x1b[31m ${file} is no prettier, please use npm run prettier and git add !`); didWarn = true; } } catch (e) { didError = true; } });
files.forEach(file => { Promise.all([ prettier.resolveConfig(file, { config: prettierConfigPath, }),
files.forEach(file => { const options = prettier.resolveConfig.sync(file, { config: prettierConfigPath, }); const fileInfo = prettier.getFileInfo.sync(file); if (fileInfo.ignored) { return; } try { const input = fs.readFileSync(file, 'utf8'); const withParserOptions = { ...options, parser: fileInfo.inferredParser, }; const output = prettier.format(input, withParserOptions); if (output !== input) { fs.writeFileSync(file, output, 'utf8'); console.log(chalk.green(`${file} is prettier`)); } } catch (e) { didError = true; } });
files.forEach(file => { const options = prettier.resolveConfig.sync(file, { config: prettierConfigPath, }); const fileInfo = prettier.getFileInfo.sync(file); if (fileInfo.ignored) { return; } try { const input = fs.readFileSync(file, 'utf8'); const withParserOptions = { ...options, parser: fileInfo.inferredParser, }; const output = prettier.format(input, withParserOptions); if (output !== input) { fs.writeFileSync(file, output, 'utf8'); console.log(`\x1b[34m ${file} is prettier`); } } catch (e) { didError = true; } });