1
+ const { src, dest, series, parallel } = require ( 'gulp' ) ;
2
+ const del = require ( 'del' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ const zip = require ( 'gulp-zip' ) ;
5
+ const log = require ( 'fancy-log' ) ;
6
+ const webpack_stream = require ( 'webpack-stream' ) ;
7
+ const webpack_config = require ( './webpack.config.js' ) ;
8
+ var exec = require ( 'child_process' ) . exec ;
9
+
10
+ const paths = {
11
+ prod_build : '../prod-build' ,
12
+ server_file_name : 'server.bundle.js' ,
13
+ react_src : '../my-app/build/**/*' ,
14
+ react_dist : '../prod-build/my-app/build' ,
15
+ zipped_file_name : 'react-nodejs.zip'
16
+ } ;
17
+
18
+ function clean ( ) {
19
+ log ( 'removing the old files in the directory' )
20
+ return del ( '../prod-build/**' , { force :true } ) ;
21
+ }
22
+
23
+ function createProdBuildFolder ( ) {
24
+
25
+ const dir = paths . prod_build ;
26
+ log ( `Creating the folder if not exist ${ dir } ` )
27
+ if ( ! fs . existsSync ( dir ) ) {
28
+ fs . mkdirSync ( dir ) ;
29
+ log ( '📁 folder created:' , dir ) ;
30
+ }
31
+
32
+ return Promise . resolve ( 'the value is ignored' ) ;
33
+ }
34
+
35
+ function buildReactCodeTask ( cb ) {
36
+ log ( 'building React code into the directory' )
37
+ return exec ( 'cd ../my-app && npm run build' , function ( err , stdout , stderr ) {
38
+ log ( stdout ) ;
39
+ log ( stderr ) ;
40
+ cb ( err ) ;
41
+ } )
42
+ }
43
+
44
+ function copyReactCodeTask ( ) {
45
+ log ( 'copying React code into the directory' )
46
+ return src ( `${ paths . react_src } ` )
47
+ . pipe ( dest ( `${ paths . react_dist } ` ) ) ;
48
+ }
49
+
50
+ function copyNodeJSCodeTask ( ) {
51
+ log ( 'building and copying server code into the directory' )
52
+ return webpack_stream ( webpack_config )
53
+ . pipe ( dest ( `${ paths . prod_build } ` ) )
54
+ }
55
+
56
+ function zippingTask ( ) {
57
+ log ( 'zipping the code ' )
58
+ return src ( `${ paths . prod_build } /**` )
59
+ . pipe ( zip ( `${ paths . zipped_file_name } ` ) )
60
+ . pipe ( dest ( `${ paths . prod_build } ` ) )
61
+ }
62
+
63
+ exports . default = series (
64
+ clean ,
65
+ createProdBuildFolder ,
66
+ buildReactCodeTask ,
67
+ parallel ( copyReactCodeTask , copyNodeJSCodeTask ) ,
68
+ zippingTask
69
+ ) ;
0 commit comments