UNPKG

5.96 kBJavaScriptView Raw
1var _ = require('lodash');
2var babel = require('gulp-babel');
3var cleanCSS = require('gulp-clean-css');
4var concat = require('gulp-concat');
5var ghpages = require('gh-pages');
6var gulp = require('@momsfriendlydevco/gulpy');
7var gutil = require('gulp-util');
8var nodemon = require('gulp-nodemon');
9var path = require('path');
10var plumber = require('gulp-plumber');
11var preprocess = require('gulp-preprocess');
12var rename = require('gulp-rename');
13var replace = require('gulp-replace');
14var rimraf = require('rimraf');
15var uglify = require('gulp-uglify');
16var vm = require('vm');
17var watch = require('gulp-watch');
18
19gulp.task('default', ['serve']);
20gulp.task('build', ['js', 'css', 'spec']);
21
22
23/**
24* Create a Nodemon monitored server and serve the demo project
25*/
26gulp.task('serve', ['build'], function() {
27 var monitor = nodemon({
28 script: './demo/server.js',
29 ext: 'js css',
30 ignore: ['**/*.js', '**/.css'], // Ignore everything else as its watched seperately
31 })
32 .on('start', function() {
33 console.log('Server started');
34 })
35 .on('restart', function() {
36 console.log('Server restarted');
37 });
38
39 watch(['./index.js', './demo/**/*.js', './src/**/*.js', './src/**/*.html'], function() {
40 console.log('Rebuild client-side JS files...');
41 gulp.start('js');
42 });
43
44 watch(['./demo/**/*.css', './src/**/*.css'], function() {
45 console.log('Rebuild client-side CSS files...');
46 gulp.start('css');
47 });
48});
49
50
51/**
52* Compile all JS files (including minified varients)
53*/
54gulp.task('js', ()=>
55 gulp.src([
56 './src/macgyver.js',
57 './src/components/**/*.js',
58 ])
59 .pipe(plumber({
60 errorHandler: function(err) {
61 gutil.log(gutil.colors.red('ERROR DURING JS BUILD'));
62 process.stdout.write(err.stack);
63 this.emit('end');
64 },
65 }))
66 .pipe(concat('macgyver.js'))
67 .pipe(preprocess({
68 context: {
69 angular: true,
70 },
71 includeBase: `${__dirname}/src`,
72 }))
73 .pipe(babel({
74 presets: ['@babel/env'],
75 plugins: ['angularjs-annotate'],
76 }))
77 .pipe(gulp.dest('./dist'))
78 .pipe(rename('macgyver.min.js'))
79 .pipe(uglify({mangle: false}))
80 .pipe(gulp.dest('./dist'))
81);
82
83
84/**
85* Compile all CSS (including minified varients)
86*/
87gulp.task('css', ()=>
88 gulp.src('./src/**/*.css')
89 .pipe(concat('macgyver.css'))
90 .pipe(gulp.dest('./dist'))
91 .pipe(rename('macgyver.min.css'))
92 .pipe(cleanCSS())
93 .pipe(gulp.dest('./dist'))
94);
95
96
97/**
98* Extract the specification parts of all mg* components and create dist/widgets.json as a lookup object of data about each by key
99* NOTE: Yes I know this is bloody awful in how it works, but it works
100*/
101gulp.task('spec', ()=> {
102 var widgets = {};
103
104 return gulp.src('./src/components/**/*.js')
105 .pipe(plumber({
106 errorHandler: function(err) {
107 gutil.log(gutil.colors.red('ERROR DURING SPEC BUILD'));
108 process.stdout.write(err.stack);
109 this.emit('end');
110 },
111 }))
112 // Slurp JS config for widgets, eval it into a JS object and dump it out as a translated JSON object {{{
113 .pipe(replace(/\$macgyverProvider\.register\((.+?),\s*((.|[\n\r])+?)\t\}\)\)/gm, (all, id, spec) => {
114 var sandbox = {widget: {}};
115 vm.runInContext('widget = ' + spec + '};', vm.createContext(sandbox));
116 // widgets[_.trim(id, "'")] = (new Function('return ' + spec))();
117
118 widgets[_.trim(id, "'")] = _(sandbox.widget)
119 .mapValues((v, k) => {
120 if (_.isString(v) || _.isNumber(v) || _.isBoolean(v) || _.isObject(v)) {
121 return v; // Simple scalar mapping - pass though
122 } else if (k == 'toString' && _.isFunction(k)) {
123 return true; // Mark that we can conver the value to a string but drop the actual function evaluation
124 } else {
125 throw new Error(`Unrecognised property type in MacGyver config for key "${k}" type "${typeof v}". All values should be scalars or a translation should be specified in gulpfile.js`);
126 }
127 })
128 .value();
129 }))
130 // }}}
131 .pipe(concat('widgets.json'))
132 .pipe(replace(/^(.|[\n\r])+$/m, ()=> JSON.stringify(widgets)))
133 .pipe(gulp.dest('./dist'))
134});
135
136
137/**
138* Compile the gh-pages branch in GitHub
139*/
140gulp.task('gh-pages', ['build'], function() {
141 rimraf.sync('./gh-pages');
142
143 return gulp.src([
144 './LICENSE',
145 './demo/_config.yml',
146 './demo/app.js',
147 './demo/app.css',
148 './demo/editor.html',
149 './demo/index.html',
150 './demo/style.css',
151 './dist/**/*',
152 './examples/**/*',
153 './node_modules/angular/angular.min.js',
154 './node_modules/@momsfriendlydevco/angular-bs-tooltip/dist/angular-bs-tooltip.min.js',
155 './node_modules/angular-gravatar/build/angular-gravatar.min.js',
156 './node_modules/angular-relative-date/dist/angular-relative-date.min.js',
157 './node_modules/angular-sanitize/angular-sanitize.js',
158 './node_modules/angular-sanitize/angular-sanitize.js',
159 './node_modules/@momsfriendlydevco/angular-ui-scribble/dist/angular-ui-scribble.css',
160 './node_modules/@momsfriendlydevco/angular-ui-scribble/dist/angular-ui-scribble.js',
161 './node_modules/popper.js/dist/popper.min.js',
162 './node_modules/bootstrap/dist/css/bootstrap.min.css',
163 './node_modules/bootstrap/dist/js/bootstrap.min.js',
164 './node_modules/dragular/dist/dragular.min.js',
165 './node_modules/filesize/lib/filesize.js',
166 './node_modules/font-awesome/css/font-awesome.min.css',
167 './node_modules/font-awesome/fonts/fontawesome-webfont.ttf',
168 './node_modules/font-awesome/fonts/fontawesome-webfont.woff',
169 './node_modules/font-awesome/fonts/fontawesome-webfont.woff2',
170 './node_modules/jquery/dist/jquery.min.js',
171 './node_modules/lodash/lodash.min.js',
172 './node_modules/signature_pad/dist/signature_pad.min.js',
173 './node_modules/tree-tools/dist/ngTreeTools.js',
174 './node_modules/ui-select/dist/select.css',
175 './node_modules/ui-select/dist/select.js',
176 ], {base: __dirname})
177 .pipe(rename(function(path) {
178 if (path.dirname == 'demo') { // Move all demo files into root
179 path.dirname = '.';
180 }
181 return path;
182 }))
183 .pipe(gulp.dest('gh-pages/'))
184 .on('end', function(done) {
185 ghpages.publish(path.join(process.cwd(), 'gh-pages'), done);
186 })
187});