| 59 | }; |
| 60 | |
| 61 | async function main() { |
| 62 | if (!argv['input-html']) { |
| 63 | console.log('The --input-html option is required.'); |
| 64 | process.exit(1); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if (!argv['output-css'] && !argv['output-html']) { |
| 69 | console.log('Either --output-css or --output-html options must be specified.'); |
| 70 | process.exit(1); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (argv['input-css'] && argv['output-css'] && path.resolve(argv['input-css']) === path.resolve(argv['output-css'])) { |
| 75 | console.log('The --input-css and --output-css options cannot refer to the same file.'); |
| 76 | process.exit(1); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if (argv['input-html'] && argv['output-html'] && path.resolve(argv['input-html']) === path.resolve(argv['output-html'])) { |
| 81 | console.log('The --input-html and --output-html options cannot refer to the same file.'); |
| 82 | process.exit(1); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | const tailwindcss_path = require.resolve('tailwindcss/lib/cli.js'); |
| 87 | const tailwind_config_path = argv['tailwind-config'] || path.resolve(__dirname, './tailwind.config.js'); |
| 88 | const input_css_path = argv['input-css'] || path.resolve(__dirname, './style.css'); |
| 89 | const output_css_path = argv['output-css'] || path.resolve(os.tmpdir(), 'mailwind.css'); |
| 90 | const input_html_path = argv['input-html']; |
| 91 | const output_html_path = argv['output-html']; |
| 92 | |
| 93 | const result = await exec(process.argv0, [ |
| 94 | tailwindcss_path, |
| 95 | '--config', tailwind_config_path, |
| 96 | '--input', input_css_path, |
| 97 | '--output', output_css_path, |
| 98 | '--content', input_html_path, |
| 99 | ]); |
| 100 | |
| 101 | if (result.exit_code !== 0) { |
| 102 | console.error('Failed to run Tailwind.'); |
| 103 | console.error(result.stderr); |
| 104 | process.exit(1); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if (output_html_path) { |
| 109 | const input_html = fs.readFileSync(input_html_path).toString(); |
| 110 | const output_css = fs.readFileSync(output_css_path).toString(); |
| 111 | |
| 112 | const inlined_html = inlineCSS(input_html, output_css); |
| 113 | |
| 114 | fs.writeFileSync(output_html_path, inlined_html); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | main().catch(error => { |