Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Improves plist parsing for a better/easier development experience #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"fs-extra": "^11.1.0",
"node-fetch": "^3.3.0",
"open": "^8.4.0",
"plist": "^3.0.6",
"xml2js": "^0.4.23"
Copy link
Author

@nidib nidib Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If accepted, this package should be removed.

},
"devDependencies": {
Expand Down
40 changes: 18 additions & 22 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import path from 'path';
import querystring from 'querystring';
import url from 'url';
import util from 'util';

import {parseString} from 'xml2js';
import plist from 'plist';

function logWithInspect(object) {
// https://stackoverflow.com/a/10729284/15487978
Expand All @@ -24,26 +23,28 @@ function isAppNameAllowed(appName) {
return !disallowedAppNames.includes(appName);
}

function getAppNamesToIconPaths(parsedDockData) {
const parsedAppData = parsedDockData.plist.dict[0]
function parseDockData(rawDockPlist) {
return plist.parse(rawDockPlist);
}

//logWithInspect(parsedAppData);
function getAppNamesWithIconPaths(parsedDockData) {
//logWithInspect(parsedDockData);

const persistentApps = parsedAppData.array[1].dict;
const _persistentOthers = parsedAppData.array[2].dict;
const _recentApps = parsedAppData.array[3].dict;
const persistentApps = parsedDockData['persistent-apps'] ?? [];
const persistentAppsWithoutSpacers = persistentApps.filter(item => item['tile-type'] === 'file-tile');
const appNamesWithIconPaths = {};

const result = {};
for (const app of persistentAppsWithoutSpacers) {
const appName = app['tile-data']?.['file-label'];
const appDirectoryUrl = app['tile-data']?.['file-data']?.['_CFURLString'];

for (const parsedAppData of persistentApps ?? []) {
const appName = parsedAppData.dict[0].string[1];
const appDirectoryUrl = parsedAppData.dict[0].dict?.[0].string[0];
if (appDirectoryUrl && isAppNameAllowed(appName)) {
const appDirectory = url.fileURLToPath(appDirectoryUrl)
result[appName] = getIconPath(appDirectory);
appNamesWithIconPaths[appName] = getIconPath(appDirectory);
}
}
return result;

return appNamesWithIconPaths;
}

/**
Expand Down Expand Up @@ -139,14 +140,9 @@ export async function scanDockAndBringToWebApp(dockXmlPlist) {
throw 'Dock data appears to be invalid. Expected: Apple plist XML.';
}

const parsedDockData = await new Promise((resolve, reject) => {
parseString(dockXmlPlist, function (error, result) {
return error ? reject(error) : resolve(result);
});
});

const appNamesToIconPaths = getAppNamesToIconPaths(parsedDockData);
const appNames = Object.keys(appNamesToIconPaths);
const parsedDockData = parseDockData(dockXmlPlist);
const appNamesWithIconPaths = getAppNamesWithIconPaths(parsedDockData);
const appNames = Object.keys(appNamesWithIconPaths);

if (appNames.length) {
console.log('Found the following pinned apps in your dock:\n')
Expand Down