forked from TerriaJS/TerriaMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
159 lines (130 loc) · 5.52 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
'use strict';
/*global require*/
// Every module required-in here must be a `dependency` in package.json, not just a `devDependency`,
// This matters if ever we have gulp tasks run from npm, especially post-install ones.
var gulp = require('gulp');
var gutil = require('gulp-util');
var path = require('path');
var minNode = require('./package.json').engines.node;
if (!require('semver').satisfies(process.version, minNode)) {
console.log('Terria requires Node.js ' + minNode + ' to build. Please update your version of Node.js, delete your node_modules directory' +
', then run npm install and gulp again.');
process.exit();
}
gulp.task('build', ['build-css', 'copy-terriajs-assets', 'build-app']);
gulp.task('release', ['build-css', 'copy-terriajs-assets', 'release-app', 'make-editor-schema']);
gulp.task('watch', ['watch-css', 'watch-terriajs-assets', 'watch-app']);
gulp.task('default', ['lint', 'build']);
var watchOptions = {
interval: 1000
};
gulp.task('build-app', ['write-version'], function(done) {
var runWebpack = require('terriajs/buildprocess/runWebpack.js');
var webpack = require('webpack');
var webpackConfig = require('./buildprocess/webpack.config.js');
runWebpack(webpack, webpackConfig, done);
});
gulp.task('release-app', ['write-version'], function(done) {
var runWebpack = require('terriajs/buildprocess/runWebpack.js');
var webpack = require('webpack');
var webpackConfig = require('./buildprocess/webpack.config.js');
runWebpack(webpack, Object.assign({}, webpackConfig, {
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin()
].concat(webpackConfig.plugins || [])
}), done);
});
gulp.task('watch-app', function(done) {
var fs = require('fs');
var watchWebpack = require('terriajs/buildprocess/watchWebpack');
var webpack = require('webpack');
var webpackConfig = require('./buildprocess/webpack.config.js');
fs.writeFileSync('version.js', 'module.exports = \'Development Build\';');
watchWebpack(webpack, webpackConfig, done);
});
gulp.task('build-css', function() {
var less = require('gulp-less');
var NpmImportPlugin = require('less-plugin-npm-import');
var rename = require('gulp-rename');
return gulp.src('./index.less')
.on('error', onError)
.pipe(less({
plugins: [
new NpmImportPlugin()
]
}))
.pipe(rename('TerriaMap.css'))
.pipe(gulp.dest('./wwwroot/build/'));
});
gulp.task('watch-css', ['build-css'], function() {
var terriaStylesGlob = path.join(getPackageRoot('terriajs'), 'lib', 'Styles', '**', '*.less');
var appStylesGlob = path.join(__dirname, 'lib', 'Styles', '**', '*.less');
return gulp.watch(['./index.less', terriaStylesGlob, appStylesGlob], watchOptions, ['build-css']);
});
gulp.task('copy-terriajs-assets', function() {
var terriaWebRoot = path.join(getPackageRoot('terriajs'), 'wwwroot');
var sourceGlob = path.join(terriaWebRoot, '**');
var destPath = path.resolve(__dirname, 'wwwroot', 'build', 'TerriaJS');
return gulp
.src([ sourceGlob ], { base: terriaWebRoot })
.pipe(gulp.dest(destPath));
});
gulp.task('watch-terriajs-assets', ['copy-terriajs-assets'], function() {
var terriaWebRoot = path.join(getPackageRoot('terriajs'), 'wwwroot');
var sourceGlob = path.join(terriaWebRoot, '**');
return gulp.watch(sourceGlob, watchOptions, [ 'copy-terriajs-assets' ]);
});
// Generate new schema for editor, and copy it over whatever version came with editor.
gulp.task('make-editor-schema', ['copy-editor'], function() {
var generateSchema = require('generate-terriajs-schema');
return generateSchema({
source: getPackageRoot('terriajs'),
dest: 'wwwroot/editor',
noversionsubdir: true,
editor: true,
quiet: true
});
});
gulp.task('copy-editor', function() {
var glob = path.join(getPackageRoot('terriajs-catalog-editor'), '**');
return gulp.src(glob)
.pipe(gulp.dest('./wwwroot/editor'));
});
gulp.task('lint', function() {
var runExternalModule = require('terriajs/buildprocess/runExternalModule');
runExternalModule('eslint/bin/eslint.js', [
'-c', path.join(getPackageRoot('terriajs'), '.eslintrc'),
'--ignore-pattern', 'lib/ThirdParty',
'--max-warnings', '0',
'index.js',
'lib'
]);
});
gulp.task('write-version', function() {
var fs = require('fs');
var spawnSync = require('child_process').spawnSync;
// Get a version string from "git describe".
var version = spawnSync('git', ['describe']).stdout.toString().trim();
var isClean = spawnSync('git', ['status', '--porcelain']).stdout.toString().length === 0;
if (!isClean) {
version += ' (plus local modifications)';
}
fs.writeFileSync('version.js', 'module.exports = \'' + version + '\';');
});
function onError(e) {
if (e.code === 'EMFILE') {
console.error('Too many open files. You should run this command:\n ulimit -n 2048');
process.exit(1);
} else if (e.code === 'ENOSPC') {
console.error('Too many files to watch. You should run this command:\n' +
' echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p');
process.exit(1);
}
gutil.log(e.message);
process.exit(1);
}
function getPackageRoot(packageName) {
return path.dirname(require.resolve(packageName + '/package.json'));
}