Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark committed Oct 11, 2024
1 parent 1c76749 commit 9a498ce
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 354 deletions.
1 change: 0 additions & 1 deletion packages/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
6 changes: 5 additions & 1 deletion packages/common/utils/platforms.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
5 changes: 0 additions & 5 deletions packages/common/utils/sanitize.ts

This file was deleted.

56 changes: 0 additions & 56 deletions packages/server/assets/filesList.html

This file was deleted.

1 change: 0 additions & 1 deletion packages/server/assets/images/discord.svg

This file was deleted.

1 change: 0 additions & 1 deletion packages/server/assets/images/github.svg

This file was deleted.

16 changes: 0 additions & 16 deletions packages/server/assets/images/link.svg

This file was deleted.

44 changes: 19 additions & 25 deletions packages/server/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getCachePath,
getProgramPath,
getStaticPath,
platformResolver,
unzip,
wLogger,
writeConfig
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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' });
}

Expand All @@ -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
Expand Down
160 changes: 3 additions & 157 deletions packages/server/utils/counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
config,
getStaticPath,
recursiveFilesSearch,
sanitizeText,
wLogger
} from '@tosu/common';
import fs from 'fs';
Expand All @@ -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,
Expand All @@ -28,10 +27,8 @@ import {
noMoreCounters,
resultItemHTML,
saveSettingsButtonHTML,
selectHTML,
settingsItemHTML,
submitCounterHTML,
textareaHTML
submitCounterHTML
} from './htmls';
import { parseCounterSettings } from './parseSettings';

Expand Down Expand Up @@ -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) =>
`<option ${(value || setting.value) === r ? 'selected="selected"' : ''} value="${r}">${r}</option>`
)
.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 = `<h2 class="ms-title"><span>Settings</span><span>«${decodeURI(folderName)}»</span></h2><div class="m-scroll">`;
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 += '</div>'; // close scroll div

html += `<div class="ms-btns flexer si-btn">
<button class="button update-settings-button flexer" n="${decodeURI(folderName)}"><span>Update settings</span></button>
<button class="button cancel-button flexer"><span>Cancel</span></button>
</div>`;
return html;
}

export function saveSettings(folderName: string, payload: bodyPayload[]) {
const result = parseCounterSettings(
folderName,
Expand All @@ -277,7 +123,7 @@ export function saveSettings(folderName: string, payload: bodyPayload[]) {
}

fs.writeFileSync(
result.settingsValuesPath,
result.settingsValuesPath!,
JSON.stringify(result.values),
'utf8'
);
Expand Down
Loading

0 comments on commit 9a498ce

Please sign in to comment.