Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/localdeck-configurator/src/server/api/editor.get.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
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';

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;
Expand All @@ -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 };
Expand Down
37 changes: 20 additions & 17 deletions packages/localdeck-configurator/src/server/api/index-files.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
});
Expand Down
Loading