corenova / yang-js

YANG parser and composer
Apache License 2.0
56 stars 18 forks source link

Improve Yang.import to support directory search and extension order #119

Closed sekur closed 4 years ago

sekur commented 4 years ago

Currently, Yang.import only supports explicit module resolution via package.json as follows:

{
  "yang": {
    "resolve": {
       "some-module": "./schema/some-module.yang",
       "some-bound-module": "./schema/some-bound-module.js",
       "ietf-yang-types": "yang-js"
     }
  }
}

This means that you'd need a specific entry for every YANG schema module used in your project and where to locate the module in the filesystem.

However, it would be far more convenient to simply specify the directories and packages you can search and the order in which the file extensions should be tried.

The new improvement will support following additional options inside package.json:

{
  "yang": {
    "search": [ "./schema", "yang-js" ],
    "order": [ ".js", ".yang" ]
  }
}

Now, when you attempt to Yang.import a module by name, it will perform resolution in following order:

  1. Check if it's an already loaded module
  2. Scan a directory if there's a package.json
  3. If package.json exists, look for explicit resolution
  4. If explicit resolution and it's a target with filename extension, return the match
  5. If explicit resolution and it's a target for a dependency package, locate the path of the dependency and go back to step 2.
  6. If explicit resolution and it's a target for a directory, go back to step 2.
  7. If no explicit resolution, use the search array to iterate from step 2 until match
  8. If nothing found so far, search the directory context for files using the extension search order (default: [".js", ".yang"])
  9. If still nothing, then go up a directory tree (towards root) and repeat from step 2.