weatherstation/firmware/libraries/WiFiManager/extras/parse.js
Aaron Fischer f24858d162 Move all dependencies to the repository
This step seems bold, but is saves us so much hassle. Even better, we have a
reliable codebase, with all the dependencies (and their versions) we
need in order to build the project. If a library got an update, we can
replace it inplace if the code is still compatible.
2019-02-03 16:15:00 +01:00

60 lines
1.7 KiB
JavaScript

'use strict';
const fs = require('fs');
console.log('starting');
const inFile = 'WiFiManager.template.html';
const outFile = 'template.h';
const defineRegEx = /<!-- ([A-Z_]+) -->/gm;
console.log('parsing', inFile);
fs.readFile(inFile, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
//console.log(data);
let defines = data.match(defineRegEx);
//console.log(defines);
var stream = fs.createWriteStream(outFile);
stream.once('open', function(fd) {
for (const i in defines) {
const start = defines[i];
const end = start.replace('<!-- ', '<!-- /')
defineRegEx.lastIndex = 0;
const constantName = defineRegEx.exec(start)[1];
console.log(constantName);
var extractRE = new RegExp(start + '([\\s\\S]+)' + end, 'gm');
let extractArray = extractRE.exec(data);
if(extractArray.length > 1) {
let def = extractArray[1];
//console.log(def);
//minimise a bit
def = def.replace(/\s+/g, ' ');
def = def.replace(/>\s+</g, '><');
def = def.trim();
//more extraneous spaces - possible bad results, needs to be checked
//def = def.replace(/(\w)\s(\W)|(\W)\s(\w)|(\W)\s(\W)/g, '$1$2$3$4$5$6');
def = def.replace(/(\w)\s(\W)|(\W)\s(\w)/g, '$1$2$3$4');
//escape double quotes
def = def.replace(/\\([\s\S])|(")/g, "\\$1$2");
console.log(def);
//const char HTTP_HEAD[] PROGMEM =
let string = 'const char ' + constantName + '[] PROGMEM';
for (let i = string.length; i < 42; i++) {
string += ' ';
}
string += '= "' + def + '";\n';
stream.write(string);
}
}
stream.end();
});
});