From 9a498ce5c4563b892ed2ebfc161cc2e739c112dc Mon Sep 17 00:00:00 2001 From: ck <21735205+cyperdark@users.noreply.github.com> Date: Tue, 8 Oct 2024 04:43:16 +0300 Subject: [PATCH] chore: cleanup --- packages/common/index.ts | 1 - packages/common/utils/platforms.ts | 6 +- packages/common/utils/sanitize.ts | 5 - packages/server/assets/filesList.html | 56 -------- packages/server/assets/images/discord.svg | 1 - packages/server/assets/images/github.svg | 1 - packages/server/assets/images/link.svg | 16 --- packages/server/router/index.ts | 44 +++--- packages/server/utils/counters.ts | 160 +--------------------- packages/server/utils/homepage.ts | 76 ++++------ packages/server/utils/htmls.ts | 4 - packages/server/utils/parseSettings.ts | 43 +----- 12 files changed, 59 insertions(+), 354 deletions(-) delete mode 100644 packages/common/utils/sanitize.ts delete mode 100644 packages/server/assets/filesList.html delete mode 100644 packages/server/assets/images/discord.svg delete mode 100644 packages/server/assets/images/github.svg delete mode 100644 packages/server/assets/images/link.svg diff --git a/packages/common/index.ts b/packages/common/index.ts index 9fdccd77..d981b92a 100644 --- a/packages/common/index.ts +++ b/packages/common/index.ts @@ -7,5 +7,4 @@ export * from './utils/config'; export * from './utils/unzip'; export * from './utils/directories'; export * from './utils/json'; -export * from './utils/sanitize'; export * from './utils/gosu'; diff --git a/packages/common/utils/platforms.ts b/packages/common/utils/platforms.ts index aac38c7c..6bb26bd6 100644 --- a/packages/common/utils/platforms.ts +++ b/packages/common/utils/platforms.ts @@ -1,22 +1,26 @@ export const platformResolver = (platform: string) => { let platformType = ''; let platformFileType = ''; + let platformCommand = ''; switch (platform) { case 'win32': platformType = 'windows'; platformFileType = 'exe'; + platformCommand = 'start ""'; break; case 'linux': platformType = 'linux'; + platformCommand = 'xdg-open'; break; case 'darwin': platformType = 'macos'; platformFileType = 'macos'; + platformCommand = 'open -R'; break; } - return { platformType, platformFileType }; + return { platformType, platformFileType, platformCommand }; }; diff --git a/packages/common/utils/sanitize.ts b/packages/common/utils/sanitize.ts deleted file mode 100644 index fefa3401..00000000 --- a/packages/common/utils/sanitize.ts +++ /dev/null @@ -1,5 +0,0 @@ -export function sanitizeText(text: string) { - return text.replace(/[&<>"'\\/]/gim, function (i) { - return '&#' + i.charCodeAt(0) + ';'; - }); -} diff --git a/packages/server/assets/filesList.html b/packages/server/assets/filesList.html deleted file mode 100644 index 4e308fc9..00000000 --- a/packages/server/assets/filesList.html +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - {PAGE_URL} - - - - - - - - \ No newline at end of file diff --git a/packages/server/assets/images/discord.svg b/packages/server/assets/images/discord.svg deleted file mode 100644 index 7f9a31f0..00000000 --- a/packages/server/assets/images/discord.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/packages/server/assets/images/github.svg b/packages/server/assets/images/github.svg deleted file mode 100644 index d5e64918..00000000 --- a/packages/server/assets/images/github.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/packages/server/assets/images/link.svg b/packages/server/assets/images/link.svg deleted file mode 100644 index 9e6a3231..00000000 --- a/packages/server/assets/images/link.svg +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/packages/server/router/index.ts b/packages/server/router/index.ts index 575b9dc8..671a2938 100644 --- a/packages/server/router/index.ts +++ b/packages/server/router/index.ts @@ -3,6 +3,7 @@ import { getCachePath, getProgramPath, getStaticPath, + platformResolver, unzip, wLogger, writeConfig @@ -159,24 +160,12 @@ export default function buildBaseApi(server: Server) { }); } - let command; - switch (process.platform) { - case 'win32': - command = `start "" "${folderPath}"`; - break; - case 'linux': - command = `xdg-open "${folderPath}"`; - break; - case 'darwin': - command = `open -R "${folderPath}"`; - break; - } - wLogger.info( `PP Counter opened: ${folderName} (${req.headers.referer})` ); - exec(command, (err) => { + const { platformCommand } = platformResolver(process.platform); + exec(`${platformCommand} "${folderPath}"`, (err) => { if (err) { wLogger.error('Error opening file explorer:'); wLogger.debug(err); @@ -299,19 +288,24 @@ export default function buildBaseApi(server: Server) { 'dev/save', body as any ); + if (result instanceof Error) { + wLogger.debug(`${folderName}-settings-update`, result); - if (!(result instanceof Error)) { - wLogger.info( - `Settings re:created: ${folderName} (${req.headers.referer})` - ); - - fs.writeFileSync( - result.settingsPath, - JSON.stringify(result.settings), - 'utf8' - ); + return sendJson(res, { + error: result.name + }); } + wLogger.info( + `Settings re:created: ${folderName} (${req.headers.referer})` + ); + + fs.writeFileSync( + result.settingsPath!, + JSON.stringify(result.settings), + 'utf8' + ); + return sendJson(res, { result: 'success' }); } @@ -321,7 +315,7 @@ export default function buildBaseApi(server: Server) { const html = saveSettings(folderName, body as any); if (html instanceof Error) { - wLogger.debug(`counter-${folderName}-settings-save`, html); + wLogger.debug(`${folderName}-settings-save`, html); return sendJson(res, { error: html.name diff --git a/packages/server/utils/counters.ts b/packages/server/utils/counters.ts index 2bf2a335..7d5e5d7f 100644 --- a/packages/server/utils/counters.ts +++ b/packages/server/utils/counters.ts @@ -3,7 +3,6 @@ import { config, getStaticPath, recursiveFilesSearch, - sanitizeText, wLogger } from '@tosu/common'; import fs from 'fs'; @@ -12,7 +11,7 @@ import path from 'path'; import semver from 'semver'; import { getContentType } from '../utils'; -import { ICounter, ISettings, bodyPayload } from './counters.types'; +import { ICounter, bodyPayload } from './counters.types'; import { authorHTML, authorLinksHTML, @@ -28,10 +27,8 @@ import { noMoreCounters, resultItemHTML, saveSettingsButtonHTML, - selectHTML, settingsItemHTML, - submitCounterHTML, - textareaHTML + submitCounterHTML } from './htmls'; import { parseCounterSettings } from './parseSettings'; @@ -115,157 +112,6 @@ export function parseTXT(filePath: string) { return object; } -export function createSetting(setting: ISettings, value: any) { - const title = sanitizeText(setting.title); - const description = sanitizeText(setting.description); - - switch (setting.type) { - case 'text': { - return settingsItemHTML - .replace('{CLASSES}', '') - .replace('{NAME}', title) - .replace('{DESCRIPTION}', description) - .replace( - '{INPUT}', - inputHTML - .replace('{TYPE}', 'text') - .replace(/{ID}/gm, setting.uniqueID) - .replace('{ADDON}', `ucs t="${setting.type}"`) - .replace('{VALUE}', value) - ); - } - - case 'number': { - return settingsItemHTML - .replace('{CLASSES}', '') - .replace('{NAME}', title) - .replace('{DESCRIPTION}', description) - .replace( - '{INPUT}', - inputHTML - .replace('{TYPE}', 'number') - .replace(/{ID}/gm, setting.uniqueID) - .replace('{ADDON}', `ucs t="${setting.type}"`) - .replace('{VALUE}', value) - ); - } - - case 'password': { - return settingsItemHTML - .replace('{CLASSES}', '') - .replace('{NAME}', title) - .replace('{DESCRIPTION}', description) - .replace( - '{INPUT}', - inputHTML - .replace('{TYPE}', 'password') - .replace(/{ID}/gm, setting.uniqueID) - .replace('{ADDON}', `ucs t="${setting.type}"`) - .replace('{VALUE}', value) - ); - } - - case 'checkbox': { - return settingsItemHTML - .replace('{CLASSES}', '') - .replace('{NAME}', title) - .replace('{DESCRIPTION}', description) - .replace( - '{INPUT}', - checkboxHTML - .replace('{TYPE}', 'text') - .replace(/{ID}/gm, setting.uniqueID) - .replace( - '{ADDON}', - value - ? `ucs t="${setting.type}" checked="true"` - : `ucs t="${setting.type}"` - ) - .replace('{VALUE}', `${value}`) - ); - } - - case 'color': { - return settingsItemHTML - .replace('{CLASSES}', '') - .replace('{NAME}', title) - .replace('{DESCRIPTION}', description) - .replace( - '{INPUT}', - inputHTML - .replace('{TYPE}', 'color') - .replace(/{ID}/gm, setting.uniqueID) - .replace('{ADDON}', `ucs t="${setting.type}"`) - .replace('{VALUE}', value) - ); - } - - case 'options': { - const options = Array.isArray(setting.options) - ? setting.options - .filter((r) => r) - .map( - (r) => - `` - ) - .join('\n') - : ''; - return settingsItemHTML - .replace('{CLASSES}', '') - .replace('{NAME}', title) - .replace('{DESCRIPTION}', description) - .replace( - '{INPUT}', - selectHTML - .replace(/{ID}/gm, setting.uniqueID) - .replace('{ADDON}', `ucs t="${setting.type}"`) - .replace('{OPTIONS}', options) - ); - } - - case 'textarea': { - return settingsItemHTML - .replace('{CLASSES}', 'txt-area') - .replace('{NAME}', title) - .replace('{DESCRIPTION}', description) - .replace( - '{INPUT}', - textareaHTML - .replace('{TYPE}', 'text') - .replace(/{ID}/gm, setting.uniqueID) - .replace('{ADDON}', `ucs t="${setting.type}"`) - .replace('{VALUE}', value) - ); - } - } - - return ''; -} - -export function parseSettings( - settings: ISettings[], - folderName: string -): string | Error { - let html = `

Settings«${decodeURI(folderName)}»

`; - for (let i = 0; i < settings.length; i++) { - const setting = settings[i]; - - if (setting.uniqueID === undefined || setting.uniqueID === null) { - continue; - } - - html += createSetting(setting, setting.value); - } - - html += '
'; // close scroll div - - html += `
- - -
`; - return html; -} - export function saveSettings(folderName: string, payload: bodyPayload[]) { const result = parseCounterSettings( folderName, @@ -277,7 +123,7 @@ export function saveSettings(folderName: string, payload: bodyPayload[]) { } fs.writeFileSync( - result.settingsValuesPath, + result.settingsValuesPath!, JSON.stringify(result.values), 'utf8' ); diff --git a/packages/server/utils/homepage.ts b/packages/server/utils/homepage.ts index eb0752a0..4f3a7762 100644 --- a/packages/server/utils/homepage.ts +++ b/packages/server/utils/homepage.ts @@ -1,76 +1,58 @@ export const OVERLAYS_STATIC = ` - - - - + {PAGE_URL} + + + - - - - - + + + `; diff --git a/packages/server/utils/htmls.ts b/packages/server/utils/htmls.ts index 382bb9d8..ca4428b5 100644 --- a/packages/server/utils/htmls.ts +++ b/packages/server/utils/htmls.ts @@ -60,10 +60,6 @@ export const checkboxHTML = ` export const inputHTML = ''; -export const textareaHTML = ''; - -export const selectHTML = ``; - export const saveSettingsButtonHTML = '
'; diff --git a/packages/server/utils/parseSettings.ts b/packages/server/utils/parseSettings.ts index c7098881..acabd900 100644 --- a/packages/server/utils/parseSettings.ts +++ b/packages/server/utils/parseSettings.ts @@ -56,38 +56,9 @@ export function parseCounterSettings( switch (action) { case 'parse': - for (let i = 0; i < settings.length; i++) { - const setting = settings[i]; - - const value = values[setting.uniqueID] ?? setting.value; - switch (setting.type) { - case 'number': { - setting.value = isNaN(value) ? 0 : +value; - break; - } - - case 'checkbox': { - if (typeof value === 'string') { - setting.value = value === 'true'; - break; - } - - setting.value = value; - break; - } - - default: { - setting.value = value; - break; - } - } - } - return { settings, - values, - settingsPath, - settingsValuesPath + values }; case 'user/save': @@ -135,9 +106,7 @@ export function parseCounterSettings( } return { - settings, values, - settingsPath, settingsValuesPath }; @@ -170,12 +139,7 @@ export function parseCounterSettings( } } - return { - settings, - values, - settingsPath, - settingsValuesPath - }; + return { values }; case 'dev/save': { if (!Array.isArray(payload)) { @@ -215,8 +179,7 @@ export function parseCounterSettings( return { settings: payload, - settingsPath, - settingsValuesPath + settingsPath }; } }