Skip to content

Commit

Permalink
Format app folder (#358)
Browse files Browse the repository at this point in the history
Also put prettier config in its own file
  • Loading branch information
ficristo authored Sep 9, 2024
1 parent 30d188b commit df167cc
Show file tree
Hide file tree
Showing 18 changed files with 321 additions and 202 deletions.
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @see https://prettier.io/docs/en/configuration.html
* @type {import("prettier").Config}
*/
const config = {
printWidth: 100,
tabWidth: 4,
semi: true,
trailingComma: "es5",
};

module.exports = config;
84 changes: 60 additions & 24 deletions app/appshell/app-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,21 @@ function _refreshMenu(win: BrowserWindow, callback?: () => void): void {
}

function _findMenuItemPosition(
id: string, where: Array<MenuItemOptions>, whereId: string = ""
id: string,
where: Array<MenuItemOptions>,
whereId: string = ""
): [string, number] | null {
const result = _.find(where, { id });
if (result) {
return [whereId, _.findIndex(where, { id })];
}
const results = _.compact(where.map(function (menuItem) {
return menuItem.submenu
? _findMenuItemPosition(id, menuItem.submenu as Array<MenuItemOptions>, menuItem.id)
: null;
}));
const results = _.compact(
where.map(function (menuItem) {
return menuItem.submenu
? _findMenuItemPosition(id, menuItem.submenu as Array<MenuItemOptions>, menuItem.id)
: null;
})
);
return results.length > 0 ? results[0] : null;
}

Expand All @@ -75,9 +79,13 @@ function _deleteMenuItemById(id: string, where: Array<MenuItemOptions>): boolean
where.splice(result, 1);
return true;
}
const deleted = where.map(function (menuItem) {
return menuItem.submenu ? _deleteMenuItemById(id, menuItem.submenu as Array<MenuItemOptions>) : null;
}).filter((x) => x === true);
const deleted = where
.map(function (menuItem) {
return menuItem.submenu
? _deleteMenuItemById(id, menuItem.submenu as Array<MenuItemOptions>)
: null;
})
.filter((x) => x === true);
return deleted.length > 0 ? true : false;
}

Expand All @@ -86,9 +94,13 @@ function _findMenuItemById(id: string, where: Array<MenuItemOptions>): MenuItemO
if (result) {
return result;
}
const results = _.compact(where.map(function (menuItem) {
return menuItem.submenu ? _findMenuItemById(id, menuItem.submenu as Array<MenuItemOptions>) : null;
}));
const results = _.compact(
where.map(function (menuItem) {
return menuItem.submenu
? _findMenuItemById(id, menuItem.submenu as Array<MenuItemOptions>)
: null;
})
);
return results.length > 0 ? results[0] : null;
}

