Skip to content

Commit 2bdc8be

Browse files
committed
feat: add allInOutput option
#298 (review)
1 parent cbe615e commit 2bdc8be

4 files changed

Lines changed: 26 additions & 17 deletions

File tree

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ $ posthtml --help
2626
--output -o Output File or Folder
2727
--config -c Path to config file
2828
--use -u PostHTML plugin name
29-
--root -r Mirror the directory structure relative to this path in the output directory
29+
--root -r Mirror the directory structure relative to this path in the output directory(default: .)
30+
--allInOutput -a Save the nesting structure for output
3031
--help -h CLI Help
3132
--version -v CLI Version
3233

@@ -37,7 +38,8 @@ $ posthtml --help
3738
$ posthtml input.html -o output.html -c posthtml.js
3839
$ posthtml input.html -o output.html -u posthtml-bem --posthtml-bem.elemPrefix __
3940
$ posthtml inputFolder/*.html -o outputFolder
40-
$ posthtml inputFolder/**/*.html -o outputFolder
41+
$ posthtml inputFolder/**/*.html -o outputFolder -a
42+
$ posthtml inputFolder/**/*.html -o outputFolder -a -r inputFolder
4143
```
4244

4345
## Options

src/cfg-resolve.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import mergeOptions from 'merge-options';
44

55
export default ({input, flags = {}}) => {
66
const explorer = cosmiconfigSync('posthtml');
7-
let {config, use, output, root} = flags;
7+
let {config, use, output, root, allInOutput} = flags;
88

99
if (config) {
1010
({config} = explorer.load(config));
@@ -21,6 +21,7 @@ export default ({input, flags = {}}) => {
2121
return mergeOptions({
2222
input,
2323
output,
24-
root
24+
root,
25+
allInOutput
2526
}, use || {}, config || {});
2627
};

src/cli.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ const cli = meow(`
1313
Usage: posthtml <patterns>
1414
1515
Options:
16-
--output -o Output File or Folder
17-
--config -c Path to config file
18-
--use -u PostHTML plugin name
19-
--root -r Mirror the directory structure relative to this path in the output directory
20-
--help -h CLI Help
21-
--version -v CLI Version
16+
--output -o Output File or Folder
17+
--config -c Path to config file
18+
--use -u PostHTML plugin name
19+
--root -r Mirror the directory structure relative to this path in the output directory(default: .)
20+
--allInOutput -a Save the nesting structure for output
21+
--help -h CLI Help
22+
--version -v CLI Version
2223
2324
Examples:
2425
$ posthtml input.html
@@ -27,8 +28,8 @@ const cli = meow(`
2728
$ posthtml input.html -o output.html -c posthtml.js
2829
$ posthtml input.html -o output.html -u posthtml-bem --posthtml-bem.elemPrefix __
2930
$ posthtml inputFolder/*.html -o outputFolder
30-
$ posthtml inputFolder/**/*.html -o outputFolder
31-
$ posthtml inputFolder/**/*.html -o outputFolder -r inputFolder
31+
$ posthtml inputFolder/**/*.html -o outputFolder -a
32+
$ posthtml inputFolder/**/*.html -o outputFolder -a -r inputFolder
3233
`, {
3334
flags: {
3435
config: {
@@ -53,7 +54,12 @@ const cli = meow(`
5354
},
5455
root: {
5556
type: 'string',
56-
alias: 'r'
57+
alias: 'r',
58+
default: '.'
59+
},
60+
allInOutput: {
61+
type: 'boolean',
62+
alias: 'a'
5763
}
5864
}
5965
});
@@ -74,7 +80,7 @@ const getPlugins = config => Object.keys(config.plugins || {})
7480
const config = cfgResolve(cli);
7581

7682
const processing = async file => {
77-
const output = await outResolve(file, config.output, config.root);
83+
const output = await outResolve(file, config.output, config.root, config.allInOutput);
7884
const plugins = Array.isArray(config.plugins) ? config.plugins : getPlugins(config);
7985

8086
makeDir(path.dirname(output))

src/out-resolve.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import path from 'path';
22

3-
export default (input, output, root) => new Promise(resolve => {
3+
export default (input, output, root, allInOutput) => new Promise(resolve => {
44
if (output && path.extname(output)) {
55
return resolve(output);
66
}
77

88
if (output) {
9-
const rootPath = root ? path.resolve(input).replace(path.resolve(root), '') : path.basename(input);
10-
return resolve(path.join(output, rootPath));
9+
const inputPath = allInOutput ? path.relative(root, input) : path.basename(input);
10+
return resolve(path.join(output, inputPath));
1111
}
1212

1313
resolve(input);

0 commit comments

Comments
 (0)