-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
218 lines (200 loc) · 5.48 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* use node v16.13.2
* nvm use v16.13.2
*/
const gulp = require("gulp")
const concat = require("gulp-concat")
var less = require("gulp-less")
const del = require("del")
const plumber = require("gulp-plumber")
const headerComment = require("gulp-header-comment")
const minify = require("gulp-minify")
const zip = require("gulp-zip")
/**
* all settings for the deploy and release
*/
const settings = {
// all for the custom card
files: [
"./src/const.js",
"./src/main.js",
"./src/entitydata.js",
"./src/tools.js",
"./src/dataprovider.js",
"./src/colors.js",
"./src/chartdata.js",
"./src/graphchart.js"
],
// all for the chart bundle
libs: [
"./src/libs/chartjs/chart.js",
"./src/libs/chartjs/plugins/chartjs-gradient.js",
"./src/libs/chartjs/plugins/chartjs-background.js",
"./src/libs/chartjs/plugins/chartjs-plugin-trendline.js",
"./src/libs/chartjs/chartjs-adapter-date.bundle.min.js"
],
libs2: [
"./src/libs/chartjs/chart.js",
"./src/libs/chartjs/plugins/chartjs-gradient.js",
"./src/libs/chartjs/plugins/chartjs-background.js",
"./src/libs/chartjs/plugins/chartjs-plugin-trendline.js"
],
assetsfolder: "assets",
outfile: "chart-card.js",
libsfile: "chart.js",
libsfile2: "chart-bundle.js",
lessfiles: "./src/chartjs.less",
distfolder: "./dist/chart-card",
releasefile: "chart-card.zip",
releasefolder: "./release",
hassfolder: "/Volumes/zeususdata/home/homeassistant/.homeassistant/www/community/chart-card"
// rb3afolder: "/Volumes/rb3a-data/home/homeassistant/.homeassistant/www/community/chart-card"
}
// Command line option:
// --fatal=[warning|error|off]
// var fatalLevel = require('yargs').argv.fatal;
var fatalLevel = "error"
let ERROR_LEVELS = ["error", "warning"]
/**
* Return true if the given level is equal to or more severe than
* the configured fatality error level.
* If the fatalLevel is 'off', then this will always return false.
* Defaults the fatalLevel to 'error'.
* @param {*} level
*/
function isFatal(level) {
return ERROR_LEVELS.indexOf(level) <= ERROR_LEVELS.indexOf(fatalLevel || "error")
}
/**
* Handle an error based on its severity level.
* Log all levels, and exit the process for fatal levels.
* @param {*} level
* @param {*} error
*/
function handleError(level, error) {
console.log(error.message)
if (isFatal(level)) {
process.exit(1)
}
}
function onError(error) {
handleError.call(this, "error", error)
}
function onWarning(error) {
handleError.call(this, "warning", error)
}
/**
* clean up distributen and release
*/
gulp.task("cleanup", function () {
return del([settings.distfolder + "/**/*", settings.releasefolder + "/*.zip"])
})
/**
* used to deploy the files to
* the home-assistant plugins folder
*/
gulp.task("deploy", function () {
if (settings.hassfolder) {
return gulp
.src(settings.distfolder + "/**/*")
.pipe(gulp.dest(settings.hassfolder))
//.pipe(gulp.dest(settings.rb3afolder))
.on("error", onError)
} else {
return
}
})
/**
* build the release for github
*/
gulp.task("release", function () {
return gulp
.src([settings.distfolder + "/**/*"])
.pipe(zip(settings.releasefile))
.pipe(gulp.dest(settings.releasefolder))
.on("error", onError)
})
/**
* build the libs
*/
gulp.task("build-libs", function () {
return (
gulp
.src(settings.libs)
//.pipe(plumber())
.pipe(plumber({ errorHandler: console.log }))
.pipe(concat(settings.libsfile))
.pipe(minify())
.pipe(gulp.dest(settings.distfolder))
.on("error", onError)
)
})
/**
* build the libs-bundle
*/
gulp.task("build-libs-bundle", function () {
return (
gulp
.src(settings.libs2)
.pipe(plumber({ errorHandler: console.log }))
.pipe(concat(settings.libsfile2))
.pipe(minify())
.pipe(gulp.dest(settings.distfolder))
.on("error", onError)
)
})
gulp.task("build-css", function () {
return gulp
.src(settings.lessfiles)
.pipe(less())
.pipe(minify())
.pipe(gulp.dest(settings.distfolder))
.on("error", onError)
})
gulp.task("build-assets", function () {
return gulp
.src(["./src/" + settings.assetsfolder + "/**/*"])
.pipe(gulp.dest(settings.distfolder + "/assets"))
.on("error", onError)
})
/**
* build the custom card
*/
gulp.task("build", function () {
return gulp
.src(settings.files)
.pipe(plumber())
.pipe(concat(settings.outfile))
.pipe(minify())
.pipe(
headerComment(`
<%= pkg.name %> <%= pkg.version %>
<%= pkg.homepage %>
License: <%= pkg.license %>
Generated on <%= moment().format('YYYY') %>
Author: <%= _.capitalize(pkg.author) %>
`)
)
.pipe(gulp.dest(settings.distfolder))
.on("error", onError)
})
/**
* default task
*/
gulp.task(
"release",
gulp.series(["cleanup", "build", "build-libs", "build-libs-bundle", "build-assets", "release"], function (done) {
// task code here
done()
})
)
/**
* default task
*/
gulp.task(
"default",
gulp.series(["cleanup", "build", "build-libs", "build-assets", "release", "deploy"], function (done) {
// task code here
done()
})
)