Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit bd3c7dc

Browse files
committed
Merge pull request #265 from adobe/jeff/unlink-folders-fs-optional
Folders can now be unlinked and callbacks are optional on some APIs
2 parents 9766a9e + 5ff5ae9 commit bd3c7dc

3 files changed

Lines changed: 82 additions & 63 deletions

File tree

appshell/appshell_extensions.js

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ if (!appshell.app) {
121121
*/
122122
appshell.app.ERR_NODE_FAILED = -3;
123123

124+
/*
125+
* @private
126+
*/
127+
var _dummyCallback = function () {
128+
};
129+
124130
/**
125131
* Display the OS File Open dialog, allowing the user to select
126132
* files or directories.
@@ -217,27 +223,28 @@ if (!appshell.app) {
217223
*
218224
* @param {string} path The path of the directory to create.
219225
* @param {number} mode The permissions for the directory, in numeric format (ie 0777)
220-
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument.
226+
* @param {function(err)=} callback Asynchronous callback function. The callback gets one argument.
221227
*
222228
* @return None. This is an asynchronous call that sends all return information to the callback.
223229
**/
224230
native function MakeDir();
225231
appshell.fs.makedir = function (path, mode, callback) {
226-
MakeDir(callback, path, mode);
232+
// RFC: mode should be 0777 if it's undefined
233+
MakeDir(callback || _dummyCallback, path, mode);
227234
};
228235

229236
/**
230237
* Rename a file or directory.
231238
*
232239
* @param {string} oldPath The old name of the file or directory.
233240
* @param {string} newPath The new name of the file or directory.
234-
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument.
241+
* @param {function(err)=} callback Asynchronous callback function. The callback gets one argument.
235242
*
236243
* @return None. This is an asynchronous call that sends all return information to the callback.
237244
**/
238245
native function Rename();
239246
appshell.fs.rename = function(oldPath, newPath, callback) {
240-
Rename(callback, oldPath, newPath);
247+
Rename(callback || _dummyCallback, oldPath, newPath);
241248
};
242249

243250
/**
@@ -338,7 +345,7 @@ if (!appshell.app) {
338345
* @param {string} path The path of the file to write.
339346
* @param {string} data The data to write to the file.
340347
* @param {string} encoding The encoding for the file. The only supported encoding is 'utf8'.
341-
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
348+
* @param {function(err)=} callback Asynchronous callback function. The callback gets one argument (err).
342349
* Possible error values:
343350
* NO_ERROR
344351
* ERR_UNKNOWN
@@ -351,15 +358,15 @@ if (!appshell.app) {
351358
*/
352359
native function WriteFile();
353360
appshell.fs.writeFile = function (path, data, encoding, callback) {
354-
WriteFile(callback, path, data, encoding);
361+
WriteFile(callback || _dummyCallback, path, data, encoding);
355362
};
356363

357364
/**
358365
* Set permissions for a file or directory.
359366
*
360367
* @param {string} path The path of the file or directory
361368
* @param {number} mode The permissions for the file or directory, in numeric format (ie 0777)
362-
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
369+
* @param {function(err)=} callback Asynchronous callback function. The callback gets one argument (err).
363370
* Possible error values:
364371
* NO_ERROR
365372
* ERR_UNKNOWN
@@ -370,14 +377,14 @@ if (!appshell.app) {
370377
*/
371378
native function SetPosixPermissions();
372379
appshell.fs.chmod = function (path, mode, callback) {
373-
SetPosixPermissions(callback, path, mode);
380+
SetPosixPermissions(callback || _dummyCallback, path, mode);
374381
};
375382

376383
/**
377384
* Delete a file permanently.
378385
*
379386
* @param {string} path The path of the file to delete
380-
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
387+
* @param {function(err)=} callback Asynchronous callback function. The callback gets one argument (err).
381388
* Possible error values:
382389
* NO_ERROR
383390
* ERR_UNKNOWN
@@ -389,14 +396,14 @@ if (!appshell.app) {
389396
*/
390397
native function DeleteFileOrDirectory();
391398
appshell.fs.unlink = function (path, callback) {
392-
DeleteFileOrDirectory(callback, path);
399+
DeleteFileOrDirectory(callback || _dummyCallback, path);
393400
};
394401

395402
/**
396403
* Delete a file non-permanently (to trash).
397404
*
398405
* @param {string} path The path of the file or directory to delete
399-
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
406+
* @param {function(err)=} callback Asynchronous callback function. The callback gets one argument (err).
400407
* Possible error values:
401408
* NO_ERROR
402409
* ERR_UNKNOWN
@@ -408,7 +415,7 @@ if (!appshell.app) {
408415
*/
409416
native function MoveFileOrDirectoryToTrash();
410417
appshell.fs.moveToTrash = function (path, callback) {
411-
MoveFileOrDirectoryToTrash(callback, path);
418+
MoveFileOrDirectoryToTrash(callback || _dummyCallback, path);
412419
};
413420

414421
/**
@@ -425,7 +432,7 @@ if (!appshell.app) {
425432
*
426433
* @param {string} url
427434
* @param {boolean} enableRemoteDebugging
428-
* @param {function(err)} callback Asynchronous callback function with one argument (the error)
435+
* @param {function(err)=} callback Asynchronous callback function with one argument (the error)
429436
* Possible error values:
430437
* NO_ERROR
431438
* ERR_INVALID_PARAMS - invalid parameters
@@ -438,7 +445,7 @@ if (!appshell.app) {
438445
appshell.app.openLiveBrowser = function (url, enableRemoteDebugging, callback) {
439446
// enableRemoteDebugging flag is ignored on mac
440447
setTimeout(function() {
441-
OpenLiveBrowser(callback, url, enableRemoteDebugging);
448+
OpenLiveBrowser(callback || _dummyCallback, url, enableRemoteDebugging);
442449
}, 0);
443450
};
444451

@@ -447,7 +454,7 @@ if (!appshell.app) {
447454
* the close attempt if there is a page with unsaved changes. This function will fire the
448455
* callback when the browser is closed (No_ERROR) or after a three minute timeout (ERR_UNKNOWN).
449456
*
450-
* @param {function(err)} callback Asynchronous callback function with one argument (the error)
457+
* @param {function(err)} callback Asynchronous callback function with one argument (the error)
451458
* Possible error values:
452459
* NO_ERROR (all windows are closed by the time the callback is fired)
453460
* ERR_UNKNOWN - windows are currently open, though the user may be getting prompted by the
@@ -457,20 +464,21 @@ if (!appshell.app) {
457464
*/
458465
native function CloseLiveBrowser();
459466
appshell.app.closeLiveBrowser = function (callback) {
460-
CloseLiveBrowser(callback);
467+
CloseLiveBrowser(callback || _dummyCallback);
461468
};
462469

463470
/**
464471
* Open a URL in the default OS browser window.
465472
*
466473
* @param {function(err)} callback Asynchronous callback function with one argument (the error)
474+
* NOTE: null or undefined can be used for this argument to avoid the callback
467475
* @param {string} url URL to open in the browser.
468476
*
469477
* @return None. This is an asynchronous call that sends all return information to the callback.
470478
*/
471479
native function OpenURLInDefaultBrowser();
472-
appshell.app.openURLInDefaultBrowser = function (callback, url) {
473-
OpenURLInDefaultBrowser(callback, url);
480+
appshell.app.openURLInDefaultBrowser = function (url, callback) {
481+
OpenURLInDefaultBrowser(callback || _dummyCallback, url);
474482
};
475483

476484
/**
@@ -505,15 +513,15 @@ if (!appshell.app) {
505513
* @param {string} command ID of the menu item.
506514
* @param {bool} enabled bool to enable or disable the command
507515
* @param {bool} checked bool to set the 'checked' attribute of the menu item.
508-
* @param {function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
516+
* @param {?function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
509517
* Possible error values:
510518
* NO_ERROR
511519
* ERR_INVALID_PARAMS
512520
* @return None. This is an asynchronous call that is not meant to have a return
513521
*/
514522
native function SetMenuItemState();
515523
appshell.app.setMenuItemState = function (commandid, enabled, checked, callback) {
516-
SetMenuItemState(callback, commandid, enabled, checked);
524+
SetMenuItemState(callback || _dummyCallback, commandid, enabled, checked);
517525
};
518526

519527
/**
@@ -537,7 +545,7 @@ if (!appshell.app) {
537545
* @param {string} id Menu ID, e.g. "file"
538546
* @param {string} position Where to put the item; values are "before", "after", "first", "last", and ""
539547
* @param {string} relativeId The ID of the menu to which is this relative, for position "before" and "after"
540-
* @param {function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
548+
* @param {?function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
541549
* Possible error values:
542550
* NO_ERROR
543551
* ERR_INVALID_PARAMS
@@ -547,7 +555,7 @@ if (!appshell.app) {
547555
appshell.app.addMenu = function (title, id, position, relativeId, callback) {
548556
position = position || '';
549557
relativeId = relativeId || '';
550-
AddMenu(callback, title, id, position, relativeId);
558+
AddMenu(callback || _dummyCallback, title, id, position, relativeId);
551559
};
552560

553561
/**
@@ -560,7 +568,7 @@ if (!appshell.app) {
560568
* @param {string} position Where to put the item; values are "before", "after", "first", "last",
561569
* "firstInSection", "lastInSection", and ""
562570
* @param {string} relativeId The ID of the menu item to which is this relative, for position "before" and "after"
563-
* @param {function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
571+
* @param {?function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
564572
* Possible error values:
565573
* NO_ERROR
566574
* ERR_INVALID_PARAMS
@@ -584,22 +592,22 @@ if (!appshell.app) {
584592
key = key || '';
585593
position = position || '';
586594
relativeId = relativeId || '';
587-
AddMenuItem(callback, parentId, title, id, key, displayStr, position, relativeId);
595+
AddMenuItem(callback || _dummyCallback, parentId, title, id, key, displayStr, position, relativeId);
588596
};
589597

590598
/**
591599
* Change the title of a menu or menu item.
592600
* @param {string} commandid Menu/Command ID, e.g. "file" or "file.open"
593601
* @param {string} title Menu title to display, e.g. "File" or "Open"
594-
* @param {function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
602+
* @param {?function(integer)} callback Asynchronous callback function. The callback gets one argument, error code.
595603
* Possible error values:
596604
* NO_ERROR
597605
* ERR_INVALID_PARAMS, ERR_NOT_FOUND, ERR_UNKNOWN
598606
* @return None. This is an asynchronous call that is not meant to have a return
599607
*/
600608
native function SetMenuTitle();
601609
appshell.app.setMenuTitle = function (commandid, title, callback) {
602-
SetMenuTitle(callback, commandid, title);
610+
SetMenuTitle(callback || _dummyCallback, commandid, title);
603611
};
604612

605613
/**
@@ -622,18 +630,18 @@ if (!appshell.app) {
622630
* @param {string} commandId ID of the menu item.
623631
* @param {string} shortcut Shortcut string, like "Cmd-U".
624632
* @param {string} displayStr String to display in menu. If "", use shortcut.
625-
* @param {function (err)} callback Asynchronous callback function. The callback gets an error code.
633+
* @param {?function (err)} callback Asynchronous callback function. The callback gets an error code.
626634
* @return None. This is an asynchronous call that sends all return information to the callback.
627635
*/
628636
native function SetMenuItemShortcut();
629637
appshell.app.setMenuItemShortcut = function (commandId, shortcut, displayStr, callback) {
630-
SetMenuItemShortcut(callback, commandId, shortcut, displayStr);
638+
SetMenuItemShortcut(callback || _dummyCallback, commandId, shortcut, displayStr);
631639
};
632640

633641
/**
634642
* Remove menu associated with commandId.
635643
* @param {string} commandid ID of the menu item.
636-
* @param {function(err)} callback Asynchronous callback function. The callback gets an error code.
644+
* @param {function(err)=} callback Asynchronous callback function. The callback gets an error code.
637645
* Possible error values:
638646
* NO_ERROR
639647
* ERR_INVALID_PARAMS
@@ -643,13 +651,13 @@ if (!appshell.app) {
643651
*/
644652
native function RemoveMenu();
645653
appshell.app.removeMenu = function (commandId, callback) {
646-
RemoveMenu(callback, commandId);
654+
RemoveMenu(callback || _dummyCallback, commandId);
647655
};
648656

649657
/**
650658
* Remove menuitem associated with commandId.
651659
* @param {string} commandid ID of the menu item.
652-
* @param {function(err)} callback Asynchronous callback function. The callback gets an error code.
660+
* @param {function(err)=} callback Asynchronous callback function. The callback gets an error code.
653661
* Possible error values:
654662
* NO_ERROR
655663
* ERR_INVALID_PARAMS
@@ -659,7 +667,7 @@ if (!appshell.app) {
659667
*/
660668
native function RemoveMenuItem();
661669
appshell.app.removeMenuItem = function (commandId, callback) {
662-
RemoveMenuItem(callback, commandId);
670+
RemoveMenuItem(callback || _dummyCallback, commandId);
663671
};
664672

665673
/**
@@ -708,20 +716,20 @@ if (!appshell.app) {
708716
* Open the specified folder in an OS file window.
709717
*
710718
* @param {string} path Path of the folder to show.
711-
* @param {function(err)} callback Asyncrhonous callback function with one argument (the error)
719+
* @param {function(err)=} callback Asyncrhonous callback function with one argument (the error)
712720
*
713721
* @return None. This is an asynchronous call that sends all return information to the callback.
714722
*/
715723
native function ShowOSFolder();
716724
appshell.app.showOSFolder = function (path, callback) {
717-
ShowOSFolder(callback, path);
725+
ShowOSFolder(callback || _dummyCallback, path);
718726
}
719727

720728
/**
721729
* Open the extensions folder in an OS file window.
722730
*
723731
* @param {string} appURL Not used
724-
* @param {function(err)} callback Asynchronous callback function with one argument (the error)
732+
* @param {function(err)=} callback Asynchronous callback function with one argument (the error)
725733
*
726734
* @return None. This is an asynchronous call that sends all return information to the callback.
727735
*/

appshell/appshell_extensions_mac.mm

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,9 @@ int32 DeleteFileOrDirectory(ExtensionString filename)
517517
NSError* error = nil;
518518

519519
NSString* path = [NSString stringWithUTF8String:filename.c_str()];
520-
BOOL isDirectory;
521520

522-
// Contrary to the name of this function, we don't actually delete directories
523-
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
524-
if (isDirectory) {
525-
return ERR_NOT_FILE;
526-
}
527-
} else {
521+
// Make sure it exists
522+
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
528523
return ERR_NOT_FOUND;
529524
}
530525

0 commit comments

Comments
 (0)