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 { path, content } = file process.stdout.write('\b'.repeat(10000)) process.stdout.write(chalk.grey(`INFO: check ${path}`)) const valid = prettier.check(content, { filepath: path }) file.check = valid if (!valid && !program.lint) { print(chalk.red(`ERROR: ${path} lint check fails`)) 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; } });