diff --git a/packages/localdeck-configurator/src/server/api/editor.get.ts b/packages/localdeck-configurator/src/server/api/editor.get.ts index 0db749a..20aa749 100644 --- a/packages/localdeck-configurator/src/server/api/editor.get.ts +++ b/packages/localdeck-configurator/src/server/api/editor.get.ts @@ -1,5 +1,4 @@ -import * as fs from 'node:fs'; -import * as readline from 'node:readline'; +import * as fs from 'node:fs/promises'; import { decompress } from '@localbytes/localdeck-components/src/utils/compression'; import { zPadEditor } from '@localbytes/localdeck-components/src/utils/PadCfg'; @@ -7,17 +6,16 @@ import { zPadEditor } from '@localbytes/localdeck-components/src/utils/PadCfg'; export default defineEventHandler(async (event) => { const { filesDir } = useRuntimeConfig(); const { filename } = getQuery(event); - const content = fs.createReadStream(`${filesDir}/${filename}`, 'utf8'); - const rl = readline.createInterface({ input: content, crlfDelay: Infinity }); + const file = await fs.open(`${filesDir}/${filename}`, 'r'); let configMatch = null; let matchName = null; let matchFriendly = null; - for await (const line of rl) { + for await (const line of file.readLines()) { configMatch ??= line.match(/localdeck-configurator\?config=(.*)/); - matchName ??= line.match(/name: "?(.*)"?/); - matchFriendly ??= line.match(/friendly_name: "?(.*)"?/); + matchName ??= line.match(/name: ?("?)(.*)\1/); + matchFriendly ??= line.match(/friendly_name: ?("?)(.*)\1/); // noinspection PointlessBooleanExpressionJS if (configMatch) break; @@ -29,7 +27,7 @@ export default defineEventHandler(async (event) => { ? decompress(configStr, zPadEditor) : zPadEditor.parse({}); - config.title = matchFriendly?.[1] ?? matchName?.[1] ?? config.title ?? 'My LocalDeck'; + config.title = matchFriendly?.[2] ?? matchName?.[2] ?? config.title ?? 'My LocalDeck'; config.buttons ??= {}; return { configStr, config }; diff --git a/packages/localdeck-configurator/src/server/api/index-files.ts b/packages/localdeck-configurator/src/server/api/index-files.ts index ae41e4a..95666b5 100644 --- a/packages/localdeck-configurator/src/server/api/index-files.ts +++ b/packages/localdeck-configurator/src/server/api/index-files.ts @@ -1,4 +1,4 @@ -import * as fs from 'fs/promises'; +import * as fs from 'node:fs/promises'; import _ from 'lodash'; import { FileType, type IndexFile } from '~/utilities/types'; @@ -17,24 +17,27 @@ export default defineEventHandler(async (event) => { .filter(filename => filename.endsWith('.yaml') || filename.endsWith('.yml')) .map(async (filename) => { const path = `${filesDir}/${filename}`; - const content = await fs.readFile(path, 'utf8'); - let name = filename - .replace('.yaml', '') - .replace('.yml', ''); + const fileHandle = await fs.open(path, 'r'); let type = FileType.Other; - - if ( - content.includes('packages:') - && content.includes('localbytes.localdeck') - ) type = FileType.Import; - - if (content.includes('localdeck-configurator?config=')) type = FileType.LocalDeck; - - const matchName = content.match(/name: (.*)/); - if (matchName) name = matchName[1]; - const matchFriendly = content.match(/friendly_name: (.*)/); - if (matchFriendly) name = matchFriendly[1]; + let matchConfig: RegExpMatchArray | null = null; + let matchName: RegExpMatchArray | null = null; + let matchFriendly: RegExpMatchArray | null = null; + let matchPackage: RegExpMatchArray | null = null; + + for await (const line of fileHandle.readLines()) { + matchConfig ??= line.match(/localdeck-configurator\?config=(.*)/); + matchName ??= line.match(/name: ?("?)(.*)\1/); + matchFriendly ??= line.match(/friendly_name: ?("?)(.*)\1/); + matchPackage ??= line.match(/localbytes.localdeck: github:\/\//); + + // noinspection PointlessBooleanExpressionJS + if (matchConfig && matchName && matchFriendly && matchPackage) break; + } + + const name = matchFriendly?.[2] ?? matchName?.[2] ?? (filename.replace(/\.ya?ml/, '')); + if (matchPackage) type = FileType.Import; + if (matchConfig) type = FileType.LocalDeck; return { path, filename, type, name } satisfies IndexFile; });