-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
185 lines (151 loc) · 5.26 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
// ====================================================
// === REQUIRE ==================================
// ==============================================
var gulp = require("gulp");
var del = require("del");
var source = require("vinyl-source-stream");
var buffer = require("vinyl-buffer");
var fs = require("fs");
var glob = require("glob");
var js_minify = require("gulp-minify");
var clean_css = require("gulp-clean-css");
var add_src = require("gulp-add-src");
var tsify = require("tsify");
var browserify = require("browserify");
var replace = require("gulp-replace");
var spawnSync = require("child_process").spawnSync;
// ====================================================
// === UTIL FUNTIONS ============================
// ==============================================
function is_argument_found( arg ){
return process.argv.indexOf(arg) != -1;
}
function git_last_sha(){
return spawnSync("git", ["--no-pager", "log", "-n1", "--format=format:%h_%cs"]).stdout.toString();
}
function git_current_branch(){
let branch = spawnSync("git", ["--no-pager", "branch", "--show-current", "--format=%(fieldname)"]).stdout.toString().replace(/[\r\n]*/g, "");
return (branch.length == 0) ? "DETACH" : branch;
}
// ====================================================
// === CONFIGURATION ============================
// ==============================================
const DIST_PATH = "dist";
const ASSETS_PATH = `assets`
const FAT_PATH = `${ASSETS_PATH}/fat`;
const IS_RELEASE = is_argument_found("--release");
const LAST_GIT_SHA = `${IS_RELEASE ? "REL__" : "DEV__"}${git_current_branch()}_${git_last_sha()}`;
const APP_VERSION_TAG = "%%APP_VERSION%%";
// ====================================================
// === GULP TASK ================================
// ==============================================
/**
* Clean the DIST_PATH folder
*/
gulp.task('clean', function(){
return del(`${DIST_PATH}/**`, {force:true});
});
/**
* Processing CSS files
*/
gulp.task("static-css-files", function(){
let g = gulp.src("static/css/*.css");
if( IS_RELEASE ){
g = g.pipe( clean_css() );
}
return g.pipe( add_src("static/css/*/**") )
.pipe( gulp.dest(DIST_PATH + "/css", {overwrite: true}));
})
/**
* Processing JS files
*/
gulp.task("static-js-files", function(){
let g = gulp.src("static/js/*.js")
.pipe( replace(APP_VERSION_TAG, LAST_GIT_SHA) );
if( IS_RELEASE ){
g = g.pipe( js_minify({
noSource: true,
ext:{ min: ".js" }
}));
}
return g.pipe( add_src("static/js/*/**"))
.pipe( gulp.dest(DIST_PATH + "/js", {overwrite: true}))
})
/**
* Processing all pther static files
*/
gulp.task("static-files", function(){
return gulp.src( "static/**", {ignore: ["static/css/**", "static/js/**"]} )
.pipe( replace(APP_VERSION_TAG, LAST_GIT_SHA) )
.pipe( gulp.dest(DIST_PATH, {overwrite: true}) );
})
/**
* Copy all static files/folders to DIST_PATH folder
*/
gulp.task("copy-static", function(...args){
gulp.parallel(
"static-files",
"static-css-files",
"static-js-files"
)(...args);
});
/**
* Generate fat.json , that contains the list of files in the FAT_PATH folder (used by app to generate MicroPython's FAT)
*/
gulp.task("generate_json_fat", async function(cb){
let files = glob.sync(`${DIST_PATH}/${FAT_PATH}/*`, {nodir: true});
let result = [];
files.forEach( (file) => {
let filename = file.substring( file.lastIndexOf("/") + 1);
result.push({
name: filename.substring(0, filename.lastIndexOf(".")).toUpperCase(),
extension: filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(),
isBinary: fs.readFileSync(file, {encoding: null, flag: "r"}).slice(0, 300).findIndex( (value) => value == 0x00 ) != -1, // If there is a null (0x00) character in the first 300, so it's a binary file
path: "." + file.substring(DIST_PATH.length)
})
});
fs.writeFileSync(`${DIST_PATH}/${ASSETS_PATH}/fat.json`, JSON.stringify(result, null, IS_RELEASE ? "" : "\t"));
cb();
});
/**
* Compile Typescript files
*/
gulp.task("ts-compilation", function(){
var b = browserify({
basedir: ".",
debug: !IS_RELEASE,
entries: "src/app.ts",
cache: {},
packageCache: {},
})
.plugin(tsify)
.bundle()
.pipe( source("app.js") )
.pipe( replace("%%APP_VERSION%%", LAST_GIT_SHA) );
if( IS_RELEASE ){
b = b.pipe( buffer() )
.pipe( js_minify({
noSource: true,
ext:{ min: ".js" }
}));
}
return b.pipe( gulp.dest(`${DIST_PATH}/js`, {overwrite: true}) );
});
/**
* Default task
*/
gulp.task("default",
gulp.series(
"clean",
gulp.parallel(
gulp.series("copy-static", "generate_json_fat"),
"ts-compilation"
)
)
);
// Start watch if arg '--watch' is present
if( is_argument_found("--watch") ){
console.log("\n\tWatching enabled\n");
gulp.watch( "src/**", gulp.series("ts-compilation") );
gulp.watch( "static/**", gulp.series("default") );
}