-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
173 lines (157 loc) · 5.86 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
'use strict';
var gulp = require( 'gulp' );
var _ = require( 'lodash' );
var requiredir = require( 'require-dir' );
var plugins = {
plumber : require( 'gulp-plumber' ),
chmod : require( 'gulp-chmod' ), //changes file permissions, used by imagemin
concat : require( 'gulp-concat' ), //concatenating multiple files into 1
cssmin : require( 'gulp-cssmin' ), //minifies css
dateFormat : require( 'dateformat' ),
del : require( 'del' ), //deleting folders/files
es : require( 'event-stream' ),
exec : require( 'child_process' ).exec,
execSync : require( 'child_process' ).execSync,
filelog : require( 'gulp-filelog' ), //used for logging files in the pipe to the console
fs : require( 'fs' ), //file system
imagemin : require( 'gulp-imagemin' ), //used by pngCrush
jscs : require( 'gulp-jscs' ), //javascript coding styles
jshint : require( 'gulp-jshint' ), //js linting
stylish : require( 'gulp-jscs-stylish' ), //js linting
karma : require( 'karma' ).server, //unit test runner
mobilizer : require( 'gulp-mobilizer' ), //?
ngAnnotate : require( 'gulp-ng-annotate' ), //adding ngAnnotate
path : require( 'path' ),
pngcrush : require( 'imagemin-pngcrush' ), //img resizer
rename : require( 'gulp-rename' ), //rename files
simpleRename : require( 'gulp-simple-rename' ), //rename files
replace : require( 'gulp-replace' ),
sass : require( 'gulp-sass' ), //sass => css
seq : require( 'run-sequence' ), //run tasks in parallel
size : require( 'gulp-size' ), //output file size
sourcemaps : require( 'gulp-sourcemaps' ), //generate sourcemaps
templateCache: require( 'gulp-angular-templatecache' ),
uglify : require( 'gulp-uglify' ), //minify/uglify
bytediff : require( 'gulp-bytediff' ), //tells the file size before and after a gulp operation
inject : require( 'gulp-inject' ) // used for injecting scripts
};
var util = {
getArg : getArg,
readJson : readJson,
readdir : readdir,
fixRelativePath: fixRelativePath
};
var customAppJsonPath = getArg( "--appjson" );
customAppJsonPath = fixRelativePath( customAppJsonPath );
//if user provided custom appconfig path in the commandline, use it.
//else use the default config.app path
//if config files don't exist, it means this is running as standalone uifw project without uifw-app-scaffold context
// then use framework's config.
var defaultAppConfigPath = './gulp/config.app.js';
var frameworkJsonConfig = require( './gulp/config.framework.js' );
var configs = readJson( (customAppJsonPath || defaultAppConfigPath), frameworkJsonConfig );
_.forEach( configs.app.edition, function( edition ) {
var editionContent;
edition.path = fixRelativePath( edition.path );
editionContent = require( edition.path );
configs[edition.name] = editionContent;
_.mergeWith( configs.component, editionContent, concatArrays );
} );
//masterConfig is a superset of common, component, and app
_.mergeWith( configs.masterConfig, configs.common, configs.component, configs.app, concatArrays );
/*========================================
= gulp tasks =
========================================*/
gulp.task( 'default', ['clean'], function() {
gulp.start( 'build' );
} );
initTasksInGulpFolder();
/*========================================
= funcs =
========================================*/
/**
* @description
* For each task inside "gulp/" folder, initialize/register the task
*/
function initTasksInGulpFolder() {
var tasksInGulpFolder = _.extend( {}, requiredir( './gulp/' ), readdir( './gulp/custom/' ) );
_.forEach( tasksInGulpFolder, initTask );
function initTask( task, name ) {
if ( typeof task.init === "function" ) {
task.init( gulp, plugins, configs, _, util );
}
}
}
/**
* @description
* This function finds the value of command line parameters regardless their location or their appearance order
* for example: gulp script --config /tmp/app.config.js
* getArg("--config") // => /tmp/app.config.js
* @param key
* @returns {*}
*/
function getArg( key ) {
var index = process.argv.indexOf( key );
var next = process.argv[index + 1];
return (index < 0) ? null : (!next || next[0] === "-") ? true : next;
}
/**
* Reads the json file and returns its content. If the file is not found, it will return defaults or {}
*
* @param filePath
* @returns {*}
*/
function readJson( filePath, defaults ) {
var json;
try {
filePath = plugins.path.join( __dirname, filePath );
json = require( filePath );
} catch ( ex ) {
console.log( 'file not found:', ex );
json = defaults || {};
}
return json;
}
/**
* Reads the folder and returns its content. If the folder is not found, it will return defaults or {}
*
* @param dirPath
* @returns {*}
*/
function readdir( dirPath, defaults ) {
var json;
try {
dirPath = plugins.path.join( dirPath );
plugins.fs.readdirSync( dirPath );
//if it doesn't throw, the use requiredir
json = requiredir( dirPath );
} catch ( ex ) {
json = defaults || {};
}
return json;
}
/**
* Used by mergeWith to concat []s instead of replace the entire array
* @param a
* @param b
* @returns {*|string|Array.<T>}
*/
function concatArrays( a, b ) {
if ( _.isArray( a ) ) {
return a.concat( b );
}
}
/**
* @describe if not absolute path and not relative path. e.g., foo/bar/app.config
* prefix the path with "./" to make it relative. e.g., ./foo/bar/app.config
* @param path
* @returns {*}
*/
function fixRelativePath( path ) {
var retPath = path;
if ( path && !path.startsWith( '/' ) && !path.startsWith( '.' ) ) {
//append "./" to make it "./foo/bar/app.config"
retPath = "./" + retPath;
}
return retPath;
}