-
Notifications
You must be signed in to change notification settings - Fork 24
/
gulpfile.js
188 lines (155 loc) · 5.61 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*!
* Project: roulette-predictor
* File: ./gulpfile.js
* Copyright(c) [2018-present] <[email protected]>
* License: MIT
*/
'use strict';
// ------------------------------------------------------------------------ //
// ----------------------------- DEPENDENCIES --------------------------- //
// ------------------------------------------------------------------------ //
const fs = require('fs');
const path = require('path');
const util = require('util');
const utin = util.inspect;
const _ = require('lodash');
const argv = require('yargs').argv;
const parseArgs = require('minimist');
const vinylPaths = require('vinyl-paths');
const dateFormat = require('dateformat');
const gulp = require('gulp');
const changed = require('gulp-changed');
const gulpTasks = require('gulp-require-tasks');
// ------------------------------------------------------------------------ //
// ---------------------------- CONFIGURATION --------------------------- //
// ------------------------------------------------------------------------ //
global.ME = {};
const appPath = __dirname;
const modName = path.basename(module.filename, '.js');
const modPath = path.relative(appPath, path.dirname(module.filename));
const modsPath = path.join(appPath, 'modules', path.sep);
const Config = require('config');
const pkg = require('./package.json');
ME.Config = Config;
ME.pkg = _.extend({}, pkg);
ME.version = ME.pkg.version;
ME.NODE_ENV = argv.env
? argv.env
: fs.existsSync('./NODE_ENV')
? fs.readFileSync('./NODE_ENV', ME.pkg.options.file).split('\n')[0].trim()
: ME.NODE_ENV;
ME.VERSION = fs.existsSync('./VERSION') ? fs.readFileSync('./VERSION', ME.pkg.options.file).trim() : 'VERSION_UNKNOWN';
ME.COMMIT = fs.existsSync('./COMMIT') ? fs.readFileSync('./COMMIT', ME.pkg.options.file).trim() : 'COMMIT_UNKNOWN';
ME.CR = '\n';
ME.DIR = {};
ME.WD = path.join(__dirname, path.sep);
ME.DOC = path.join('docs', path.sep);
ME.TMP = path.join('tmp', path.sep);
ME.SRC = path.join('src', path.sep);
ME.BUILD = path.join(`build-${ME.VERSION}`, path.sep);
ME.DIST = path.join(`dist-${ME.VERSION}`, path.sep);
ME.WEB = path.join(`webroot`, path.sep);
ME.CURDIR = path.join(process.cwd(), path.sep);
ME.BOWER = JSON.parse(
fs.existsSync('./.bowerrc')
? fs.readFileSync('./.bowerrc')
: {directory: 'bower_modules'}
).directory;
utin.defaultOptions = _.extend({}, ME.pkg.options.iopts);
let now = new Date();
let headerTpl = _.template(`/**
* @project: <%= pkg.title %> - <%= pkg.description %>
* @package: <%= pkg.name %>@<%= pkg.version %>
* @version: v<%= ME.VERSION %>
* @build: [<%= ME.NODE_ENV %>] ${dateFormat(now, 'yyyy-mm-dd')}T${dateFormat(now, 'HH:MM:ss')}
* @license: <%= pkg.license %>
* @link: <%= pkg.homepage %>
* @repository: <%= pkg.github %>
* Copyright (c): Since ${dateFormat(now, 'yyyy')} - <%= pkg.author.email %>
*/
`);
let footerTpl = _.template(`
/**
* @version: v<%= ME.VERSION %>
* @commit: <%= ME.COMMIT %>
* @build: [<%= ME.NODE_ENV %>] ${dateFormat(now, 'yyyy-mm-dd')}T${dateFormat(now, 'HH:MM:ss')}
* @package: <%= pkg.name %>@<%= pkg.version %> - <%= pkg.title %>
* =========================================================================== *
*/
`);
const Banner = {
header: headerTpl({pkg: ME.pkg, ME: ME})
, footer: footerTpl({pkg: ME.pkg, ME: ME})
};
ME.Banner = Banner;
var envConfig = {
string: 'env'
, default: {env: (process.env.NODE_ENV || ME.NODE_ENV || global.NODE_ENV || 'test')}
};
envConfig = parseArgs(process.argv.slice(2), envConfig);
//-------//
// TASKS //
//-------//
gulpTasks({
path: process.cwd() + '/gulp-tasks'
, separator: ':'
, passGulp: true
});
// ENV ROUTER
gulp.task('default', function () {
// DEFAULT Scenario Route
(function () {
switch (ME.NODE_ENV) {
case 'test': {
return ['test'];
break;
}
case ('dev' || 'development'): {
return ['dev'];
break;
}
case ('prod' || 'production'): {
return ['build'];
break;
}
default: {
return ['greet'];
break;
}
}
})();
});
gulp.task('test', ['greet']);
gulp.task('greet', ['show:config', 'usage']);
gulp.task('build', ['bower2src'], function () {
gulp.start('sync:src2build');
});
// Log file paths in the stream
gulp.task('files:src', function () {
return gulp.src([
path.join(ME.SRC, '**/*')
, path.join(ME.SRC, '**/*.*')
, path.join(ME.SRC, '**/.*')
])
.pipe(changed(ME.BUILD))
.pipe(vinylPaths(function (loPath) {
console.log(`Changed: [${loPath}]`);
return Promise.resolve();
}));
});
gulp.task('show:config', function () {
console.warn(`ENV Config: [${utin(envConfig)}]`);
console.log(`APP-Config: [${utin(ME.Config)}]`);
});
gulp.task('usage', function () {
console.log('\n' + (new Array(50).join('-')));
console.info('\n' + 'Usage: gulp <task>\t-\tRun gulp task(s) specified');
console.info('\n, where <task> is one of:\n');
console.warn('\tusage' + '\t\t', 'Show this topic');
console.warn('\tshow:config' + '\t', 'Show Configuration file');
console.warn('\tfiles:src' + '\t', 'Log File Paths in the Stream');
console.warn('\tsync:bower2src' + '\t', 'Collect main bower files into build/resources');
console.warn('\tsync:src2build' + '\t', 'Copy source code into build/');
console.log('\n' + (new Array(50).join('-')));
console.warn('\n');
});