Skip to content

Commit

Permalink
feat: filecount by extension
Browse files Browse the repository at this point in the history
  • Loading branch information
BCsabaEngine committed Aug 14, 2024
1 parent 90a8d30 commit aa8f5d3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
9 changes: 8 additions & 1 deletion demo/esp32/include/svelteesp32async.h

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion demo/esp32/include/svelteesp32psychic.h

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions demo/esp32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#error Missing index file
#endif

#if SVELTEESP32_CSS_FILES > 1
#error Too much CSS files
#endif

AsyncWebServer server(80);
void setup()
{
Expand All @@ -34,6 +38,10 @@ void loop() {}
#error Missing index file
#endif

#if SVELTEESP32_CSS_FILES > 1
#error Too much CSS files
#endif

PsychicHttpServer server;
void setup()
{
Expand Down
22 changes: 20 additions & 2 deletions src/cppCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { compile as handlebarsCompile } from 'handlebars';

import { cmdLine } from './commandLine';

export type cppCodeSource = {
export type CppCodeSource = {
filename: string;
dataname: string;
datanameUpperCase: string;
Expand All @@ -11,6 +11,13 @@ export type cppCodeSource = {
isGzip: boolean;
md5: string;
};
export type CppCodeSources = CppCodeSource[];

export type ExtensionGroup = {
extension: string;
count: number;
};
export type ExtensionGroups = ExtensionGroup[];

const psychicTemplate = `
//engine: PsychicHttpServer
Expand All @@ -25,10 +32,15 @@ const psychicTemplate = `
#define {{definePrefix}}_COUNT {{fileCount}}
#define {{definePrefix}}_SIZE {{fileSize}}
{{#each sources}}
#define {{../definePrefix}}_FILE_{{this.datanameUpperCase}}
{{/each}}
{{#each filesByExtension}}
#define {{../definePrefix}}_{{this.extension}}_FILES {{this.count}}
{{/each}}
{{#each sources}}
const uint8_t data_{{this.dataname}}[{{this.length}}] = { {{this.bytes}} };
{{#if ../isEtag}}
Expand Down Expand Up @@ -73,10 +85,15 @@ const asyncTemplate = `
#define {{definePrefix}}_COUNT {{fileCount}}
#define {{definePrefix}}_SIZE {{fileSize}}
{{#each sources}}
#define {{../definePrefix}}_FILE_{{this.datanameUpperCase}}
{{/each}}
{{#each filesByExtension}}
#define {{../definePrefix}}_{{this.extension}}_FILES {{this.count}}
{{/each}}
{{#each sources}}
const uint8_t data_{{this.dataname}}[{{this.length}}] PROGMEM = { {{this.bytes}} };
{{#if ../isEtag}}
Expand Down Expand Up @@ -110,7 +127,7 @@ void {{methodName}}(AsyncWebServer * server) {
{{/each}}
}`;

export const getCppCode = (sources: cppCodeSource[]): string =>
export const getCppCode = (sources: CppCodeSources, filesByExtension: ExtensionGroups): string =>
handlebarsCompile(cmdLine.engine === 'psychic' ? psychicTemplate : asyncTemplate)({
commandLine: process.argv.slice(2).join(' '),
now: `${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}`,
Expand All @@ -122,6 +139,7 @@ export const getCppCode = (sources: cppCodeSource[]): string =>
bytes: [...s.content].map((v) => `0x${v.toString(16)}`).join(', '),
isDefault: s.filename.startsWith('index.htm')
})),
filesByExtension,
isEtag: cmdLine.etag,
methodName: cmdLine.espmethod,
definePrefix: cmdLine.define
Expand Down
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { gzipSync } from 'node:zlib';
import { lookup } from 'mime-types';

import { cmdLine } from './commandLine';
import { cppCodeSource, getCppCode } from './cppCode';
import { CppCodeSources, ExtensionGroups, getCppCode } from './cppCode';
import { getFiles } from './file';

const summary = {
Expand All @@ -16,7 +16,8 @@ const summary = {
gzipsize: 0
};

const sources: cppCodeSource[] = [];
const sources: CppCodeSources = [];
const filesByExtension: ExtensionGroups = [];
const files = getFiles();
if (files.length === 0) {
console.error(`Directory ${cmdLine.sourcepath} is empty`);
Expand All @@ -30,6 +31,12 @@ for (const file of files) {

const filename = file.replace(/\\/g, '/');
const dataname = filename.replace(/[./-]/g, '_');
let extension = path.extname(filename).toUpperCase();
if (extension.startsWith('.')) extension = extension.slice(1);

const group = filesByExtension.find((fe) => fe.extension === extension);
if (group) group.count += 1;
else filesByExtension.push({ extension, count: 1 });

const rawContent = readFileSync(path.join(cmdLine.sourcepath, file), { flag: 'r' });
const md5 = createHash('md5').update(rawContent).digest('hex');
Expand Down Expand Up @@ -75,8 +82,9 @@ for (const file of files) {

console.log('');
}
filesByExtension.sort((left, right) => left.extension.localeCompare(right.extension));

const cppFile = getCppCode(sources);
const cppFile = getCppCode(sources, filesByExtension);
writeFileSync(cmdLine.outputfile, cppFile);

if (cmdLine['no-gzip']) {
Expand Down

0 comments on commit aa8f5d3

Please sign in to comment.