|
| 1 | +#!/usr/bin/env node |
| 2 | +import chalk from 'chalk'; |
| 3 | +import { spawn } from 'child_process'; |
| 4 | +import fs from 'fs/promises'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +const debug = process.argv.includes('--debug'); |
| 8 | + |
| 9 | +const logDebug = (message) => { |
| 10 | + if (debug) { |
| 11 | + console.log(`[${chalk.blue('debug')}]`, message); |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +const newLine = () => console.log(); |
| 16 | + |
| 17 | +const inputDir = './input'; |
| 18 | +const expectedOutputDir = './expected-output'; |
| 19 | + |
| 20 | +// NOTE: Run with `--debug` to get debug output (from both this script and prettier) |
| 21 | +const run = async () => { |
| 22 | + logDebug('Reading input directory...'); |
| 23 | + |
| 24 | + try { |
| 25 | + const inputFiles = await fs.readdir(inputDir); |
| 26 | + const originalFiles = new Map(); |
| 27 | + |
| 28 | + logDebug('Backing up input files...'); |
| 29 | + for (const file of inputFiles) { |
| 30 | + const filePath = path.join(inputDir, file); |
| 31 | + const fileContent = await fs.readFile(filePath, 'utf-8'); |
| 32 | + originalFiles.set(file, fileContent); |
| 33 | + } |
| 34 | + |
| 35 | + if (debug) { |
| 36 | + newLine(); |
| 37 | + } |
| 38 | + console.log('💅 Running Prettier...'); |
| 39 | + const prettier = spawn( |
| 40 | + 'prettier', |
| 41 | + [ |
| 42 | + '.', |
| 43 | + '--write', |
| 44 | + '--plugin', |
| 45 | + 'prettier-plugin-ember-template-tag', |
| 46 | + debug ? '--log-level' : null, |
| 47 | + debug ? 'debug' : null, |
| 48 | + ].filter(Boolean), |
| 49 | + { |
| 50 | + env: { |
| 51 | + ...process.env, |
| 52 | + FORCE_COLOR: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + ); |
| 56 | + |
| 57 | + prettier.stdout.pipe(process.stdout); |
| 58 | + prettier.stderr.pipe(process.stderr); |
| 59 | + |
| 60 | + prettier.on('close', async (code) => { |
| 61 | + if (code !== 0) { |
| 62 | + console.error(chalk.red(`💩 Prettier exited with code ${code}`)); |
| 63 | + process.exit(code); |
| 64 | + } |
| 65 | + |
| 66 | + newLine(); |
| 67 | + console.log('👯 Comparing files...'); |
| 68 | + |
| 69 | + let allFilesMatch = true; |
| 70 | + |
| 71 | + for (const file of inputFiles) { |
| 72 | + const inputFilePath = path.join(inputDir, file); |
| 73 | + const expectedOutputFilePath = path.join(expectedOutputDir, file); |
| 74 | + |
| 75 | + try { |
| 76 | + const [inputContent, expectedOutputContent] = await Promise.all([ |
| 77 | + fs.readFile(inputFilePath, 'utf-8'), |
| 78 | + fs.readFile(expectedOutputFilePath, 'utf-8'), |
| 79 | + ]); |
| 80 | + |
| 81 | + if (inputContent === expectedOutputContent) { |
| 82 | + console.log( |
| 83 | + chalk.green('Pass'), |
| 84 | + `${file}: Formatted content matches expected output.`, |
| 85 | + ); |
| 86 | + logDebug(`Resetting ${file}...`); |
| 87 | + await fs.writeFile(inputFilePath, originalFiles.get(file)); |
| 88 | + } else { |
| 89 | + console.log( |
| 90 | + chalk.red('Fail'), |
| 91 | + `${file}: Formatted content does not match expected output.`, |
| 92 | + ); |
| 93 | + allFilesMatch = false; |
| 94 | + } |
| 95 | + } catch (err) { |
| 96 | + console.error(chalk.red(`Error processing ${file}:`), err); |
| 97 | + allFilesMatch = false; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + newLine(); |
| 102 | + |
| 103 | + if (allFilesMatch) { |
| 104 | + console.log('👋 Exiting...', chalk.green('SUCCESS')); |
| 105 | + process.exit(0); |
| 106 | + } else { |
| 107 | + console.log('👋 Exiting...', chalk.red('FAIL')); |
| 108 | + process.exit(1); |
| 109 | + } |
| 110 | + }); |
| 111 | + } catch (err) { |
| 112 | + console.error(chalk.red('💩 Error:'), err); |
| 113 | + process.exit(2); |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +run(); |
0 commit comments