From b44d5931d8ef46edf9661e76d80d5aaaf8b117d4 Mon Sep 17 00:00:00 2001 From: throwaway96 <68320646+throwaway96@users.noreply.github.com> Date: Thu, 7 Mar 2024 19:40:08 -0500 Subject: [PATCH] replace deprecated fs.exists() --- services/adapter.ts | 4 ---- services/service.ts | 28 ++++++++++++++-------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/services/adapter.ts b/services/adapter.ts index 0f1774e..47c79f5 100644 --- a/services/adapter.ts +++ b/services/adapter.ts @@ -38,8 +38,4 @@ export const asyncWriteFile: (path: string, contents: string, options?: fs.Write fs.writeFile, ); export const asyncChmod: (path: string, mode: fs.Mode) => Promise = Bluebird.Promise.promisify(fs.chmod); -export const asyncExists: (path: string) => Promise = (path) => - new Promise((resolve) => { - fs.exists(path, resolve); - }); export const asyncMkdir: (path: string, mode?: fs.Mode) => Promise = Bluebird.Promise.promisify(fs.mkdir); diff --git a/services/service.ts b/services/service.ts index ee471f1..52e2123 100644 --- a/services/service.ts +++ b/services/service.ts @@ -11,17 +11,7 @@ import { Promise } from 'bluebird'; // eslint-disable-line @typescript-eslint/no import progress from 'progress-stream'; import Service, { Message } from 'webos-service'; import fetch from 'node-fetch'; -import { - asyncStat, - asyncExecFile, - asyncPipeline, - asyncUnlink, - asyncWriteFile, - asyncReadFile, - asyncChmod, - asyncExists, - asyncMkdir, -} from './adapter'; +import { asyncStat, asyncExecFile, asyncPipeline, asyncUnlink, asyncWriteFile, asyncReadFile, asyncChmod, asyncMkdir } from './adapter'; import rootAppInfo from '../appinfo.json'; import serviceInfo from './services.json'; @@ -106,6 +96,16 @@ async function isFile(targetPath: string): Promise { } } +/** + * Check whether a path exists + */ +function exists(targetPath: string): Promise { + return asyncStat(targetPath).then( + () => true, + () => false, + ); +} + /** * Copies a file */ @@ -169,7 +169,7 @@ function flagFilePath(flagFile: FlagFileName): string { * Returns whether a flag is set or not. */ async function flagFileRead(flagFile: FlagFileName): Promise { - return asyncExists(flagFilePath(flagFile)); + return exists(flagFilePath(flagFile)); } /** @@ -785,11 +785,11 @@ function runService(): void { if (!runningAsRoot) { return { message: 'Not running as root.', returnValue: true }; } - if (await asyncExists('/tmp/webosbrew_startup')) { + if (await exists('/tmp/webosbrew_startup')) { return { message: 'Startup script already executed.', returnValue: true }; } // Copy startup.sh if doesn't exist - if (!(await asyncExists('/var/lib/webosbrew/startup.sh'))) { + if (!(await exists('/var/lib/webosbrew/startup.sh'))) { try { await asyncMkdir('/var/lib/webosbrew/', 0o755); } catch {