This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
127 lines (113 loc) · 4.57 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
/*
TODO:
1. Add autoprefixer plugin
*/
// File includes
console.time('Gulp Initialization Time');
var fs = require('fs'), // File system module (built-in)
path = require('path'), // Path module (built-in)
es = require('event-stream'), // Event Stream modification utility
sequence = require('run-sequence'), // Enable sequencing of tasks
// Gulp + Plugins
gulp = require('gulp'), // The main gulp module
sass = require('gulp-sass'), // Compile SASS files
nodemon = require('gulp-nodemon'), // Start nodemon w/ gulp
rename = require('gulp-rename'), // Rename piped files
jshint = require('gulp-jshint'), // Log JSHinting to the console
stylish = require('jshint-stylish'), // Make JSHint look better
concat = require('gulp-concat'), // Concatenate files together
uglify = require('gulp-uglifyjs'), // UglifyJS piped files
clean = require('gulp-clean'), // Clean out the build folder
install = require('gulp-install'), // Install new npm packages
notify = require('gulp-notify'), // Send messages to the system's notifications
livereload = require('gulp-livereload'), // Set up a LiveReload server
// Directories and files, for easy access
files = {
'sass': './server/stylesheets/**/*.scss',
'angular': './server/views/ng/**/*.html',
'js': './server/**/*.js',
'allJS': ['*.js', 'server/**/*.js']
}, dirs = {
'build': './public',
'js': './server/js'
};
// The CSS task
gulp.task('css', function () {
return gulp.src(files.sass)
.pipe(sass({
style: 'compressed',
errLogToConsole: false,
onError: function(err) {
return notify().write(err);
}
})) // Compile SASS to CSS
.pipe(gulp.dest(dirs.build + '/css')) // Write to disk
.pipe(livereload()); // Start a LiveReload instance
});
// The AngularJS task
gulp.task('angularjs', function () {
return gulp.src(files.angular)
.pipe(gulp.dest(path.join(dirs.build, 'html')));
});
// Function that returns an array of all of the
// directories within the given directory
function getFolders(dir){
// Grab all files & folders, then filter out the ones that
// aren't directories (i.e. the files)
return fs.readdirSync(dir).filter(function(file){
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// The JS task
gulp.task('js', function() {
// For each folder found in server/js, run the following
return es.concat.apply(null, getFolders(dirs.js).map(function(folder) {
return gulp.src(path.join(dirs.js, folder, '/*.js')) // All of the files in a folder
.pipe(concat(folder + '.js')) // Concatenate all files
.pipe(gulp.dest(dirs.build + '/js')) // Write to disk
.pipe(uglify()) // Uglify the file
.pipe(rename(folder + '.min.js')) // Add .min to the filename
.pipe(gulp.dest(dirs.build + '/js')) // Write to disk
.pipe(livereload()); // Start a LiveReload Instance
}));
});
// The JSHint task
gulp.task('hint', function () {
return gulp.src(files.allJS)
.pipe(jshint()) // JSHint all of our development files
// Send the JSHint errors to the OS's notifications api
.pipe(notify(function (file) {
if (file.jshint.success) return false;
var errors = file.jshint.results.map(function (data) {
if (data.error) {
return "(" + data.error.line + ':' + data.error.character + ') ' + data.error.reason;
}
}).join("\n");
return file.relative + " (" + file.jshint.results.length + " errors)\n" + errors;
}))
.pipe(jshint.reporter('jshint-stylish')); // Use the stylish output
});
// The Build Cleaner
gulp.task('clean', function () {
return gulp.src([dirs.build + '/js', dirs.build + '/css']).pipe(clean());
});
gulp.task('install', function () {
return gulp.src('./package.json')
.pipe(install());
});
gulp.task('watch', function () {
gulp.watch(files.sass, ['css']); // When sass files are changed run 'css'
gulp.watch(files.allJS, ['hint', 'js']); // When all js files as changed run 'js'
nodemon({
'script': 'server.js',
'ext': 'js,html',
'ignore': ['public/**', 'node_modules/**', 'server/*/**']
});
console.timeEnd('Gulp Initialization Time');
});
gulp.task('dev', function () {
sequence('clean', ['install', 'css', 'angularjs', 'hint', 'js'], 'watch');
});
gulp.task('default', function () {
sequence('clean', ['css', 'angularjs', 'hint', 'js'], 'watch');
});