From d1235291f5ec9944414fa8176b3d367179d8e22b Mon Sep 17 00:00:00 2001 From: RetoRuto9900K Date: Wed, 4 Dec 2024 22:13:21 +0900 Subject: [PATCH] feat: add execute permission --- src/updater/Installer.ts | 31 ++++++++++++++++++++++++------- src/updater/Merge.ts | 4 ++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/updater/Installer.ts b/src/updater/Installer.ts index 89ba5d9..d3f2de7 100644 --- a/src/updater/Installer.ts +++ b/src/updater/Installer.ts @@ -14,7 +14,7 @@ const KEEP_ITEMS: [string, MergeInfo][] = [ ['permissions.json', {}], ['whitelist.json', {}], ['server.properties', serverPropertiesMerger], - ['config/default/permissions.json', permissionsJsonMerger] // TODO: merge array + ['config/default/permissions.json', permissionsJsonMerger] ]; // normalize file paths KEEP_ITEMS.forEach(item => item[0] = path.normalize(item[0])); @@ -28,6 +28,13 @@ export class Installer { console.log('Downloading bedrock server: Done'); await installer.updateFiles(); console.log('Updating files: Done'); + + if (process.platform !== 'win32') { + const bedrockServer = path.join(serverFolder, 'bedrock_server'); + const currentMode = (await fs.stat(bedrockServer)).mode; + await fs.chmod(bedrockServer, currentMode | fs.constants.S_IXUSR); + console.log('Added execute permission to bedrock_server'); + } } async downloadAndExtractServer(version: VersionInfo): Promise { @@ -67,12 +74,13 @@ export class Installer { async scanDir(base: string, paths: string = '') { const basePath = path.join(base, paths); + const promises: Promise[] = []; for (const item of await fs.readdir(basePath, { withFileTypes: true })) { const relPath = path.join(paths, item.name); const newPath = path.join(base, relPath) const ITEM = KEEP_ITEMS.find(([p]) => p.startsWith(relPath)); // true: keep - let result: string | Buffer | true | undefined; + let result: true | undefined | Promise; if (ITEM) { result = true; const info = ITEM[1]; @@ -80,7 +88,7 @@ export class Installer { this.scanDir(base, relPath); continue; } else { - if (info.onFile) result = await info.onFile(newPath) + if (info.onFile) result = info.onFile(newPath); } } const oldPath = path.join(serverFolder, paths, item.name); @@ -88,16 +96,25 @@ export class Installer { const exists = await fs.exists(oldPath); if (result === undefined || !exists) { // replace - console.log(' REPLACE:', item.name); - await safeRename(newPath, oldPath); + promises.push( + safeRename(newPath, oldPath) + .then(() => console.log(' REPLACE:', item.name)) + .catch(err => console.error(` ERROR: Failed to replace ${item.name}:`, err)) + ); } else if (result === true) { // keep console.log(' KEEP:', ITEM![0]); } else { // merge - console.log(' MERGE:', ITEM![0]); - await fs.writeFile(oldPath, result); + promises.push( + result + .then(value => fs.writeFile(oldPath, value)) + .then(() => console.log(' MERGE:', ITEM![0])) + .catch(err => console.error(` ERROR: Failed to merge ${item.name}:`, err)) + ); } } + + await Promise.all(promises); } } diff --git a/src/updater/Merge.ts b/src/updater/Merge.ts index 55a390e..ceb0cb5 100644 --- a/src/updater/Merge.ts +++ b/src/updater/Merge.ts @@ -4,7 +4,7 @@ import { jsonc } from 'jsonc'; import { serverFolder } from './CacheManager'; export interface MergeInfo { - onFile?: (path: string) => Promise; + onFile?: (path: string) => Promise; //onDirectory?: (path: string) => Promise; } @@ -52,6 +52,6 @@ export const permissionsJsonMerger: MergeInfo = { const newPermissions: PermissionsJson = jsonc.parse(newPermissionsFile); const oldPermissions: PermissionsJson = jsonc.parse(oldPermissionsFile); const allowed_modules = [...new Set([...oldPermissions.allowed_modules, ...newPermissions.allowed_modules])]; - return jsonc.stringify({ allowed_modules }); + return jsonc.stringify({ allowed_modules }, { space: 2 }); } } \ No newline at end of file