Skip to content

Commit 0643c49

Browse files
committed
add basic test support for prettyhtml cli and api
1 parent d57a93a commit 0643c49

12 files changed

Lines changed: 4853 additions & 183 deletions

File tree

packages/prettyhtml/cli.js

Lines changed: 0 additions & 178 deletions
This file was deleted.

packages/prettyhtml/cli/args.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const meow = require('meow')
2+
3+
function args(prettierConfig = {}) {
4+
return meow(
5+
`
6+
Usage: prettyhtml [<glob> ...] [options ...],
7+
8+
Options:
9+
10+
--tab-width Specify the number of spaces per indentation-level
11+
--print-width Specify the maximum line length
12+
--use-tabs Use tabs for indentation
13+
--single-quote Use single instead of double quotes
14+
--use-prettier Use prettier to format embedded content
15+
--wrapAttributes Force to wrap attributes (when it has multiple)
16+
--sortAttributes Sort attributes alphabetically
17+
--stdin Specify the standard stream as source (for pipe mode)
18+
--quiet Do not output anything for a file which has no warnings or errors
19+
--silent Do not output messages without fatal set to true
20+
21+
Examples
22+
$ prettyhtml *.html
23+
$ prettyhtml *.html !example.html
24+
$ echo "<custom foo='bat'></custom>" | prettyhtml --stdin
25+
$ echo "<custom foo='bat'></custom>" --stdin ./test.html
26+
`,
27+
{
28+
autoHelp: true,
29+
autoVersion: true,
30+
flags: {
31+
tabWidth: {
32+
type: 'number',
33+
default: prettierConfig.tabWidth || 2
34+
},
35+
printWidth: {
36+
type: 'number',
37+
default: prettierConfig.printWidth || 80
38+
},
39+
useTabs: {
40+
type: 'boolean',
41+
default: prettierConfig.useTabs || false
42+
},
43+
singleQuote: {
44+
type: 'boolean',
45+
default: false
46+
},
47+
usePrettier: {
48+
type: 'boolean',
49+
default: true
50+
},
51+
wrapAttributes: {
52+
type: 'boolean',
53+
default: false
54+
},
55+
sortAttributes: {
56+
type: 'boolean',
57+
default: false
58+
},
59+
stdin: {
60+
type: 'boolean',
61+
default: false
62+
},
63+
quiet: {
64+
type: 'boolean',
65+
default: false
66+
},
67+
silent: {
68+
type: 'boolean',
69+
default: false
70+
}
71+
}
72+
}
73+
)
74+
}
75+
76+
module.exports = args

packages/prettyhtml/cli/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const parse = require('@starptech/rehype-webparser')
2+
const stringify = require('@starptech/prettyhtml-formatter/stringify')
3+
const sortAttributes = require('@starptech/prettyhtml-sort-attributes')
4+
const format = require('@starptech/prettyhtml-formatter')
5+
const report = require('vfile-reporter')
6+
7+
function processor({ flags }) {
8+
return function processResult(err, code, result) {
9+
const out = report(err || result.files, {
10+
quiet: flags.quiet,
11+
silent: flags.silent
12+
})
13+
14+
if (out) {
15+
console.error(out)
16+
}
17+
18+
process.exit(code)
19+
}
20+
}
21+
22+
function transform({ prettierConfig, flags }) {
23+
const plugins = [
24+
[
25+
parse,
26+
{
27+
ignoreFirstLf: false,
28+
decodeEntities: false,
29+
selfClosingCustomElements: true,
30+
selfClosingElements: true
31+
}
32+
],
33+
[
34+
format,
35+
{
36+
tabWidth: flags.tabWidth,
37+
useTabs: flags.useTabs,
38+
singleQuote: flags.singleQuote,
39+
usePrettier: flags.usePrettier,
40+
prettier: prettierConfig
41+
}
42+
]
43+
]
44+
45+
if (flags.sortAttributes || prettierConfig.sortAttributes) {
46+
plugins.push([sortAttributes, {}])
47+
}
48+
49+
plugins.push([
50+
stringify,
51+
{
52+
tabWidth: flags.tabWidth,
53+
printWidth: flags.printWidth,
54+
singleQuote: flags.singleQuote,
55+
wrapAttributes: flags.wrapAttributes
56+
}
57+
])
58+
59+
return { plugins }
60+
}
61+
62+
module.exports = { transform, processor }

0 commit comments

Comments
 (0)