Three questions about FileManager

See Script Reference for FileManager.

1. Directory or file?

Is there a way to determine whether a path is a directory or a file? This would be useful for example when using listContents to walk recursively down through a directory tree.

2. Extending the class

In JavaScript both built-in and custom classes can be subclassed with the extends keyword. Why does this not work for the FileManager class?

Cut-down example to illustrate:

class myFileManager extends FileManager {
  BASEPATH() {
    return this.basePath
  }
}

let fm = myFileManager.createCloud();
alert(fm.BASEPATH());

This gives

Script Error: TypeError: fm.BASEPATH is not a function. (In ‘fm.BASEPATH()’, ‘fm.BASEPATH’ is undefined)

3. lastError

Why is lastError undefined after a failure to write a file in a nonexistent directory?

let fm = FileManager.createCloud();
let path = "/deliberately/nonexisting/path.txt";
let success = fm.writeString(path, "Hello world");
if (!success) throw new Error(
  `Could not write ${path}: success=${success} error=${fm.lastError}`
);

The error message says “…success=false error=undefined”.

  1. There’s not a method to test if a path is a directory currently. I’ll add one. If you are using the results of listContents you can just test for whether the path ends with /
  2. You cannot “subclass” native objects (e.g. Swift/Objective-C) that are bridged to JavaScript with extends, to the best of my understanding this is a limitation of JavaScriptCore. You can extend them using the prototype object (example below).
  3. This seems to be an oversight, I’ll fix it up in the next release.
// use prototype to extend a class
FileManager.prototype.myFunction = function() {
	alert("myFunction!")
}
let fm = FileManager.createCloud();
fm.myFunction()

Great. Thanks very much for the helpful answers!