-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathgulpfile.js
159 lines (137 loc) · 4.94 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
/*jshint esversion: 6, node: true */
'use strict';
const gutil = require('gulp-util');
const clean = require('gulp-clean');
const gulp = require('gulp');
const gulpDotFlatten = require('./libs/gulp-dot-flatten.js');
const gulpRename = require('gulp-rename');
const gulpScreepsUpload = require('./libs/gulp-screeps-upload.js');
const path = require('path');
const PluginError = require('gulp-util').PluginError;
const ts = require('gulp-typescript');
const tslint = require('gulp-tslint');
const tsProject = ts.createProject('tsconfig.json', { typescript: require('typescript') });
const webpack = require('webpack-stream');
/********/
/* INIT */
/********/
let config;
try {
config = require('./config.json');
} catch (error) {
if (error.code == "MODULE_NOT_FOUND") {
gutil.log(gutil.colors.red('ERROR'), 'Could not find file "config.json"');
} else {
gutil.log(error);
}
process.exit();
}
if (!config.user || !config.user.email || !config.user.password) {
gutil.log(gutil.colors.red('ERROR'), 'Invalid "config.json" file: cannot find user credentials');
process.exit();
}
if (!config.targets) {
gutil.log(gutil.colors.red('ERROR'), 'Invalid "config.json" file: cannot find build targets');
process.exit();
}
if (!config.defaultTarget || !config.targets[config.defaultTarget]) {
gutil.log(gutil.colors.red('ERROR'), 'Invalid "config.json" file: cannot find default build target');
process.exit();
}
gutil.log('Successfully loaded', gutil.colors.magenta('config.json'));
if (gutil.env.target) {
if (!config.targets[gutil.env.target]) {
gutil.log(gutil.colors.red('ERROR'), 'Invalid build target "' + gutil.env.target + '"');
gutil.log('Valid build targets are:', '"' + Object.keys(config.targets).join('", "') + '"');
process.exit();
}
gutil.log('Using selected build target', gutil.colors.magenta(gutil.env.target));
} else {
gutil.log('Using default build target', gutil.colors.magenta(config.defaultTarget));
}
const buildTarget = gutil.env.target || config.defaultTarget;
const buildConfig = config.targets[buildTarget];
/*********/
/* TASKS */
/*********/
gulp.task('lint', function(done) {
if (buildConfig.lint) {
gutil.log('linting ...');
return gulp.src('src/**/*.ts')
.pipe(tslint({ formatter: 'prose' }))
.pipe(tslint.report({
summarizeFailureOutput: true,
emitError: buildConfig.lintRequired === true
}));
} else {
gutil.log('skipped lint, according to config');
return done();
}
});
gulp.task('clean', function () {
return gulp.src(['dist/tmp/', 'dist/' + buildTarget], { read: false, allowEmpty: true })
.pipe(clean());
});
gulp.task('compile-bundled', gulp.series(gulp.parallel('lint', 'clean'), function bundle() {
const webpackConfig = require('./webpack.config.js');
return gulp.src('src/main.ts')
.pipe(webpack(webpackConfig))
.pipe(gulp.dest('dist/' + buildTarget));
}));
gulp.task('compile-flattened', gulp.series(
gulp.parallel('lint', 'clean'),
function tsc() {
global.compileFailed = false;
return tsProject.src()
.pipe(tsProject())
.on('error', (err) => global.compileFailed = true)
.js.pipe(gulp.dest('dist/tmp'));
},
function checkTsc(done) {
if (!global.compileFailed) {
return done();
}
throw new PluginError("gulp-typescript", "failed to compile: not executing further tasks");
},
function flatten() {
return gulp.src('dist/tmp/**/*.js')
.pipe(gulpDotFlatten(0))
.pipe(gulp.dest('dist/' + buildTarget));
}
));
gulp.task('compile', gulp.series(buildConfig.bundle ? 'compile-bundled' : 'compile-flattened'));
gulp.task('upload', gulp.series('compile', function uploading() {
return gulp.src('dist/' + buildTarget + '/*.js')
.pipe(gulpRename((path) => path.extname = ''))
.pipe(gulpScreepsUpload(config.user.email, config.user.password, buildConfig.branch, 0));
}));
gulp.task('copyLocal', gulp.series('compile', function() {
return gulp.src('dist/dev/*')
.pipe(gulp.dest(config.localPath));
}));
gulp.task('watchUpload', function () {
gulp.watch('src/**/*.ts', gulp.series('upload'))
.on('all', function(event, path, stats) {
console.log('');
gutil.log(gutil.colors.green('File ' + path + ' was ' + event + 'ed, running tasks...'));
})
.on('error', function () {
gutil.log(gutil.colors.green('Error during build tasks: aborting'));
});
});
gulp.task('watchLocal', function () {
gulp.watch('src/**/*.ts', gulp.series('copyLocal'))
.on('all', function(event, path, stats) {
console.log('');
gutil.log(gutil.colors.green('File ' + path + ' was ' + event + 'ed, running tasks...'));
})
.on('error', function () {
gutil.log(gutil.colors.green('Error during build tasks: aborting'));
});
});
gulp.task('build', gulp.series('upload', function buildDone(done) {
gutil.log(gutil.colors.green('Build done'));
return done();
}));
gulp.task('test', gulp.series('lint'));
gulp.task('default', gulp.series('watchLocal'));