forked from stellarterm/stellarterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
195 lines (161 loc) · 5.35 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
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
/* eslint-disable import/no-extraneous-dependencies */
const gulp = require('gulp');
const del = require('del');
const runSequence = require('run-sequence');
const $ = {
sass: require('gulp-sass'),
useref: require('gulp-useref'),
uglify: require('gulp-uglify'),
inlineSource: require('gulp-inline-source'),
};
const browserify = require('browserify');
const watchify = require('watchify');
const source = require('vinyl-source-stream');
const browserSync = require('browser-sync');
const fs = require('fs');
const execSync = require('child_process').execSync;
const reload = browserSync.reload;
// Default task
gulp.task('default', ['clean', 'watch']);
// Clean
gulp.task('clean', (cb) => {
cb(del.sync(['dist']));
});
// Styles
gulp.task('styles', () => {
return gulp.src('./src/styles/**/*.scss')
.pipe($.sass().on('error', $.sass.logError))
.pipe(gulp.dest('./dist/css'));
});
// Images (For big images that get turned into base64)
gulp.task('images', (cb) => {
let file = 'let images = {\n';
let addImage = (name, extension) => {
// Known to support jpg, png, gif. Supports others if mime type matches extension
let mimeType = extension;
if (extension === 'jpg') {
mimeType = 'jpeg';
}
let image = fs.readFileSync(`./images/${name}.${extension}`);
let b64 = new Buffer(image).toString('base64');
file += ` '${name}': 'data:image/${mimeType};base64, ${b64}',\n`
};
addImage('ledger-app','png');
addImage('ledger-nano-picture','jpg');
addImage('ledger-nano-s-buttons','png');
addImage('ledger-logo','png');
file += '};\nmodule.exports = images;';
fs.writeFile('./src/images.js', file, cb);
});
// Build time config only for CUSTOM builds of StellarTerm
gulp.task('customConfig', (cb) => {
let configFile = '\n// This file generated during the gulp build process.\n';
configFile += 'window.stCustomConfig = ';
let configObj = {};
if (process.env.STELLARTERM_CUSTOM_HORIZON_URL) {
configObj.horizonUrl = process.env.STELLARTERM_CUSTOM_HORIZON_URL;
if (configObj.horizonUrl.indexOf('http') !== 0) {
throw new Error('STELLARTERM_CUSTOM_HORIZON_URL environment variable must begin with http. Got: ' + process.env.STELLARTERM_CUSTOM_HORIZON_URL);
}
if (configObj.horizonUrl.substr(-1) === '/') {
throw new Error('STELLARTERM_CUSTOM_HORIZON_URL must not have a trailing slash. Got: ' + process.env.STELLARTERM_CUSTOM_HORIZON_URL);
}
}
if (process.env.STELLARTERM_CUSTOM_NETWORK_PASSPHRASE) {
if (!configObj.horizonUrl) {
throw new Error('To use STELLARTERM_CUSTOM_NETWORK_PASSPHRASE, the environment variable STELLARTERM_CUSTOM_HORIZON_URL must also be set');
}
configObj.networkPassphrase = process.env.STELLARTERM_CUSTOM_NETWORK_PASSPHRASE;
}
configFile += JSON.stringify(configObj, null, 2);
configFile += ';\n';
try {
fs.mkdirSync('./dist');
} catch (e) {}
fs.writeFile('./dist/customConfig.js', configFile, cb);
});
// Build time information
gulp.task('buildInfo', (cb) => {
let buildInfo = '\n// This file generated during the gulp build process.\n';
buildInfo += 'window.stBuildInfo = ';
let infoObj = {};
infoObj.version = parseInt(execSync('git rev-list --count HEAD').toString().trim());
buildInfo += JSON.stringify(infoObj, null, 2);
buildInfo += ';\n';
try {
fs.mkdirSync('./dist');
} catch (e) {}
fs.writeFile('./dist/buildInfo.js', buildInfo, cb);
});
// browserify
const bundler = watchify(browserify({
entries: ['./src/app.jsx'],
debug: true,
insertGlobals: true,
cache: {},
packageCache: {},
fullPaths: false,
insertGlobalVars: {
horizonUrl: function () { return ''; }
}
}));
const rebundle = () => bundler.bundle()
// log errors if they happen
.on('error', (e) => {
console.log(e.stack)
})
.pipe(source('app.js'))
.pipe(gulp.dest('./dist/scripts'))
.on('end', () => {
reload();
});
bundler.on('update', rebundle);
bundler.on('log', (e) => {
console.log(e);
});
gulp.task('scripts', rebundle);
gulp.task('html', () => gulp.src('./src/index.html')
.pipe(gulp.dest('dist')));
gulp.task('buildBundle', ['styles', 'buildScripts', 'moveLibraries'], () => gulp.src('./app/*.html')
.pipe(gulp.useref.assets())
.pipe(gulp.useref.restore())
.pipe(gulp.useref())
.pipe(gulp.dest('dist')));
const baseTasks = ['html', 'styles', 'customConfig', 'buildInfo', 'images', 'scripts', 'copyBower'];
// Watch
gulp.task('watch', baseTasks, () => {
browserSync({
notify: false,
logPrefix: 'BS',
server: ['dist'],
https: true
});
gulp.watch('./src/index.html', ['html-reload']);
gulp.watch(['src/**/*.scss'], ['css-reload']);
});
const bsReload = (done) => {
browserSync.reload();
done();
};
gulp.task('html-reload', ['html'], bsReload);
gulp.task('css-reload', ['styles'], bsReload);
gulp.task('copyBower', () => gulp.src('bower_components/**/*')
.pipe(gulp.dest('dist/bower_components/')));
// Build production site.
gulp.task('uglify-js', () => gulp.src('dist/scripts/app.js')
.pipe($.uglify())
.pipe(gulp.dest('dist/scripts')));
gulp.task('inlinesource', () => gulp.src('./dist/index.html')
.pipe($.inlineSource())
.pipe(gulp.dest('./dist/')));
gulp.task('production', () => {
process.env.NODE_ENV = 'production';
runSequence(
'clean',
baseTasks,
'uglify-js',
'inlinesource'
, () => {
process.exit();
});
});