mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
eslint
This commit is contained in:
@@ -1,37 +1,37 @@
|
||||
const { resolve, relative, sep } = require('path')
|
||||
const { resolve, relative, sep } = require('path');
|
||||
const {
|
||||
readdirSync,
|
||||
existsSync,
|
||||
unlinkSync,
|
||||
readFileSync,
|
||||
createWriteStream,
|
||||
} = require('fs')
|
||||
var zlib = require('zlib')
|
||||
var mime = require('mime-types')
|
||||
createWriteStream
|
||||
} = require('fs');
|
||||
var zlib = require('zlib');
|
||||
var mime = require('mime-types');
|
||||
|
||||
const ARDUINO_INCLUDES = '#include <Arduino.h>\n\n'
|
||||
const ARDUINO_INCLUDES = '#include <Arduino.h>\n\n';
|
||||
|
||||
function getFilesSync(dir, files = []) {
|
||||
readdirSync(dir, { withFileTypes: true }).forEach((entry) => {
|
||||
const entryPath = resolve(dir, entry.name)
|
||||
const entryPath = resolve(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
getFilesSync(entryPath, files)
|
||||
getFilesSync(entryPath, files);
|
||||
} else {
|
||||
files.push(entryPath)
|
||||
files.push(entryPath);
|
||||
}
|
||||
})
|
||||
return files
|
||||
});
|
||||
return files;
|
||||
}
|
||||
|
||||
function coherseToBuffer(input) {
|
||||
return Buffer.isBuffer(input) ? input : Buffer.from(input)
|
||||
return Buffer.isBuffer(input) ? input : Buffer.from(input);
|
||||
}
|
||||
|
||||
function cleanAndOpen(path) {
|
||||
if (existsSync(path)) {
|
||||
unlinkSync(path)
|
||||
unlinkSync(path);
|
||||
}
|
||||
return createWriteStream(path, { flags: 'w+' })
|
||||
return createWriteStream(path, { flags: 'w+' });
|
||||
}
|
||||
|
||||
class ProgmemGenerator {
|
||||
@@ -40,70 +40,70 @@ class ProgmemGenerator {
|
||||
outputPath,
|
||||
bytesPerLine = 20,
|
||||
indent = ' ',
|
||||
includes = ARDUINO_INCLUDES,
|
||||
} = options
|
||||
this.options = { outputPath, bytesPerLine, indent, includes }
|
||||
includes = ARDUINO_INCLUDES
|
||||
} = options;
|
||||
this.options = { outputPath, bytesPerLine, indent, includes };
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
compiler.hooks.emit.tapAsync(
|
||||
{ name: 'ProgmemGenerator' },
|
||||
(compilation, callback) => {
|
||||
const { outputPath, bytesPerLine, indent, includes } = this.options
|
||||
const fileInfo = []
|
||||
const { outputPath, bytesPerLine, indent, includes } = this.options;
|
||||
const fileInfo = [];
|
||||
const writeStream = cleanAndOpen(
|
||||
resolve(compilation.options.context, outputPath),
|
||||
)
|
||||
resolve(compilation.options.context, outputPath)
|
||||
);
|
||||
try {
|
||||
const writeIncludes = () => {
|
||||
writeStream.write(includes)
|
||||
}
|
||||
writeStream.write(includes);
|
||||
};
|
||||
|
||||
const writeFile = (relativeFilePath, buffer) => {
|
||||
const variable = 'ESP_REACT_DATA_' + fileInfo.length
|
||||
const mimeType = mime.lookup(relativeFilePath)
|
||||
var size = 0
|
||||
writeStream.write('const uint8_t ' + variable + '[] PROGMEM = {')
|
||||
const zipBuffer = zlib.gzipSync(buffer)
|
||||
const variable = 'ESP_REACT_DATA_' + fileInfo.length;
|
||||
const mimeType = mime.lookup(relativeFilePath);
|
||||
var size = 0;
|
||||
writeStream.write('const uint8_t ' + variable + '[] PROGMEM = {');
|
||||
const zipBuffer = zlib.gzipSync(buffer);
|
||||
zipBuffer.forEach((b) => {
|
||||
if (!(size % bytesPerLine)) {
|
||||
writeStream.write('\n')
|
||||
writeStream.write(indent)
|
||||
writeStream.write('\n');
|
||||
writeStream.write(indent);
|
||||
}
|
||||
writeStream.write(
|
||||
'0x' + ('00' + b.toString(16).toUpperCase()).substr(-2) + ',',
|
||||
)
|
||||
size++
|
||||
})
|
||||
'0x' + ('00' + b.toString(16).toUpperCase()).substr(-2) + ','
|
||||
);
|
||||
size++;
|
||||
});
|
||||
if (size % bytesPerLine) {
|
||||
writeStream.write('\n')
|
||||
writeStream.write('\n');
|
||||
}
|
||||
writeStream.write('};\n\n')
|
||||
writeStream.write('};\n\n');
|
||||
fileInfo.push({
|
||||
uri: '/' + relativeFilePath.replace(sep, '/'),
|
||||
mimeType,
|
||||
variable,
|
||||
size,
|
||||
})
|
||||
}
|
||||
size
|
||||
});
|
||||
};
|
||||
|
||||
const writeFiles = () => {
|
||||
// process static files
|
||||
const buildPath = compilation.options.output.path
|
||||
const buildPath = compilation.options.output.path;
|
||||
for (const filePath of getFilesSync(buildPath)) {
|
||||
const readStream = readFileSync(filePath)
|
||||
const relativeFilePath = relative(buildPath, filePath)
|
||||
writeFile(relativeFilePath, readStream)
|
||||
const readStream = readFileSync(filePath);
|
||||
const relativeFilePath = relative(buildPath, filePath);
|
||||
writeFile(relativeFilePath, readStream);
|
||||
}
|
||||
// process assets
|
||||
const { assets } = compilation
|
||||
const { assets } = compilation;
|
||||
Object.keys(assets).forEach((relativeFilePath) => {
|
||||
writeFile(
|
||||
relativeFilePath,
|
||||
coherseToBuffer(assets[relativeFilePath].source()),
|
||||
)
|
||||
})
|
||||
}
|
||||
coherseToBuffer(assets[relativeFilePath].source())
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const generateWWWClass = () => {
|
||||
return `typedef std::function<void(const String& uri, const String& contentType, const uint8_t * content, size_t len)> RouteRegistrationHandler;
|
||||
@@ -111,38 +111,38 @@ class ProgmemGenerator {
|
||||
class WWWData {
|
||||
${indent}public:
|
||||
${indent.repeat(
|
||||
2,
|
||||
2
|
||||
)}static void registerRoutes(RouteRegistrationHandler handler) {
|
||||
${fileInfo
|
||||
.map(
|
||||
(file) =>
|
||||
`${indent.repeat(3)}handler("${file.uri}", "${file.mimeType}", ${
|
||||
file.variable
|
||||
}, ${file.size});`,
|
||||
}, ${file.size});`
|
||||
)
|
||||
.join('\n')}
|
||||
${indent.repeat(2)}}
|
||||
};
|
||||
`
|
||||
}
|
||||
`;
|
||||
};
|
||||
|
||||
const writeWWWClass = () => {
|
||||
writeStream.write(generateWWWClass())
|
||||
}
|
||||
writeStream.write(generateWWWClass());
|
||||
};
|
||||
|
||||
writeIncludes()
|
||||
writeFiles()
|
||||
writeWWWClass()
|
||||
writeIncludes();
|
||||
writeFiles();
|
||||
writeWWWClass();
|
||||
|
||||
writeStream.on('finish', () => {
|
||||
callback()
|
||||
})
|
||||
callback();
|
||||
});
|
||||
} finally {
|
||||
writeStream.end()
|
||||
writeStream.end();
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ProgmemGenerator
|
||||
module.exports = ProgmemGenerator;
|
||||
|
||||
Reference in New Issue
Block a user