-
Notifications
You must be signed in to change notification settings - Fork 4
/
Gulpfile.js
117 lines (98 loc) · 3.29 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
const path = require('path');
const fs = require('fs-extra');
const gulp = require('gulp-help')(require('gulp'));
const gutil = require('gulp-util');
const webpack = require('webpack');
const watch = require('gulp-watch');
const gulpCopy = require('gulp-copy');
const sequence = require('gulp-sequence');
const del = require('del');
const banner = (color, banner) => gutil.colors[color || 'cyan'](banner ? `[${banner}]` : '[GULP]');
const pkg = require('./package.json');
// Set paths for our project folder
const projectPath = pkg.widget.path ? path.resolve(pkg.widget.path) + '/' : false;
const widgetsFolder = projectPath ? path.join(projectPath, `/widgets/`) : false;
const deploymentFolder = projectPath ? path.join(projectPath, `/deployment/web/widgets/`) : false;
// Check if project folder exists and is accessible
let stat = null;
if (!projectPath) {
gutil.log(`${banner()} No testproject defined, only copying files to dist/build folder. Set project path in ${gutil.colors.cyan('widget.path')} in ${gutil.colors.magenta('package.json')}`);
} else {
gutil.log(`${banner()} Testproject defined: ${gutil.colors.magenta(projectPath)}`);
try {
stat = projectPath ? fs.statSync(projectPath) : null;
} catch (e) {
gutil.log(`${banner('red')} Error getting project directory:`, e.toString());
gutil.log(`${banner('red')} Copying to the project directory has been disabled`);
stat = null;
}
}
// Helper functions
const runWebpack = callback => {
webpack(require('./webpack.config.js'), function (err, stats) {
if (err) throw new gutil.PluginError("webpack", err);
gutil.log(banner('cyan', 'WEBPACK'), stats.toString({
colors: true,
modules: false
}));
callback && callback();
});
};
const copyFile = paths => {
try {
fs.copySync(paths.src, paths.dest);
} catch (err) {
gutil.log(`${banner('red')} Copy fail`, err);
}
};
const getPaths = (file, srcFolder, destFolder) => {
return {
src: path.join(__dirname, srcFolder, file.relative),
dest: path.join(destFolder, file.relative),
}
}
// Base tasks
gulp.task('watch:src', () => {
return watch('src/**/*', {
verbose: true
}, () => {
runWebpack();
})
});
gulp.task('watch:build', () => {
return watch('build/**/*', {
verbose: stat !== null,
read: false
}, file => {
if (stat !== null) {
const paths = getPaths(file, 'build', deploymentFolder);
if (paths.src.indexOf('package.xml') !== -1) {
return;
}
copyFile(paths);
}
})
});
gulp.task('watch:dist', () => {
return watch(`dist/${pkg.widget.package}.mpk`, {
verbose: stat !== null,
read: false
}, file => {
if (stat !== null) {
const paths = getPaths(file, 'dist', widgetsFolder);
copyFile(paths);
}
})
});
gulp.task('clean', `Cleanup the dist/build`, () => {
return del([
'dist',
'build'
], { force: true });
});
// Final tasks
gulp.task('build', 'Build the widget', done => {
sequence('clean', 'build-dist', done);
});
gulp.task('build-dist', callback => { runWebpack(callback); });
gulp.task('default', ['watch:src', 'watch:build', 'watch:dist']);