1+ 'use strict' ;
12var fs = require ( 'fs' ) ;
23var path = require ( 'path' ) ;
34
45var mergeTrees = require ( 'broccoli-merge-trees' ) ;
56var Angular2App = require ( 'angular-cli/lib/broccoli/angular2-app' ) ;
67var BroccoliSass = require ( 'broccoli-sass' ) ;
78var broccoliAutoprefixer = require ( 'broccoli-autoprefixer' ) ;
9+ var cssjanus = require ( 'cssjanus' ) ;
810
911var autoprefixerOptions = require ( './build/autoprefixer-options' ) ;
1012
1113module . exports = function ( defaults ) {
1214 var demoAppCssTree = new BroccoliSass ( [ 'src/demo-app' ] , './demo-app.scss' , 'demo-app/demo-app.css' ) ;
13- var componentCssTree = getComponentsCssTree ( ) ;
15+ var componentCssTree = getCssTree ( 'src/components/' , 'components/' ) ;
1416 var angularAppTree = new Angular2App ( defaults ) ;
1517
1618 return mergeTrees ( [
@@ -21,27 +23,53 @@ module.exports = function(defaults) {
2123} ;
2224
2325
26+ /** Generate RTL CSS files along side regular CSS. */
27+ class BroccoliSassWithRtl extends BroccoliSass {
28+ build ( ) {
29+ // This will output the regular CSS.
30+ super . build ( ) ;
31+
32+ // We then read that file and output the RTL CSS.
33+ const cssOutputPath = path . join ( this . outputPath , this . outputFile ) ;
34+ const cssRtlOutputPath = path . join ( this . outputPath ,
35+ path . dirname ( this . outputFile ) ,
36+ path . basename ( this . outputFile , '.css' ) + '-rtl.css' ) ;
37+ console . log ( cssOutputPath , cssRtlOutputPath ) ;
38+ fs . writeFileSync ( cssRtlOutputPath , cssjanus . transform ( fs . readFileSync ( cssOutputPath ) . toString ( ) ) ) ;
39+ }
40+ }
41+
42+
43+
2444/** Gets the tree for all of the components' CSS. */
25- function getComponentsCssTree ( ) {
26- // Assume that the list of components is all directories in `src/components/`
27- var componentsSrc = 'src/components/' ;
28- var components = fs . readdirSync ( componentsSrc )
29- . filter ( file => fs . statSync ( path . join ( componentsSrc , file ) ) . isDirectory ( ) ) ;
30-
31- var componentCssTrees = components . reduce ( ( trees , component ) => {
32- // We want each individual scss file to be compiled into a corresponding css file
33- // so that they can be individually included in styleUrls.
34- var scssFiles = fs . readdirSync ( path . join ( componentsSrc , component ) )
35- . filter ( file => path . extname ( file ) === '.scss' )
36- . map ( file => path . basename ( file , '.scss' ) ) ;
37-
38- return scssFiles . map ( fileName => {
39- return BroccoliSass (
40- [ `src/components/${ component } ` , 'src/core/style' ] , // Directories w/ scss sources
41- `./${ fileName } .scss` , // Root scss input file
42- `components/${ component } /${ fileName } .css` ) ; // Css output file
43- } ) . concat ( trees ) ;
44- } , [ ] ) ;
45-
46- return broccoliAutoprefixer ( mergeTrees ( componentCssTrees ) , autoprefixerOptions ) ;
45+ function getCssTree ( source , destination ) {
46+ function walk ( dir ) {
47+ const dirs = fs . readdirSync ( dir )
48+ . filter ( file => fs . statSync ( path . join ( dir , file ) ) . isDirectory ( ) ) ;
49+ if ( ! dirs . length ) {
50+ return [ dir ] ;
51+ }
52+
53+ return dirs . reduce ( ( previous , current ) => {
54+ return previous . concat ( walk ( path . join ( dir , current ) ) ) ;
55+ } , [ dir ] ) ;
56+ }
57+
58+ const trees = walk ( source )
59+ . reduce ( ( previous , current ) => {
60+ const dir = path . join ( destination , current . substr ( source . length ) ) ;
61+ const scssFiles = fs . readdirSync ( current )
62+ . filter ( file => path . extname ( file ) === '.scss' )
63+ . map ( file => path . basename ( file , '.scss' ) ) ;
64+
65+ return scssFiles . map ( filename => {
66+ return new BroccoliSassWithRtl (
67+ [ current , 'src/core/style' ] ,
68+ path . join ( '.' , filename + '.scss' ) ,
69+ path . join ( dir , filename + '.css' )
70+ ) ;
71+ } ) . concat ( previous ) ;
72+ } , [ ] ) ;
73+
74+ return broccoliAutoprefixer ( mergeTrees ( trees ) , autoprefixerOptions ) ;
4775}
0 commit comments