-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
108 lines (97 loc) · 2.84 KB
/
gulpfile.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* eslint promise/param-names: 0 */
/*
* Ensure ENV
*/
require('dotenv').config()
const fs = require('fs-extra')
const path = require('path')
const gulp = require('gulp')
const clean = require('gulp-clean')
const webpack = require('webpack-stream')
const uglify = require('gulp-uglify')
const server = require('gulp-develop-server')
const sourcemaps = require('gulp-sourcemaps')
const clientPath = path.resolve(__dirname, 'client')
const serverPath = path.resolve(__dirname, 'server')
const publicPath = path.resolve(__dirname, 'public')
const srcPath = path.join(clientPath, 'src')
const assetsPath = path.join(publicPath, 'assets')
const configPath = path.join(serverPath, 'src/config')
const config = require(path.join(configPath, 'gulp'))
const getFileData = (axios) => (`/* eslint-disable */
global.AXIOS = ${JSON.stringify(axios)}`)
const ensure = (p) => (
new Promise((success, failure) => {
fs.ensureDir(p, (e) => {
if (e) return failure(e)
success(p)
})
})
)
const writeFile = (p, f) => (
new Promise((success, failure) => {
fs.writeFile(p, f, (e) => {
if (e) return failure(e)
success(p)
})
})
)
gulp
.task('default', ['clean', 'config', 'webpack', 'uglify', 'watch', 'server', 'watch-server'], () => {
console.log('[RMA Cast React + Hogan]')
})
.task('start', ['clean', 'config', 'webpack', 'uglify'], () => {
console.log('[RMA Cast React + Hogan (Start)]')
})
.task('clean', () => {
return gulp.src(path.resolve(assetsPath, 'js/app/**/*.*'), { read: false })
.pipe(clean())
})
.task('config', (next) => {
const {
API_PROTOCOL,
API_HOST,
API_PORT,
API_BASEURL,
API_VERSION
} = process.env
global.AXIOS = {
API_PROTOCOL,
API_HOST,
API_PORT,
API_BASEURL,
API_VERSION
}
const p = path.join(clientPath, 'src/config')
const filePath = path.join(p, 'index.js')
const fileData = getFileData(global.AXIOS)
ensure(p)
.then(() => writeFile(filePath, fileData))
.then(() => next())
.catch((e) => next(e))
})
.task('webpack', () => {
return gulp.src([])
.pipe(webpack(config.webpack.run))
.pipe(gulp.dest(path.resolve(assetsPath, 'js/app/')))
})
.task('uglify', () => {
return gulp.src(path.resolve(srcPath, '**/*.js!src/app.js'))
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.resolve(assetsPath, 'js/lib/')))
})
.task('watch', () => {
gulp
.watch(config.jshint.all, ['clean', 'webpack'])
gulp
.watch(path.resolve(serverPath, 'app/mvc/views/**/*.*'), server.restart)
})
.task('server', () => {
server.listen({ path: 'app', args: ['--react', '5002', '--hogan', '5003'] })
})
.task('watch-server', () => {
gulp
.watch(['app.js', 'server/index.js'], server.restart)
})