diff --git a/src/bundle/mac/app.ts b/src/bundle/mac/app.ts index 2ae288a..fa1fe1f 100644 --- a/src/bundle/mac/app.ts +++ b/src/bundle/mac/app.ts @@ -1,11 +1,11 @@ -import {copyFile, mkdir, stat, writeFile} from 'node:fs/promises'; +import {copyFile, mkdir, readFile, stat, writeFile} from 'node:fs/promises'; import {join as pathJoin, basename, dirname} from 'node:path'; import {fsLstatExists} from '@shockpkg/archive-files'; +import {Plist} from '@shockpkg/plist-dom'; import {trimExtension} from '../../util'; import { - plistRead, infoPlistBundleExecutableSet, infoPlistBundleIconFileGet, machoTypesFile, @@ -77,7 +77,8 @@ export class BundleMacApp extends BundleMac { const appPkgInfo = pathJoin(appContents, 'PkgInfo'); // Read the projector Info.plist. - const plist = await plistRead(projector.infoPlistPath); + const plist = new Plist(); + plist.fromXml(await readFile(projector.infoPlistPath, 'utf8')); // Get the binary path and read the types. const projBinaryPath = projector.binaryPath; diff --git a/src/projector/mac/app.ts b/src/projector/mac/app.ts index 4084761..d7d7dd0 100644 --- a/src/projector/mac/app.ts +++ b/src/projector/mac/app.ts @@ -14,8 +14,6 @@ import { trimExtension } from '../../util'; import { - plistRead, - plistParse, infoPlistBundleExecutableSet, infoPlistBundleIconFileSet, infoPlistBundleNameSet @@ -431,23 +429,22 @@ export class ProjectorMacApp extends ProjectorMac { } /** - * Get Info.plist data if any specified, document, data, or file. + * Get Info.plist data if any specified, from data or file. * * @returns Info.plist data or null. */ - public async getInfoPlistDocument() { + public async getInfoPlistData() { const {infoPlistData, infoPlistFile} = this; - let xml; if (typeof infoPlistData === 'string') { - xml = infoPlistData; - } else if (infoPlistData) { - xml = infoPlistData.toString('utf8'); - } else if (infoPlistFile) { - xml = await readFile(infoPlistFile, 'utf8'); - } else { - return null; + return infoPlistData; + } + if (infoPlistData) { + return infoPlistData.toString('utf8'); } - return plistParse(xml); + if (infoPlistFile) { + return readFile(infoPlistFile, 'utf8'); + } + return null; } /** @@ -731,24 +728,43 @@ export class ProjectorMacApp extends ProjectorMac { * Update the projector Info.plist if needed. */ protected async _updateInfoPlist() { - const customPlist = await this.getInfoPlistDocument(); + const path = this.infoPlistPath; + const xml = await this._generateInfoPlist(); + if (xml === null) { + return; + } + + await rm(path, {force: true}); + await mkdir(dirname(path), {recursive: true}); + await writeFile(path, xml, 'utf8'); + } + + /** + * Generate Info.plist XML string, if any. + * + * @returns XML string or null. + */ + protected async _generateInfoPlist() { + const customPlist = await this.getInfoPlistData(); const bundleName = this.getBundleName(); const {appBinaryNameCustom, appIconNameCustom} = this; if ( !( - customPlist || + customPlist !== null || appIconNameCustom || appBinaryNameCustom || bundleName !== false ) ) { - return; + return null; } // Use a custom plist or the existing one. - const plist = customPlist || (await this._readInfoPlist()); + const xml = customPlist ?? (await readFile(this.infoPlistPath, 'utf8')); + + const plist = new Plist(); + plist.fromXml(xml); - // Update values. if (appIconNameCustom) { infoPlistBundleIconFileSet(plist, appIconNameCustom); } @@ -759,28 +775,6 @@ export class ProjectorMacApp extends ProjectorMac { infoPlistBundleNameSet(plist, bundleName); } - // Write out the plist. - await this._writeInfoPlist(plist); - } - - /** - * Read the projector Info.plist file. - * - * @returns Plist document. - */ - protected async _readInfoPlist() { - return plistRead(this.infoPlistPath); - } - - /** - * Write the projector Info.plist file. - * - * @param plist Plist document. - */ - protected async _writeInfoPlist(plist: Plist) { - const path = this.infoPlistPath; - await rm(path, {force: true}); - await mkdir(dirname(path), {recursive: true}); - await writeFile(path, plist.toXml(), 'utf8'); + return plist.toXml(); } } diff --git a/src/util/mac.ts b/src/util/mac.ts index 16546cc..f53e9e9 100644 --- a/src/util/mac.ts +++ b/src/util/mac.ts @@ -25,31 +25,6 @@ export interface IMachoType { cpuSubtype: number; } -/** - * Parse plist data. - * Currently only supports XML plist. - * - * @param data Plist XML. - * @returns Plist document. - */ -// eslint-disable-next-line @typescript-eslint/require-await -export async function plistParse(data: string) { - const plist = new Plist(); - plist.fromXml(data); - return plist; -} - -/** - * Read plist file. - * Currently only supports XML plist. - * - * @param path Plist file. - * @returns Plist document. - */ -export async function plistRead(path: string) { - return plistParse(await readFile(path, 'utf8')); -} - /** * Get Info.plist dictionary or throw. *