Expand All @@ -104,14 +116,21 @@ function _addToPosition(
} else if (position === "last") {
target.push(obj);
} else if (
position === "before" || position === "after" || position === "firstInSection" || position === "lastInSection"
position === "before" ||
position === "after" ||
position === "firstInSection" ||
position === "lastInSection"
) {
let idx = _.findIndex(target, {id: relativeId!});
let idx = _.findIndex(target, { id: relativeId! });
let idxSection: number;
if (idx === -1) {
// NOTE: original behaviour - if relativeId wasn't found
// menu should be put to the end of the list
console.warn("menu item with id: " + relativeId + " was not found, adding entry to the end of the list");
console.warn(
"menu item with id: " +
relativeId +
" was not found, adding entry to the end of the list"
);
retVal = ERR_NOT_FOUND;
idx = target.length;
}
Expand Down Expand Up @@ -170,8 +189,11 @@ export function addMenu(
assert(typeof winId === "number", "winId must be a number");
assert(title && typeof title === "string", "title must be a string");
assert(id && typeof id === "string", "id must be a string");
assert(!position || position && typeof position === "string", "position must be a string");
assert(!relativeId || relativeId && typeof relativeId === "string", "relativeId must be a string");
assert(!position || (position && typeof position === "string"), "position must be a string");
assert(
!relativeId || (relativeId && typeof relativeId === "string"),
"relativeId must be a string"
);
assert(typeof callback === "function", "callback must be a function");
process.nextTick(function () {
const newObj = { id, label: title };
Expand All @@ -197,10 +219,16 @@ export function addMenuItem(
assert(parentId && typeof parentId === "string", "parentId must be a string");
assert(title && typeof title === "string", "title must be a string");
assert(id && typeof id === "string", "id must be a string");
assert(!key || key && typeof key === "string", "key must be a string");
assert(!displayStr || displayStr && typeof displayStr === "string", "displayStr must be a string");
assert(!position || position && typeof position === "string", "position must be a string");
assert(!relativeId || relativeId && typeof relativeId === "string", "relativeId must be a string");
assert(!key || (key && typeof key === "string"), "key must be a string");
assert(
!displayStr || (displayStr && typeof displayStr === "string"),
"displayStr must be a string"
);
assert(!position || (position && typeof position === "string"), "position must be a string");
assert(
!relativeId || (relativeId && typeof relativeId === "string"),
"relativeId must be a string"
);
assert(typeof callback === "function", "callback must be a function");
process.nextTick(function () {
const win = BrowserWindow.fromId(winId)!;
Expand All @@ -214,7 +242,7 @@ export function addMenuItem(
type: isSeparator ? "separator" : "normal",
id,
label: title,
click: () => win.webContents.send("executeCommand", id)
click: () => win.webContents.send("executeCommand", id),
};

if (key) {
Expand All @@ -233,7 +261,12 @@ export function addMenuItem(
parentObj.submenu = [];
}

const err = _addToPosition(newObj, parentObj.submenu as Array<MenuItemOptions>, position || "last", relativeId);
const err = _addToPosition(
newObj,
parentObj.submenu as Array<MenuItemOptions>,
position || "last",
relativeId
);
_refreshMenu(win, callback.bind(null, err));
});
}
Expand Down Expand Up @@ -325,7 +358,10 @@ export function setMenuItemShortcut(
): void {
assert(typeof winId === "number", "winId must be a number");
assert(commandId && typeof commandId === "string", "commandId must be a string");
assert(shortcut === "" || (shortcut && typeof shortcut === "string"), "shortcut must be a string");
assert(
shortcut === "" || (shortcut && typeof shortcut === "string"),
"shortcut must be a string"
);
process.nextTick(function () {
shortcut = _fixBracketsKeyboardShortcut(shortcut);
const menuTemplate = menuTemplates[winId];
Expand Down
22 changes: 11 additions & 11 deletions app/appshell/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export function getExtensionsFolder(): string {
export function showExtensionsFolder(appURL: any, callback: (err?: Error) => void): void {
process.nextTick(function () {
shell.showItemInFolder(utils.convertBracketsPathToWindowsPath(getExtensionsFolder()));
if (callback) { callback(); }
if (callback) {
callback();
}
});
}

Expand All @@ -77,7 +79,9 @@ export function getElapsedMilliseconds(): number {
return diff[0] * 1000 + diff[1] / 1000000;
}

export function getPendingFilesToOpen(callback: (err?: Error, filePaths?: Array<string>) => void): void {
export function getPendingFilesToOpen(
callback: (err?: Error, filePaths?: Array<string>) => void
): void {
process.nextTick(function () {
// TODO: implement
callback(new Error("app.getPendingFilesToOpen not implemented"), []);
Expand Down Expand Up @@ -114,10 +118,7 @@ export function openLiveBrowser(
});
}

export function openURLInDefaultBrowser(
url: string,
callback: (err?: Error) => void
): void {
export function openURLInDefaultBrowser(url: string, callback: (err?: Error) => void): void {
assert(url && typeof url === "string", "url must be a string");
process.nextTick(function () {
shell.openExternal(url);
Expand All @@ -138,13 +139,12 @@ export function showDeveloperTools(): void {
}

// TODO: get rid of callback? This call is not throwing any error.
export function showOSFolder(
path: string,
callback: () => void
): void {
export function showOSFolder(path: string, callback: () => void): void {
process.nextTick(function () {
shell.showItemInFolder(utils.convertBracketsPathToWindowsPath(path));
if (callback) { callback(); }
if (callback) {
callback();
}
});
}

Expand Down
79 changes: 53 additions & 26 deletions app/appshell/fs-additions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const trash = require("trash");
to support functionality required by brackets
*/

export function isBinaryFile(filename: string, callback: (err?: Error, res?: boolean) => void): void {
export function isBinaryFile(
filename: string,
callback: (err?: Error, res?: boolean) => void
): void {
isbinaryfile(filename, callback);
}

Expand All @@ -22,14 +25,20 @@ export function isEncodingSupported(encoding: string): boolean {
return ["ascii", "utf-8", "utf8"].indexOf(encoding.toLowerCase()) !== -1;
}

export function isNetworkDrive(path: string, callback: (err: Error | null, res: boolean) => void): void {
export function isNetworkDrive(
path: string,
callback: (err: Error | null, res: boolean) => void
): void {
// TODO: implement
process.nextTick(function () {
callback(null, false);
});
}

export function moveToTrash(path: string, callback: (err: Error | null, result?: any) => void): void {
export function moveToTrash(
path: string,
callback: (err: Error | null, result?: any) => void
): void {
fs.stat(path, function (err) {
if (err) {
return callback(err);
Expand All @@ -41,7 +50,11 @@ export function moveToTrash(path: string, callback: (err: Error | null, result?:
});
}

export function readTextFile(filename: string, encoding: string, callback: (err: Error | null, res?: string) => void): void {
export function readTextFile(
filename: string,
encoding: string,
callback: (err: Error | null, res?: string) => void
): void {
if (typeof encoding === "function") {
callback = encoding;
encoding = "utf-8";
Expand All @@ -57,7 +70,9 @@ export function readTextFile(filename: string, encoding: string, callback: (err:
return callback(err);
}
if (isBinary) {
const err2: NodeJS.ErrnoException = new Error("ECHARSET: file is a binary file: " + filename);
const err2: NodeJS.ErrnoException = new Error(
"ECHARSET: file is a binary file: " + filename
);
err2.code = "ECHARSET";
return callback(err2);
}
Expand Down Expand Up @@ -113,12 +128,12 @@ export function showOpenDialog(
* Extensions without wildcards or dots (e.g. 'png' is good but '.png' and '*.png' are bad).
* To show all files, use the '*' wildcard (no other wildcard is supported).
*/
filters: Array<{ name: string, extensions: Array<string> }>,
filters: Array<{ name: string; extensions: Array<string> }>,
callback: (err: Error | null, fileNames?: Array<string>) => void
): void {
const properties: Array<(
const properties: Array<
"openFile" | "openDirectory" | "multiSelections" | "createDirectory" | "showHiddenFiles"
)> = [];
> = [];
if (chooseDirectory) {
properties.push("openDirectory");
} else {
Expand All @@ -129,16 +144,22 @@ export function showOpenDialog(
}
// TODO: I don't think defaultPath and filters work right now - we should test that
// Also, it doesn't return an error code on failure any more (and doesn't pass one to the callback as well)
dialog.showOpenDialog({
title,
defaultPath,
filters,
properties
}).then((result) => {
callback(null, result.filePaths ? result.filePaths.map(utils.convertWindowsPathToUnixPath) : []);
}).catch((err) => {
callback(err);
});
dialog
.showOpenDialog({
title,
defaultPath,
filters,
properties,
})
.then((result) => {
callback(
null,
result.filePaths ? result.filePaths.map(utils.convertWindowsPathToUnixPath) : []
);
})
.catch((err) => {
callback(err);
});
}

export function showSaveDialog(
Expand All @@ -150,12 +171,18 @@ export function showSaveDialog(
// TODO: Implement proposedNewFilename
// TODO: I don't think defaultPath works right now - we should test that
// Also, it doesn't return an error code on failure any more (and doesn't pass one to the callback as well)
dialog.showSaveDialog({
title,
defaultPath
}).then((result) => {
callback(null, result.filePath ? utils.convertWindowsPathToUnixPath(result.filePath) : undefined);
}).catch((err) => {
callback(err);
});
dialog
.showSaveDialog({
title,
defaultPath,
})
.then((result) => {
callback(
null,
result.filePath ? utils.convertWindowsPathToUnixPath(result.filePath) : undefined
);
})
.catch((err) => {
callback(err);
});
}
Loading

0 comments on commit df167cc

Please sign in to comment.