Skip to content

Commit 71783a0

Browse files
authored
Merge pull request #53 from LocalBytes/feat/perf
feat/perf
2 parents be32b74 + 62ed835 commit 71783a0

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

packages/localdeck-configurator/src/server/api/editor.get.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import * as fs from 'node:fs';
2-
import * as readline from 'node:readline';
1+
import * as fs from 'node:fs/promises';
32

43
import { decompress } from '@localbytes/localdeck-components/src/utils/compression';
54
import { zPadEditor } from '@localbytes/localdeck-components/src/utils/PadCfg';
65

76
export default defineEventHandler(async (event) => {
87
const { filesDir } = useRuntimeConfig();
98
const { filename } = getQuery(event);
10-
const content = fs.createReadStream(`${filesDir}/${filename}`, 'utf8');
11-
const rl = readline.createInterface({ input: content, crlfDelay: Infinity });
9+
const file = await fs.open(`${filesDir}/${filename}`, 'r');
1210

1311
let configMatch = null;
1412
let matchName = null;
1513
let matchFriendly = null;
1614

17-
for await (const line of rl) {
15+
for await (const line of file.readLines()) {
1816
configMatch ??= line.match(/localdeck-configurator\?config=(.*)/);
19-
matchName ??= line.match(/name: "?(.*)"?/);
20-
matchFriendly ??= line.match(/friendly_name: "?(.*)"?/);
17+
matchName ??= line.match(/name: ?("?)(.*)\1/);
18+
matchFriendly ??= line.match(/friendly_name: ?("?)(.*)\1/);
2119

2220
// noinspection PointlessBooleanExpressionJS
2321
if (configMatch) break;
@@ -29,7 +27,7 @@ export default defineEventHandler(async (event) => {
2927
? decompress(configStr, zPadEditor)
3028
: zPadEditor.parse({});
3129

32-
config.title = matchFriendly?.[1] ?? matchName?.[1] ?? config.title ?? 'My LocalDeck';
30+
config.title = matchFriendly?.[2] ?? matchName?.[2] ?? config.title ?? 'My LocalDeck';
3331
config.buttons ??= {};
3432

3533
return { configStr, config };

packages/localdeck-configurator/src/server/api/index-files.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from 'fs/promises';
1+
import * as fs from 'node:fs/promises';
22
import _ from 'lodash';
33
import { FileType, type IndexFile } from '~/utilities/types';
44

@@ -17,24 +17,27 @@ export default defineEventHandler(async (event) => {
1717
.filter(filename => filename.endsWith('.yaml') || filename.endsWith('.yml'))
1818
.map(async (filename) => {
1919
const path = `${filesDir}/${filename}`;
20-
const content = await fs.readFile(path, 'utf8');
21-
let name = filename
22-
.replace('.yaml', '')
23-
.replace('.yml', '');
20+
const fileHandle = await fs.open(path, 'r');
2421

2522
let type = FileType.Other;
26-
27-
if (
28-
content.includes('packages:')
29-
&& content.includes('localbytes.localdeck')
30-
) type = FileType.Import;
31-
32-
if (content.includes('localdeck-configurator?config=')) type = FileType.LocalDeck;
33-
34-
const matchName = content.match(/name: (.*)/);
35-
if (matchName) name = matchName[1];
36-
const matchFriendly = content.match(/friendly_name: (.*)/);
37-
if (matchFriendly) name = matchFriendly[1];
23+
let matchConfig: RegExpMatchArray | null = null;
24+
let matchName: RegExpMatchArray | null = null;
25+
let matchFriendly: RegExpMatchArray | null = null;
26+
let matchPackage: RegExpMatchArray | null = null;
27+
28+
for await (const line of fileHandle.readLines()) {
29+
matchConfig ??= line.match(/localdeck-configurator\?config=(.*)/);
30+
matchName ??= line.match(/name: ?("?)(.*)\1/);
31+
matchFriendly ??= line.match(/friendly_name: ?("?)(.*)\1/);
32+
matchPackage ??= line.match(/localbytes.localdeck: github:\/\//);
33+
34+
// noinspection PointlessBooleanExpressionJS
35+
if (matchConfig && matchName && matchFriendly && matchPackage) break;
36+
}
37+
38+
const name = matchFriendly?.[2] ?? matchName?.[2] ?? (filename.replace(/\.ya?ml/, ''));
39+
if (matchPackage) type = FileType.Import;
40+
if (matchConfig) type = FileType.LocalDeck;
3841

3942
return { path, filename, type, name } satisfies IndexFile;
4043
});

0 commit comments

Comments
 (0)