-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.babel.js
121 lines (110 loc) · 3.51 KB
/
gulpfile.babel.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
import gulp from "gulp";
import rimraf from "gulp-rimraf";
import webpack from "webpack";
import preprocess from "gulp-preprocess";
import cssMin from "gulp-cssmin";
import scss from "gulp-sass";
import fs from "fs";
import mime from "mime-types";
import pkg from "./package.json";
const
APP_NAME = `EntityBrowser`,
SOURCE_DIR = `${ __dirname }/src`,
BUILD_DIR = `${ __dirname }/docs`,
STATIC_BUILD_DIR = BUILD_DIR,
STATIC_DATA_FILE = `${ SOURCE_DIR }/cls/${ APP_NAME }/StaticData.cls`,
context = {
package: pkg
},
webpackConfig = {
context: `${ __dirname }`,
entry: {
"index": `${ SOURCE_DIR }/static/js/index.js`
},
output: {
path: `${ STATIC_BUILD_DIR }/js`,
filename: `[name].js`
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: `babel-loader`,
query: {
presets: [`es2015`]
}
}
]
}
};
function getAllFiles (dir) {
let results = [];
let list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
let stat = fs.statSync(file);
if (stat && stat.isDirectory()) results = results.concat(getAllFiles(file));
else results.push(file)
});
return results;
}
function base64Encode (file) {
return (new Buffer(fs.readFileSync(file, 'binary'), 'binary')).toString(`base64`);
}
gulp.task("clean", () => {
return gulp.src(BUILD_DIR, { read: false })
.pipe(rimraf());
});
gulp.task("cls", ["clean"], () => {
return gulp.src([
`!${ STATIC_DATA_FILE }`,
`${ SOURCE_DIR }/cls/**/*.cls`
])
.pipe(preprocess({ context: context }))
.pipe(gulp.dest(BUILD_DIR + "/cls"));
});
gulp.task("html", ["clean"], () => {
return gulp.src(`${ SOURCE_DIR }/static/**/*.html`)
.pipe(preprocess({ context: context }))
.pipe(gulp.dest(STATIC_BUILD_DIR));
});
gulp.task("etc", ["clean"], () => {
return gulp.src([
`${ SOURCE_DIR }/static/**/*.*`,
`!${ SOURCE_DIR }/static/js/**/*.*`,
`!${ SOURCE_DIR }/static/scss/**/*.*`,
`!${ SOURCE_DIR }/static/index.html`
])
.pipe(gulp.dest(STATIC_BUILD_DIR));
});
gulp.task("js", ["clean"], (done) => {
webpack(webpackConfig, (err, stats) => {
if (err) throw new Error(err);
console.log(stats.toString());
done(err);
});
});
gulp.task("css", ["clean"], () => {
return gulp.src(`${ SOURCE_DIR }/static/scss/index.scss`)
.pipe(scss().on("error", scss.logError))
.pipe(cssMin())
.pipe(gulp.dest(`${ STATIC_BUILD_DIR }/css`));
});
/// doing file replacement manually because preprocess sucks.
gulp.task("StaticData", ["html", "js", "css", "etc"], () => {
let files = getAllFiles(STATIC_BUILD_DIR),
staticData = files.map((fileName, i) =>
`/// ${ fileName.replace(`${ STATIC_BUILD_DIR }/`, "") }\r\n\
XData File${ i } [ MimeType = ${ mime.lookup(fileName) || "text/plain" } ]\r\n\
{\r\n\
${ base64Encode(fileName).replace(/(.{32765})/g, "$1\r\n") }\r\n\
}`
).join(`\r\n\r\n`);
fs.writeFileSync(
STATIC_DATA_FILE.replace(SOURCE_DIR, BUILD_DIR),
new Buffer(fs.readFileSync(STATIC_DATA_FILE)).toString()
.replace("<!-- staticData -->", staticData)
);
});
gulp.task("default", ["cls", "StaticData"]);