1
- const { src, dest, parallel, series } = require ( 'gulp' ) ;
1
+ const { src, dest, parallel, series, task } = require ( 'gulp' ) ;
2
2
const fs = require ( 'fs' ) ;
3
3
const through = require ( 'through2' ) ;
4
4
const sass = require ( 'gulp-sass' ) ;
@@ -8,7 +8,87 @@ const ts = require('gulp-typescript');
8
8
const cleanCSS = require ( 'gulp-clean-css' ) ;
9
9
const rename = require ( 'gulp-rename' ) ;
10
10
const babel = require ( 'gulp-babel' ) ;
11
+ const mergeStream = require ( 'merge-stream' ) ;
11
12
13
+ function difference ( array , arrCompare ) {
14
+ return array . concat ( arrCompare ) . filter ( function ( v , i , arr ) {
15
+ return arr . indexOf ( v ) === arr . lastIndexOf ( v ) ;
16
+ } ) ;
17
+ }
18
+ function unique ( arr ) {
19
+ return Array . from ( new Set ( arr ) ) ;
20
+ }
21
+ /**
22
+ * existStyleCatalogName ---- The name of the component catalog where the style exists
23
+ * componentCatalogName ---- All component catalog names
24
+ */
25
+ let existStyleCatalogName = [ ] ;
26
+ let componentCatalogName = [ ] ;
27
+
28
+ function writeStyleFile ( catalogArr ) {
29
+ for ( let catalogName of catalogArr ) {
30
+ fs . writeFile ( `src/components/${ catalogName } /style.scss` , "" , { 'flag' : 'wx' } , ( err ) => {
31
+ if ( err ) {
32
+ throw err ;
33
+ }
34
+ } )
35
+ }
36
+ }
37
+
38
+ function delStyleFile ( catalogArr ) {
39
+ for ( let catalogName of catalogArr ) {
40
+ del ( [ `src/components/${ catalogName } /style.scss` ] )
41
+ }
42
+ }
43
+ /**
44
+ * traverse files
45
+ * @param isDelete Distinguish between write and delete style file
46
+ */
47
+ function traverseExistStyleFile ( isDelete ) {
48
+ return src ( [ 'src/components/*/*.scss' ] )
49
+ . pipe ( through . obj ( function ( file , enc , callback ) {
50
+ isExist = Boolean ( file . contents . toString ( ) )
51
+ if ( isExist ) {
52
+ existStyleCatalogName . push ( file . relative . split ( '/' ) [ 0 ] ) ;
53
+ }
54
+ callback ( ) ;
55
+ } ) ) . on ( 'end' , function ( ) {
56
+ const noStyleComp = difference ( unique ( componentCatalogName ) , unique ( existStyleCatalogName ) ) ;
57
+ if ( isDelete ) {
58
+ delStyleFile ( noStyleComp )
59
+ console . log ( 'delStyleFile exec end' )
60
+ } else {
61
+ writeStyleFile ( noStyleComp )
62
+ console . log ( 'writeStyleFile exec end' )
63
+ }
64
+ } )
65
+ }
66
+ function traverseComponent ( ) {
67
+ console . warn ( 'Do not edit or modify the source file when the project is compiled !!!' )
68
+ return src ( [ 'src/components/*/' , '!src/components/utils/' ] ) // exclude utils/
69
+ . pipe ( through . obj ( function ( file , enc , callback ) {
70
+ componentCatalogName . push ( file . relative . split ( '/' ) [ 0 ] ) ;
71
+ callback ( ) ;
72
+ } ) ) . on ( 'end' , function ( ) {
73
+ console . log ( 'traverseComponent exec end ~' )
74
+ } )
75
+ }
76
+
77
+ function mergeTraverseStream ( type ) {
78
+ return mergeStream ( traverseComponent ( ) , traverseExistStyleFile ( type ) )
79
+ }
80
+
81
+ function generateStyleFile ( ) {
82
+ return mergeTraverseStream ( false )
83
+ }
84
+
85
+ function cleanUselessStyleFile ( ) {
86
+ return mergeTraverseStream ( true )
87
+ }
88
+ /**
89
+ * TODO optimize outputStyleTask
90
+ * outputStyleTask Function Time-consuming to execute..
91
+ */
12
92
function outputStyleTask ( ) {
13
93
fs . writeFile ( 'src/index.tsx' , "import './style.scss';" , { 'flag' :'a' } , ( err ) => {
14
94
if ( err ) {
@@ -44,7 +124,8 @@ function globalSass() {
44
124
. pipe ( dest ( 'lib/style' ) ) ;
45
125
}
46
126
async function clean ( cb ) {
47
- await del ( [ 'lib' , 'src/index.tsx' ] ) ;
127
+ await cleanUselessStyleFile ( )
128
+ await del ( [ 'lib' , 'src/index.tsx' ] ) ;
48
129
await cb ( ) ;
49
130
}
50
131
function globalCss ( ) {
@@ -67,4 +148,4 @@ function jsForCss(data) {
67
148
. pipe ( babel ( ) )
68
149
. pipe ( dest ( 'lib/' + String ( data ) + '/style/' ) ) ;
69
150
}
70
- exports . default = series ( clean , parallel ( outputStyleTask , globalSass ) , globalCss ) ;
151
+ exports . default = series ( clean , generateStyleFile , parallel ( outputStyleTask , globalSass ) , globalCss , cleanUselessStyleFile ) ;
0 commit comments