|
1 | | -// Generated on 2016-11-08 using generator-mendix 2.0.1 :: git+https://github.com/mendix/generator-mendix.git |
2 | | -/*jshint -W069,-W097*/ |
3 | | -"use strict"; |
4 | | - |
5 | | -// In case you seem to have trouble starting Mendix through `gulp modeler`, you might have to set the path to the Mendix application, otherwise leave both values as they are |
6 | | -var MODELER_PATH = null; |
7 | | -var MODELER_ARGS = "/file:{path}"; |
8 | | - |
9 | | -/******************************************************************************** |
10 | | - * Do not edit anything below, unless you know what you are doing |
11 | | - ********************************************************************************/ |
12 | | -var gulp = require("gulp"), |
13 | | - zip = require("gulp-zip"), |
14 | | - del = require("del"), |
15 | | - newer = require("gulp-newer"), |
16 | | - gutil = require("gulp-util"), |
17 | | - gulpif = require("gulp-if"), |
18 | | - jsonTransform = require("gulp-json-transform"), |
19 | | - intercept = require("gulp-intercept"), |
20 | | - argv = require("yargs").argv, |
21 | | - widgetBuilderHelper = require("widgetbuilder-gulp-helper"); |
22 | | - |
23 | | -var pkg = require("./package.json"), |
24 | | - paths = widgetBuilderHelper.generatePaths(pkg), |
25 | | - xmlversion = widgetBuilderHelper.xmlversion; |
26 | | - |
27 | | -gulp.task("default", function() { |
28 | | - gulp.watch("./src/**/*", ["compress"]); |
29 | | - gulp.watch("./src/**/*.js", ["copy:js"]); |
30 | | - gulp.watch("./src/**/*.html", ["copy:html"]); |
31 | | -}); |
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs-extra'); |
| 3 | +const gulp = require('gulp-help')(require('gulp')); |
| 4 | +const gutil = require('gulp-util'); |
| 5 | +const webpack = require('webpack'); |
| 6 | +const watch = require('gulp-watch'); |
| 7 | +const gulpCopy = require('gulp-copy'); |
| 8 | +const sequence = require('gulp-sequence'); |
| 9 | +const del = require('del'); |
32 | 10 |
|
33 | | -gulp.task("clean", function () { |
34 | | - return del([ |
35 | | - paths.WIDGET_TEST_DEST, |
36 | | - paths.WIDGET_DIST_DEST |
37 | | - ], { force: true }); |
38 | | -}); |
| 11 | +const banner = (color, banner) => gutil.colors[color || 'cyan'](banner ? `[${banner}]` : '[GULP]'); |
39 | 12 |
|
40 | | -gulp.task("compress", ["clean"], function () { |
41 | | - return gulp.src("src/**/*") |
42 | | - .pipe(zip(pkg.name + ".mpk")) |
43 | | - .pipe(gulp.dest(paths.TEST_WIDGETS_FOLDER)) |
44 | | - .pipe(gulp.dest("dist")); |
45 | | -}); |
| 13 | +const pkg = require('./package.json'); |
46 | 14 |
|
47 | | -gulp.task("copy:js", function () { |
48 | | - return gulp.src(["./src/**/*.js"]) |
49 | | - .pipe(newer(paths.TEST_WIDGETS_DEPLOYMENT_FOLDER)) |
50 | | - .pipe(gulp.dest(paths.TEST_WIDGETS_DEPLOYMENT_FOLDER)); |
51 | | -}); |
| 15 | +// Set paths for our project folder |
52 | 16 |
|
53 | | -gulp.task("copy:html", function () { |
54 | | - return gulp.src(["./src/**/*.html"]) |
55 | | - .pipe(newer(paths.TEST_WIDGETS_DEPLOYMENT_FOLDER)) |
56 | | - .pipe(gulp.dest(paths.TEST_WIDGETS_DEPLOYMENT_FOLDER)); |
57 | | -}); |
| 17 | +const projectPath = pkg.widget.path ? path.resolve(pkg.widget.path) + '/' : false; |
| 18 | +const widgetsFolder = projectPath ? path.join(projectPath, `/widgets/`) : false; |
| 19 | +const deploymentFolder = projectPath ? path.join(projectPath, `/deployment/web/widgets/`) : false; |
| 20 | + |
| 21 | +// Check if project folder exists and is accessible |
| 22 | + |
| 23 | +let stat = null; |
| 24 | +if (!projectPath) { |
| 25 | + 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')}`); |
| 26 | +} else { |
| 27 | + gutil.log(`${banner()} Testproject defined: ${gutil.colors.magenta(projectPath)}`); |
| 28 | + try { |
| 29 | + stat = projectPath ? fs.statSync(projectPath) : null; |
| 30 | + } catch (e) { |
| 31 | + gutil.log(`${banner('red')} Error getting project directory:`, e.toString()); |
| 32 | + gutil.log(`${banner('red')} Copying to the project directory has been disabled`); |
| 33 | + stat = null; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Helper functions |
58 | 38 |
|
59 | | -gulp.task("version:xml", function () { |
60 | | - return gulp.src(paths.PACKAGE_XML) |
61 | | - .pipe(xmlversion(argv.n)) |
62 | | - .pipe(gulp.dest("./src/")); |
| 39 | +const runWebpack = callback => { |
| 40 | + webpack(require('./webpack.config.js'), function (err, stats) { |
| 41 | + if (err) throw new gutil.PluginError("webpack", err); |
| 42 | + gutil.log(banner('cyan', 'WEBPACK'), stats.toString({ |
| 43 | + colors: true, |
| 44 | + modules: false |
| 45 | + })); |
| 46 | + callback && callback(); |
| 47 | + }); |
| 48 | +}; |
| 49 | + |
| 50 | +const copyFile = paths => { |
| 51 | + try { |
| 52 | + fs.copySync(paths.src, paths.dest); |
| 53 | + } catch (err) { |
| 54 | + gutil.log(`${banner('red')} Copy fail`, err); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +const getPaths = (file, srcFolder, destFolder) => { |
| 59 | + return { |
| 60 | + src: path.join(__dirname, srcFolder, file.relative), |
| 61 | + dest: path.join(destFolder, file.relative), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Base tasks |
| 66 | + |
| 67 | +gulp.task('watch:src', () => { |
| 68 | + return watch('src/**/*', { |
| 69 | + verbose: true |
| 70 | + }, () => { |
| 71 | + runWebpack(); |
| 72 | + }) |
63 | 73 | }); |
64 | 74 |
|
65 | | -gulp.task("version:json", function () { |
66 | | - return gulp.src("./package.json") |
67 | | - .pipe(gulpif(typeof argv.n !== "undefined", jsonTransform(function(data) { |
68 | | - data.version = argv.n; |
69 | | - return data; |
70 | | - }, 2))) |
71 | | - .pipe(gulp.dest("./")); |
| 75 | +gulp.task('watch:build', () => { |
| 76 | + return watch('build/**/*', { |
| 77 | + verbose: stat !== null, |
| 78 | + read: false |
| 79 | + }, file => { |
| 80 | + if (stat !== null) { |
| 81 | + const paths = getPaths(file, 'build', deploymentFolder); |
| 82 | + if (paths.src.indexOf('package.xml') !== -1) { |
| 83 | + return; |
| 84 | + } |
| 85 | + copyFile(paths); |
| 86 | + } |
| 87 | + }) |
72 | 88 | }); |
73 | 89 |
|
74 | | -gulp.task("icon", function (cb) { |
75 | | - var icon = (typeof argv.file !== "undefined") ? argv.file : "./icon.png"; |
76 | | - console.log("\nUsing this file to create a base64 string: " + gutil.colors.cyan(icon)); |
77 | | - gulp.src(icon) |
78 | | - .pipe(intercept(function (file) { |
79 | | - console.log("\nCopy the following to your " + pkg.name + ".xml (after description):\n\n" + gutil.colors.cyan("<icon>") + file.contents.toString("base64") + gutil.colors.cyan("<\\icon>") + "\n"); |
80 | | - cb(); |
81 | | - })); |
| 90 | +gulp.task('watch:dist', () => { |
| 91 | + return watch(`dist/${pkg.widget.package}.mpk`, { |
| 92 | + verbose: stat !== null, |
| 93 | + read: false |
| 94 | + }, file => { |
| 95 | + if (stat !== null) { |
| 96 | + const paths = getPaths(file, 'dist', widgetsFolder); |
| 97 | + copyFile(paths); |
| 98 | + } |
| 99 | + }) |
82 | 100 | }); |
83 | 101 |
|
84 | | -gulp.task("folders", function () { |
85 | | - paths.showPaths(); return; |
| 102 | +gulp.task('clean', `Cleanup the dist/build`, () => { |
| 103 | + return del([ |
| 104 | + 'dist', |
| 105 | + 'build' |
| 106 | + ], { force: true }); |
86 | 107 | }); |
87 | 108 |
|
88 | | -gulp.task("modeler", function (cb) { |
89 | | - widgetBuilderHelper.runmodeler(MODELER_PATH, MODELER_ARGS, paths.TEST_PATH, cb); |
| 109 | +// Final tasks |
| 110 | + |
| 111 | +gulp.task('build', 'Build the widget', done => { |
| 112 | + sequence('clean', 'build-dist', done); |
90 | 113 | }); |
91 | 114 |
|
92 | | -gulp.task("build", ["compress"]); |
93 | | -gulp.task("version", ["version:xml", "version:json"]); |
| 115 | +gulp.task('build-dist', callback => { runWebpack(callback); }); |
| 116 | + |
| 117 | +gulp.task('default', ['watch:src', 'watch:build', 'watch:dist']); |
0 commit comments