Skip to content

Commit 5c9729a

Browse files
arekkasRyanZim
authored andcommitted
implement --base option for keeping nested directory structures (postcss#121)
Closes postcss#30
1 parent 32e540b commit 5c9729a

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ cat input.css | postcss [OPTIONS] > output.css
4040
|Name|Type|Default|Description|
4141
|:---|:--:|:-----:|:----------|
4242
|`-d, --dir`|`{String}`|`undefined`|Output Directory|
43+
|`-b, --base`|`{String}`|`undefined`|Use together with `--dir` for keeping directory structure.|
4344
|`-x, --ext`|`{String}`|`extname(output)`|Output File Extension|
4445
|`-o, --output`|`{String}`|`undefined`|Output File|
4546
|`-r, --replace`|`{String}`|`undefined`|Replace Input <=> Output|
4647
|`-p, --parser`|`{String}`|`undefined`|Custom PostCSS Parser|
4748
|`-s, --syntax`|`{String}`|`undefined`|Custom PostCSS Syntax|
48-
|`-s, --stringifier`|`{String}`|`undefined`|Custom PostCSS Stringifier|
49+
|`-t, --stringifier`|`{String}`|`undefined`|Custom PostCSS Stringifier|
4950
|`-w, --watch`|`{Boolean}`|`false`|Enable Watch Mode|
5051
|`--poll`|`{Boolean}`|`false`|Use polling for file watching|
5152
|`-u, --use`|`{Array}`|`[]`|PostCSS Plugins|

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ Usage:
109109
desc: 'A shortcut for setting NODE_ENV',
110110
type: 'string'
111111
})
112+
.option('b', {
113+
alias: 'base',
114+
desc: 'Mirror the directory structure relative to this path in the output directory, this only works together with --dir',
115+
type: 'string'
116+
})
112117
.option('c', {
113118
alias: 'config',
114119
desc: 'Set a custom path to look for a config file',
@@ -282,7 +287,7 @@ function css (css, file) {
282287
options.from = file === 'stdin' ? path.join(process.cwd(), 'stdin') : file
283288

284289
if (output || dir || argv.replace) {
285-
options.to = output || (argv.replace ? file : path.join(dir, path.basename(file)))
290+
options.to = output || (argv.replace ? file : path.join(dir, argv.base ? file.replace(path.resolve(argv.base), '') : path.basename(file)))
286291

287292
if (argv.ext) {
288293
options.to = options.to

test/base.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import test from 'ava'
2+
import path from 'path'
3+
4+
import cli from './helpers/cli.js'
5+
import tmp from './helpers/tmp.js'
6+
import read from './helpers/read.js'
7+
8+
test('--base --dir works', async function (t) {
9+
const dir = tmp()
10+
11+
const { error, stderr } = await cli(
12+
[
13+
'test/fixtures/base/**/*.css',
14+
'--dir', dir,
15+
'--base', 'test/fixtures/base',
16+
'--no-map'
17+
]
18+
)
19+
20+
t.ifError(error, stderr)
21+
22+
t.is(
23+
await read(path.join(dir, 'level-1/level-2/a.css')),
24+
await read('test/fixtures/base/level-1/level-2/a.css')
25+
)
26+
27+
t.is(
28+
await read(path.join(dir, 'level-1/b.css')),
29+
await read('test/fixtures/base/level-1/b.css')
30+
)
31+
})

test/fixtures/base/level-1/b.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.b {
2+
color: blue;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.a {
2+
color: red;
3+
}

0 commit comments

Comments
 (0)