Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
7.8 KiB

8 years ago
8 years ago
6 years ago
8 years ago
8 years ago
8 years ago
6 years ago
6 years ago
6 years ago
6 years ago
8 years ago
8 years ago
5 years ago
5 years ago
  1. /*
  2. ESP8266 file system builder
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*eslint quotes: ['error', 'single']*/
  16. /*eslint-env es6*/
  17. // -----------------------------------------------------------------------------
  18. // Dependencies
  19. // -----------------------------------------------------------------------------
  20. const gulp = require('gulp');
  21. const through = require('through2');
  22. const htmlmin = require('gulp-htmlmin');
  23. const inline = require('gulp-inline');
  24. const inlineImages = require('gulp-css-base64');
  25. const favicon = require('gulp-base64-favicon');
  26. const crass = require('gulp-crass');
  27. const htmllint = require('gulp-htmllint');
  28. const csslint = require('gulp-csslint');
  29. const rename = require('gulp-rename');
  30. const replace = require('gulp-replace');
  31. const remover = require('gulp-remove-code');
  32. const gzip = require('gulp-gzip');
  33. const path = require('path');
  34. const terser = require('terser')
  35. // -----------------------------------------------------------------------------
  36. // Configuration
  37. // -----------------------------------------------------------------------------
  38. const htmlFolder = 'html/';
  39. const configFolder = 'espurna/config/';
  40. const dataFolder = 'espurna/data/';
  41. const staticFolder = 'espurna/static/';
  42. // -----------------------------------------------------------------------------
  43. // Methods
  44. // -----------------------------------------------------------------------------
  45. var toHeader = function(name, debug) {
  46. return through.obj(function (source, encoding, callback) {
  47. var parts = source.path.split(path.sep);
  48. var filename = parts[parts.length - 1];
  49. var safename = name || filename.split('.').join('_');
  50. // Generate output
  51. var output = '';
  52. output += '#define ' + safename + '_len ' + source.contents.length + '\n';
  53. output += 'const uint8_t ' + safename + '[] PROGMEM = {';
  54. for (var i=0; i<source.contents.length; i++) {
  55. if (i > 0) { output += ','; }
  56. if (0 === (i % 20)) { output += '\n'; }
  57. output += '0x' + ('00' + source.contents[i].toString(16)).slice(-2);
  58. }
  59. output += '\n};';
  60. // clone the contents
  61. var destination = source.clone();
  62. destination.path = source.path + '.h';
  63. destination.contents = Buffer.from(output);
  64. if (debug) {
  65. console.info('Image ' + filename + ' \tsize: ' + source.contents.length + ' bytes');
  66. }
  67. callback(null, destination);
  68. });
  69. };
  70. var htmllintReporter = function(filepath, issues) {
  71. if (issues.length > 0) {
  72. issues.forEach(function (issue) {
  73. console.info(
  74. '[gulp-htmllint] ' +
  75. filepath + ' [' +
  76. issue.line + ',' +
  77. issue.column + ']: ' +
  78. '(' + issue.code + ') ' +
  79. issue.msg
  80. );
  81. });
  82. process.exitCode = 1;
  83. }
  84. };
  85. var compressJs = function() {
  86. return through.obj(function (source, encoding, callback) {
  87. if (source.isNull()) {
  88. callback(null, source);
  89. return;
  90. }
  91. if (source.path.endsWith("custom.js")) {
  92. var contents = source.contents.toString();
  93. var result = terser.minify(contents);
  94. if (result.error) {
  95. var { message, filename, line, col, pos } = result.error;
  96. throw new Error("terser error `" + message + "` in file " + filename + ", at line " + line);
  97. }
  98. source.contents = Buffer.from(result.code);
  99. this.push(source);
  100. callback();
  101. return;
  102. }
  103. callback(null, source);
  104. return;
  105. });
  106. };
  107. var buildWebUI = function(module) {
  108. // Declare some modules as optional to remove with
  109. // removeIf(!name) ...code... endRemoveIf(!name) sections
  110. // (via gulp-remove-code)
  111. var modules = {
  112. 'light': false,
  113. 'sensor': false,
  114. 'rfbridge': false,
  115. 'rfm69': false,
  116. 'garland': false,
  117. 'thermostat': false,
  118. 'lightfox': false,
  119. 'curtain': false
  120. };
  121. // Note: only build these when specified as module arg
  122. var excludeAll = [
  123. 'rfm69',
  124. 'lightfox'
  125. ];
  126. // 'all' to include all *but* excludeAll
  127. // '<module>' to include a single module
  128. // 'small' is the default state (all disabled)
  129. if ('all' === module) {
  130. Object.keys(modules).
  131. filter(function(key) {
  132. return excludeAll.indexOf(key) < 0;
  133. }).
  134. forEach(function(key) {
  135. modules[key] = true;
  136. });
  137. } else if ('small' !== module) {
  138. modules[module] = true;
  139. }
  140. return gulp.src(htmlFolder + '*.html').
  141. pipe(htmllint({
  142. 'failOnError': true,
  143. 'rules': {
  144. 'id-class-style': false,
  145. 'label-req-for': false,
  146. 'line-end-style': false
  147. }
  148. }, htmllintReporter)).
  149. pipe(remover(modules)).
  150. pipe(favicon()).
  151. pipe(inline({
  152. base: htmlFolder,
  153. js: [function() { return remover(modules); }, compressJs],
  154. css: [crass, inlineImages],
  155. disabledTypes: ['svg', 'img']
  156. })).
  157. pipe(remover(modules)).
  158. pipe(htmlmin({
  159. collapseWhitespace: true,
  160. removeComments: true,
  161. minifyCSS: true,
  162. minifyJS: false
  163. })).
  164. pipe(replace('pure-', 'p-')).
  165. pipe(gzip({ gzipOptions: { level: 9 } })).
  166. pipe(rename('index.' + module + '.html.gz')).
  167. pipe(gulp.dest(dataFolder)).
  168. pipe(toHeader('webui_image', true)).
  169. pipe(gulp.dest(staticFolder));
  170. };
  171. // -----------------------------------------------------------------------------
  172. // Tasks
  173. // -----------------------------------------------------------------------------
  174. gulp.task('certs', function() {
  175. gulp.src(dataFolder + 'server.*').
  176. pipe(toHeader(debug=false)).
  177. pipe(gulp.dest(staticFolder));
  178. });
  179. gulp.task('csslint', function() {
  180. gulp.src(htmlFolder + '*.css').
  181. pipe(csslint({ids: false})).
  182. pipe(csslint.formatter());
  183. });
  184. gulp.task('webui_small', function() {
  185. return buildWebUI('small');
  186. });
  187. gulp.task('webui_sensor', function() {
  188. return buildWebUI('sensor');
  189. });
  190. gulp.task('webui_light', function() {
  191. return buildWebUI('light');
  192. });
  193. gulp.task('webui_rfbridge', function() {
  194. return buildWebUI('rfbridge');
  195. });
  196. gulp.task('webui_rfm69', function() {
  197. return buildWebUI('rfm69');
  198. });
  199. gulp.task('webui_lightfox', function() {
  200. return buildWebUI('lightfox');
  201. });
  202. gulp.task('webui_garland', function() {
  203. return buildWebUI('garland');
  204. });
  205. gulp.task('webui_thermostat', function() {
  206. return buildWebUI('thermostat');
  207. });
  208. gulp.task('webui_curtain', function() {
  209. return buildWebUI('curtain');
  210. });
  211. gulp.task('webui_all', function() {
  212. return buildWebUI('all');
  213. });
  214. gulp.task('webui',
  215. gulp.parallel(
  216. 'webui_small',
  217. 'webui_sensor',
  218. 'webui_light',
  219. 'webui_rfbridge',
  220. 'webui_rfm69',
  221. 'webui_lightfox',
  222. 'webui_garland',
  223. 'webui_thermostat',
  224. 'webui_curtain',
  225. 'webui_all'
  226. )
  227. );
  228. gulp.task('default', gulp.series('webui'));