This AI is still in early stages of development.
Dependency Checking
- Aubergine
Step 1
Note: It's best to sandbox your code to avoid things leaking out on to the global scope.
Define a Self Descriptor Object:
void (function fooAPI() { var self = { file: "foo.js", ver : 0.2 }
Step 2
Define dependencies then make sure they are provided or check individual requirements...
// continued... var dependencies = { "Util.js": 1.0, "bar.js" : Check.ANY_VERSION, "baz.js" : Check.NOT_LOADED } Check.required(dependencies, self); // throws error if something wrong if (Check.has("wibble.js")) { // do stuff that relies on wibble.js (any version) } else { // use a wibble-free alternative }
Because the code is being defined in a sandbox, if there are missing dependencies an error will be thrown and the whole sandbox will be blocked from further execution so you don't have to worry about stray bits of broken API being available.
Step 3
After your code is defined, mark it as provided:
// continued... // rest of your code global.addConst("Foo", yourAPI); // whatever your API exposes globally Check.provide(self); // indicate your API is available })();
If we get to the end of the sandbox, it means no errors have been found so far, so by providing your API at the end of the sandbox other scripts can be fairly sure the API is working from an architectural perspective.
Step 4
Use include() to import files in the usual manner:
include("path/to/Util.js"); include("path/to/bar.js"); include("path/to/baz.js"); include("path/to/foo.js"); // error: baz.js was loaded before foo.js
Or use autoloading by specifying some load paths and let Check load all your dependencies for you:
include("path/to/Util.js"); // always need this - its where Check lives Check.paths = ["path/to/"]; // one or more paths to look for dependencies in include("path/to/foo.js"); // autoloads "path/to/bar.js" (dependency of foo.js)
And more...
You can also:
- Check.versionOf() – find out the version of a specific file
- Check.doWhen() – run code when specific dependencies are met
Dependency Checking
Topics:
- Tricks for including files — Some tricks with the include() function...
- Dependency Descriptor Object — Define one or more dependencies for your script...
- Check() — Perform a soft-check to see if a dependency is available...
- Check.basePath — The base path to the folder that contains the APIs folder...
- Check.paths[] — Define one or more folder paths to enable autoloading...
- Check.doWhen() — Perform a task when dependencies become available.
- Check.has() — Check if a dependency is available, try and load it if not, return availability state...
- Check.LAZY_LOAD — Disable autoloading during a dependency check...
- Check.required() — Thrown a descriptive error if dependency check fails...
- Check.NOT_LOADED — Indicates a script is not yet loaded, or must not be loaded, depending on the Check function being used...
- Check.ANY_VERSION — Skip version checking for a dependency check...
- Check.provide() — Indicate a file is now available.
- Check.versionOf() — Check the version of a dependency.
- Self Descriptor Object — Define the filename and version of your script...