@@ -5,6 +5,9 @@ var path = require('path');
55var shelljs = global . shelljs || require ( 'shelljs' ) ;
66var webpackGulp = require ( 'webpack-stream' ) ;
77var webpack = require ( 'webpack' ) ;
8+ var platform = 'typescript' ;
9+ var sampleList ;
10+ var commonConfig = require ( './config.json' ) ;
811
912gulp . task ( 'scripts' , function ( done ) {
1013 var ts = require ( 'gulp-typescript' ) ;
@@ -55,8 +58,182 @@ gulp.task('whole-bundle', function (done) {
5558
5659gulp . task ( 'build' , function ( done ) {
5760 var runSequence = require ( 'run-sequence' ) ;
58- runSequence ( 'scripts' , 'whole-bundle' , done ) ;
61+ runSequence ( 'create-locale' , ' scripts', 'whole-bundle' , done ) ;
5962} )
63+ var jsoncombine = require ( 'gulp-jsoncombine' ) ;
64+ var elasticlunr = require ( 'elasticlunr' ) ;
65+ var sampleOrder = '' ;
66+ var sampleList ;
67+
68+ gulp . task ( 'combine-samplelists' , function ( done ) {
69+ combineSampleList ( platform , false , done ) ;
70+ } ) ;
71+
72+ function combineSampleList ( platform , done ) {
73+ var filename = platform === 'javascript' ? 'samplelist.js' : 'sampleList.ts' ;
74+ sampleOrder = JSON . parse ( fs . readFileSync ( `./src/common/sampleOrder.json` ) ) ;
75+ var sampleListPath = `./src/common/` ;
76+ if ( sampleList && sampleList . length ) {
77+ var controls = getControls ( ) ;
78+ sampleOrder = getSampleOrder ( controls ) ;
79+ }
80+ return gulp . src ( commonConfig . samplejson )
81+ . pipe ( jsoncombine ( `${ filename } ` , function ( data ) {
82+ var result = [ ] ;
83+ var subCategory = [ ] ;
84+ var intId = 0 ;
85+ var curDirectory = '' ;
86+ var addUID = function ( pid , dt ) {
87+ for ( var i = 0 ; i < dt . length ; i ++ ) {
88+ dt [ i ] . uid = pid + i ;
89+ if ( dt [ i ] . hasOwnProperty ( 'samples' ) ) {
90+ curDirectory = dt [ i ] . directory ;
91+ subCategory = [ ] ;
92+ addUID ( '00' + intId + i , dt [ i ] . samples ) ;
93+ intId ++ ;
94+ } else {
95+ var index = subCategory . indexOf ( dt [ i ] . category ) ;
96+ if ( index !== - 1 ) {
97+ dt [ i ] . order = index ;
98+ } else {
99+ subCategory . push ( dt [ i ] . category ) ;
100+ dt [ i ] . order = subCategory . length - 1 ;
101+ }
102+ }
103+ }
104+ } ;
105+ var orderKeys = Object . keys ( sampleOrder ) ;
106+ for ( var i = 0 ; i < orderKeys . length ; i ++ ) {
107+ console . log ( 'Category : ' + orderKeys [ i ] ) ;
108+ var components = sampleOrder [ orderKeys [ i ] ] ;
109+ for ( var j = 0 ; j < components . length ; j ++ ) {
110+ console . log ( 'Component : ' + components [ j ] ) ;
111+ var currentData = getSamples ( data , components [ j ] ) ;
112+ currentData . order = i ;
113+ result . push ( currentData ) ;
114+ }
115+ }
116+ addUID ( '0' , result ) ;
117+ generateSearchIndex ( result , sampleListPath , platform ) ;
118+ var configProps = JSON . parse ( fs . readFileSync ( './config.json' , 'utf8' ) ) ;
119+ var commonChunkSkip = configProps . cssComponent || [ ] ;
120+ var sList = `${ platform === 'javascript' ?
121+ 'window.samplesList =' : 'export let samplesList : any =' } ` + JSON . stringify ( result ) + ';\n\n' +
122+ `${ platform === 'typescript' ? `\n\n export let skipCommonChunk: string[] = ${ JSON . stringify ( commonChunkSkip ) } ;` : '' } ` ;
123+ return new Buffer ( sList ) ;
124+
125+ } ) )
126+ . pipe ( gulp . dest ( sampleListPath ) )
127+ . on ( 'error' , function ( e ) {
128+ done ( e ) ;
129+ process . exit ( 1 ) ;
130+ } ) ;
131+ }
132+
133+ function getSamples ( data , component ) {
134+ var dataList = Object . keys ( data ) ;
135+ for ( var i = 0 ; i < dataList . length ; i ++ ) {
136+ var currentData = data [ dataList [ i ] ] ;
137+ if ( component === currentData . name ) {
138+ return currentData ;
139+ }
140+ }
141+ }
142+
143+ function generateSearchIndex ( sampleArray , samplelistpath , platform ) {
144+ var searchindexpath = platform === 'javascript' ? `${ samplelistpath } search-index.js` :
145+ `${ samplelistpath } search-index.json` ;
146+ elasticlunr . clearStopWords ( ) ;
147+ var instance = elasticlunr ( function ( ) {
148+ this . addField ( 'component' ) ;
149+ this . addField ( 'name' ) ;
150+ this . setRef ( 'uid' ) ;
151+ } ) ;
152+ for ( let sampleCollection of sampleArray ) {
153+ var component = sampleCollection . name ;
154+ var directory = sampleCollection . directory ;
155+ var puid = sampleCollection . uid ;
156+ var hideOnDevice = sampleCollection . hideOnDevice ;
157+ for ( let sample of sampleCollection . samples ) {
158+ sample . component = component ;
159+ sample . dir = directory ;
160+ sample . parentId = puid ;
161+ sample . hideOnDevice = hideOnDevice ;
162+ instance . addDoc ( sample ) ;
163+ }
164+ }
165+ fs . writeFileSync ( searchindexpath , `${ platform === 'javascript' ? 'window.searchIndex =' : '' } ` + JSON . stringify ( instance . toJSON ( ) ) ) ;
166+ }
167+
168+ gulp . task ( 'create-locale' , function ( done ) {
169+ createLocale ( platform , done ) ;
170+ } ) ;
171+
172+ function createLocale ( platform , done ) {
173+ var fileExt = platform === 'javascript' ? '.js' : '.ts' ;
174+ var localePath = `${ platform === 'javascript' ? `./dist` : `./src/common` } ` ;
175+ if ( ! fs . existsSync ( localePath ) ) {
176+ shelljs . mkdir ( '-p' , localePath ) ;
177+ }
178+ var localeJson = glob . sync ( './src/**/locale.json' , {
179+ silent : true
180+ } ) ;
181+ if ( localeJson . length ) {
182+ // baseUtil;
183+ var obj = { } ;
184+ for ( var i = 0 ; i < localeJson . length ; i ++ ) {
185+ var compentLocale = JSON . parse ( fs . readFileSync ( localeJson [ i ] ) ) ;
186+ obj = extend ( { } , obj , compentLocale , true ) ;
187+ }
188+ fs . writeFileSync ( `${ localePath } /locale-string${ fileExt } ` ,
189+ `${ platform === 'javascript' ? 'window.Locale=' : 'export let Locale: Object=' } ` + JSON . stringify ( obj ) + ';' ) ;
190+ done ( ) ;
191+ } else {
192+ fs . writeFileSync ( `${ localePath } /locale-string${ fileExt } ` ,
193+ `${ platform === 'javascript' ? 'window.Locale=' : 'export let Locale: Object={}' } ` ) ;
194+ done ( ) ;
195+ }
196+ }
197+
198+ function extend ( copied , first , second , deep ) {
199+ var result = copied || { } ;
200+ var length = arguments . length ;
201+ if ( deep ) {
202+ length = length - 1 ;
203+ }
204+ var _loop_1 = function ( i ) {
205+ if ( ! arguments_1 [ i ] ) {
206+ return 'continue' ;
207+ }
208+ var obj1 = arguments_1 [ i ] ;
209+ Object . keys ( obj1 ) . forEach ( function ( key ) {
210+ var src = result [ key ] ;
211+ var copy = obj1 [ key ] ;
212+ var clone ;
213+ if ( deep && isObject ( copy ) ) {
214+ clone = isObject ( src ) ? src : { } ;
215+ result [ key ] = extend ( { } , clone , copy , true ) ;
216+ }
217+ else {
218+ result [ key ] = copy ;
219+ }
220+ } ) ;
221+ } ;
222+ var arguments_1 = arguments ;
223+ for ( var i = 1 ; i < length ; i ++ ) {
224+ _loop_1 ( i ) ;
225+ }
226+ return result ;
227+ }
228+
229+ function isObject ( obj ) {
230+ var objCon = { } ;
231+ return ( ! isNullOrUndefined ( obj ) && obj . constructor === objCon . constructor ) ;
232+ }
233+
234+ function isNullOrUndefined ( value ) {
235+ return value === undefined || value === null ;
236+ }
60237
61238gulp . task ( 'serve' , [ 'build' ] , function ( done ) {
62239 var browserSync = require ( 'browser-sync' ) ;
@@ -69,5 +246,3 @@ gulp.task('serve', ['build'], function (done) {
69246 } ;
70247 bs . init ( options , done ) ;
71248} ) ;
72-
73-
0 commit comments