summaryrefslogtreecommitdiff
path: root/gulpfile.js
blob: 579e795fe090cde3e3d2dff139a90ed661f2079e (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
 * Build file for the frontend codebase of OpenDC.
 *
 * Usage:
 *  $ gulp --config=config.json        # for a single build
 *  $ gulp watch --config=config.json  # to run once, watch for changes, and rebuild when something changed
 *
 * If the `config` argument is omitted, the config file is assumed to be named `config.json` and present in this
 * directory.
 */

'use strict';

const argv = require('yargs').argv;

const gulp = require('gulp');
const notify = require('gulp-notify');
const gulpUtil = require('gulp-util');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const del = require('del');
const fs = require('fs');
const runSequence = require('run-sequence');
const source = require('vinyl-source-stream');
const es = require('event-stream');
const less = require('gulp-less');
const browserify = require('browserify');
const watchify = require('watchify');
const tsify = require('tsify');
const gulpTypings = require("gulp-typings");
const processHTML = require('gulp-processhtml');
const bower = require('gulp-bower');


/**
 * Checks whether the configuration file is specified and reads its contents.
 *
 * @throws an Exception if the config file could not be found or read (logs appropriately to the console)
 * @returns {Object} the config file contents.
 */
function getConfigFile() {
    let configInput = argv.config;

    if (configInput === undefined) {
        if (!fs.existsSync("./config.json")) {
            gulpUtil.log(gulpUtil.colors.red('Config file argument missing\n'), 'Usage:\n' +
                ' $ gulp --config=config.json');
            throw new Exception();
        } else {
            gulpUtil.log(gulpUtil.colors.magenta('No config file argument, assuming `config.json`.'));
            configInput = "config.json";
        }
    }

    try {
        let configFilePath;
        if (configInput.indexOf('/') === -1) {
            configFilePath = './' + configInput;
        } else {
            configFilePath = configInput;
        }

        return require(configFilePath);
    } catch (error) {
        gulpUtil.log(gulpUtil.colors.red('Config file could not be read'), error);
        throw new Exception();
    }
}


/**
 * Stylesheet task.
 */
const stylesRootDir = './src/styles/';
const stylesDestDir = './build/styles/';

const styleFileNames = ['main', 'splash', 'projects', 'profile', 'navbar', '404'];
const styleFilePaths = styleFileNames.map(function (fileName) {
    return stylesRootDir + fileName + '.less';
});

gulp.task('styles', function () {
    return gulp.src(styleFilePaths)
        .pipe(less())
        .pipe(gulp.dest(stylesDestDir))
        .pipe(notify({message: 'Styles task complete', onLast: true}));
});


/**
 * Script task.
 */
const scriptsRootDir = './src/scripts/';
const scriptsDestDir = './build/scripts/';

const postfix = '.entry';
const scriptsFileNames = ['splash', 'main', 'projects', 'profile', 'error404'];
const scriptsFilePaths = scriptsFileNames.map(function (fileName) {
    return scriptsRootDir + fileName + postfix + '.ts';
});

gulp.task('scripts', function () {
    const configFile = getConfigFile();

    const tasks = scriptsFilePaths.map(function (entry, index) {
        return browserify({
            entries: [entry],
            debug: false,
            insertGlobals: true,
            cache: {},
            packageCache: {}
        })
            .plugin(tsify)
            .bundle()
            .pipe(source(scriptsFileNames[index] + postfix + '.js'))
            .pipe(replace('SERVER_BASE_URL', configFile.SERVER_BASE_URL))
            .pipe(gulp.dest(scriptsDestDir));
    });
    return es.merge.apply(null, tasks)
        .pipe(notify({message: 'Scripts task complete', onLast: true}));
});

function getWatchifyHandler(bundler, fileName) {
    const configFile = getConfigFile();

    return () => {
        gulpUtil.log('Beginning build for ' + fileName);
        return bundler
            .bundle()
            .pipe(source(fileName + postfix + '.js'))
            .pipe(replace('SERVER_BASE_URL', configFile.SERVER_BASE_URL))
            .pipe(gulp.dest(scriptsDestDir));
    };
}

gulp.task('watch-scripts', function () {
    const tasks = scriptsFilePaths.map(function (entry, index) {
        const watchedBrowserify = watchify(browserify({
            entries: [entry],
            debug: false,
            cache: {},
            packageCache: {},
            insertGlobals: true,
            poll: 100
        }).plugin(tsify));
        const watchFunction = getWatchifyHandler(watchedBrowserify, scriptsFileNames[index]);

        watchedBrowserify.on('update', watchFunction);
        watchedBrowserify.on('log', gulpUtil.log);
        return watchFunction();
    });

    return es.merge.apply(null, tasks)
        .pipe(notify({message: 'Scripts watch task complete', onLast: true}));
});


/**
 * TypeScript definitions task.
 */
gulp.task("typings", function () {
    return gulp.src("./typings.json")
        .pipe(gulpTypings())
        .pipe(notify({message: 'Typings task complete'}));
});


/**
 * HTML task.
 */
const htmlRootDir = './src/';
const htmlDestDir = './build/';

const htmlFileNames = ['index', 'app', 'projects', 'profile', '404'];
const htmlFilePaths = htmlFileNames.map(function (fileName) {
    return htmlRootDir + fileName + '.html';
});

gulp.task('html', function () {
    const configFile = getConfigFile();

    return gulp.src(htmlFilePaths)
        .pipe(replace('GOOGLE_OAUTH_CLIENT_ID', configFile.GOOGLE_OAUTH_CLIENT_ID))
        .pipe(replace('SERVER_BASE_URL', configFile.SERVER_BASE_URL))
        .pipe(processHTML())
        .pipe(gulp.dest(htmlDestDir))
        .pipe(notify({message: 'HTML task complete', onLast: true}));
});


/**
 * Images task.
 */
const imagesRootDir = './src/img/';
const imagesDestDir = './build/img/';

const imagesFilePaths = [imagesRootDir + '**/*.png', imagesRootDir + '**/*.gif'];

gulp.task('images', function () {
    return gulp.src(imagesFilePaths)
        .pipe(gulp.dest(imagesDestDir))
        .pipe(notify({message: 'Images task complete', onLast: true}));
});


/**
 * Clean task.
 */
gulp.task('clean', function () {
    return del(['./build']);
});


/**
 * Bower task.
 */
gulp.task('bower', function () {
    return bower({cmd: 'install'}, ['--allow-root'])
        .pipe(notify({message: 'Bower task complete', onLast: true}));
});


/**
 * Default build task.
 */
gulp.task('default', function () {
    try {
        getConfigFile();
    } catch (error) {
        return;
    }
    runSequence('clean', 'typings', 'styles', 'bower', 'scripts', 'html', 'images');
});


/**
 * Watch task.
 */
gulp.task('watch', function () {
    try {
        getConfigFile();
    } catch (error) {
        return;
    }

    runSequence('default', () => {
        gulp.watch(stylesRootDir + '**/*.less', ['styles']);
        gulp.start('watch-scripts');
        gulp.watch(htmlRootDir + '**/*.html', ['html']);
        gulp.watch(imagesRootDir + '**/*.png', ['images']);
    });
});