|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const PassThrough = require('stream').PassThrough |
| 6 | +const notifier = require('update-notifier') |
| 7 | +const engine = require('unified-engine') |
| 8 | +const unified = require('unified') |
| 9 | +const { basename } = require('path') |
| 10 | +const pack = require('./../package') |
| 11 | +const prettier = require('prettier') |
| 12 | +const args = require('./args') |
| 13 | +const { transform, processor } = require('./processor') |
| 14 | + |
| 15 | +const extensions = ['html'] |
| 16 | + |
| 17 | +notifier({ pkg: pack }).notify() |
| 18 | + |
| 19 | +const prettierConfig = prettier.resolveConfig.sync(process.cwd()) || {} |
| 20 | +const cli = args(prettierConfig) |
| 21 | + |
| 22 | +const settings = { |
| 23 | + processor: unified(), |
| 24 | + extensions: extensions, |
| 25 | + configTransform: transform, |
| 26 | + streamError: new PassThrough(), // sink errors |
| 27 | + rcName: '.prettyhtmlrc', |
| 28 | + packageField: 'prettyhtml', |
| 29 | + ignoreName: '.prettyhtmlignore', |
| 30 | + frail: false, |
| 31 | + defaultConfig: transform({ prettierConfig, flags: cli.flags }) |
| 32 | +} |
| 33 | + |
| 34 | +const processResult = processor({ flags: cli.flags }) |
| 35 | + |
| 36 | +if (cli.flags.stdin === false) { |
| 37 | + if (cli.input.length === 0) { |
| 38 | + cli.showHelp() |
| 39 | + } else { |
| 40 | + settings.files = cli.input |
| 41 | + settings.output = true // Whether to overwrite the input files |
| 42 | + settings.out = false // Whether to write the processed file to streamOut |
| 43 | + |
| 44 | + engine(settings, processResult) |
| 45 | + } |
| 46 | +} else { |
| 47 | + if (cli.input.length !== 0) { |
| 48 | + settings.output = basename(cli.input[0]) |
| 49 | + } |
| 50 | + engine(settings, processResult) |
| 51 | +} |
0 commit comments