Skip to content

Commit

Permalink
Pass $refs to resolvers (#98)
Browse files Browse the repository at this point in the history
This allows resolvers to understand more of
the context around what they are resolving.

Ex. `$refs._root$Ref.path` indicates
the file being dereferenced.
  • Loading branch information
ziadsawalha committed Apr 26, 2019
1 parent 29ea869 commit 3eebea7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
5 changes: 2 additions & 3 deletions docs/plugins/resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var myResolver = {

canRead: /^mongodb:/i,

read: function(file, callback) {
read: function(file, callback, $refs) {
MongoClient.connect(file.url, function(err, db) {
if (err) {
callback(err);
Expand Down Expand Up @@ -53,8 +53,7 @@ canRead: function(file) {
}
```
When using the function form, the `file` parameter is a [file info object](file-info-object.md), which contains information about the file being resolved.
When using the function form, the `file` parameter is a [file info object](file-info-object.md), which contains information about the file being resolved. The `$refs` parameter is a [`$Refs`](../refs.md) object, which allows your resolver to determine context (ex. `$refs._root$Ref.path` can be used to determine the relative path your resolver is operating from).
#### The `read` method
This is where the real work of a resolver happens. The `read` method accepts the same [file info object](file-info-object.md) as the `canRead` function, but rather than returning a boolean value, the `read` method should return the contents of the file. The file contents should be returned in as raw a form as possible, such as a string or a byte array. Any further parsing or processing should be done by [parsers](parsers.md).
Expand Down
12 changes: 6 additions & 6 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function parse (path, $refs, options) {
};

// Read the file and then parse the data
return readFile(file, options)
return readFile(file, options, $refs)
.then(function (resolver) {
$ref.pathType = resolver.plugin.name;
file.data = resolver.result;
return parseFile(file, options);
return parseFile(file, options, $refs);
})
.then(function (parser) {
$ref.value = parser.result;
Expand All @@ -59,7 +59,7 @@ function parse (path, $refs, options) {
* @returns {Promise}
* The promise resolves with the raw file contents and the resolver that was used.
*/
function readFile (file, options) {
function readFile (file, options, $refs) {
return new Promise(function (resolve, reject) {
// console.log('Reading %s', file.url);

Expand All @@ -69,7 +69,7 @@ function readFile (file, options) {

// Run the resolvers, in order, until one of them succeeds
plugins.sort(resolvers);
plugins.run(resolvers, "read", file)
plugins.run(resolvers, "read", file, $refs)
.then(resolve, onError);

function onError (err) {
Expand Down Expand Up @@ -97,7 +97,7 @@ function readFile (file, options) {
* @returns {Promise}
* The promise resolves with the parsed file contents and the parser that was used.
*/
function parseFile (file, options) {
function parseFile (file, options, $refs) {
return new Promise(function (resolve, reject) {
// console.log('Parsing %s', file.url);

Expand All @@ -110,7 +110,7 @@ function parseFile (file, options) {

// Run the parsers, in order, until one of them succeeds
plugins.sort(parsers);
plugins.run(parsers, "parse", file)
plugins.run(parsers, "parse", file, $refs)
.then(onParsed, onError);

function onParsed (parser) {
Expand Down
8 changes: 4 additions & 4 deletions lib/util/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ exports.sort = function (plugins) {
* @param {object} file - A file info object, which will be passed to each method
* @returns {Promise}
*/
exports.run = function (plugins, method, file) {
exports.run = function (plugins, method, file, $refs) {
var plugin, lastError, index = 0;

return new Promise(function (resolve, reject) {
Expand All @@ -75,7 +75,7 @@ exports.run = function (plugins, method, file) {

try {
// console.log(' %s', plugin.name);
var result = getResult(plugin, method, file, callback);
var result = getResult(plugin, method, file, callback, $refs);
if (result && typeof result.then === "function") {
// A promise was returned
result.then(onSuccess, onError);
Expand Down Expand Up @@ -128,11 +128,11 @@ exports.run = function (plugins, method, file) {
* @param {function} [callback] - A callback function, which will be passed to the method
* @returns {*}
*/
function getResult (obj, prop, file, callback) {
function getResult (obj, prop, file, callback, $refs) {
var value = obj[prop];

if (typeof value === "function") {
return value.apply(obj, [file, callback]);
return value.apply(obj, [file, callback, $refs]);
}

if (!callback) {
Expand Down

0 comments on commit 3eebea7

Please sign in to comment.