-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
52 lines (42 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var path = require( 'path' );
var glob = require( 'glob' );
var sander = require( 'sander' );
var _postcss = require( 'postcss' );
var Promise = sander.Promise;
function identity ( input ) {
return input;
}
module.exports = function postcss ( inputdir, outputdir, options ) {
var processor = _postcss( options.plugins );
var files = glob.sync( options.src || '**/*.css', {
cwd: inputdir
});
if ( typeof options.dest === 'string' && ( typeof options.src !== 'string' || ~options.src.indexOf( '*' ) ) ) {
throw new Error( 'options.dest can only be a string if options.src is a string, and not a glob pattern' );
}
var getDest = typeof options.dest === 'string' ?
function () { return options.dest; } :
typeof options.dest === 'function' ?
options.dest :
identity;
if ( !( 'map' in options ) ) options.map = true;
var separateMapFile = options.map && options.map.inline === false;
var promises = files.map( function ( src ) {
var dest = getDest( src );
return sander.readFile( inputdir, src )
.then( function ( css ) {
return processor.process( css, {
from: path.join( inputdir, src ),
to: path.join( outputdir, dest ),
map: options.map
}).then( function ( result ) {
var promises = [ sander.writeFile( outputdir, dest, result.css ) ];
if ( separateMapFile ) {
promises.push( sander.writeFile( outputdir, dest + '.map', String( result.map ) ) );
}
return Promise.all( promises );
});
});
});
return Promise.all( promises );
};