Skip to content

Commit 3eebea7

Browse files
committed
Pass $refs to resolvers (#98)
This allows resolvers to understand more of the context around what they are resolving. Ex. `$refs._root$Ref.path` indicates the file being dereferenced.
1 parent 29ea869 commit 3eebea7

3 files changed

Lines changed: 12 additions & 13 deletions

File tree

docs/plugins/resolvers.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var myResolver = {
1414

1515
canRead: /^mongodb:/i,
1616

17-
read: function(file, callback) {
17+
read: function(file, callback, $refs) {
1818
MongoClient.connect(file.url, function(err, db) {
1919
if (err) {
2020
callback(err);
@@ -53,8 +53,7 @@ canRead: function(file) {
5353
}
5454
```
5555
56-
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.
57-
56+
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).
5857
5958
#### The `read` method
6059
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).

lib/parse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ function parse (path, $refs, options) {
3232
};
3333

3434
// Read the file and then parse the data
35-
return readFile(file, options)
35+
return readFile(file, options, $refs)
3636
.then(function (resolver) {
3737
$ref.pathType = resolver.plugin.name;
3838
file.data = resolver.result;
39-
return parseFile(file, options);
39+
return parseFile(file, options, $refs);
4040
})
4141
.then(function (parser) {
4242
$ref.value = parser.result;
@@ -59,7 +59,7 @@ function parse (path, $refs, options) {
5959
* @returns {Promise}
6060
* The promise resolves with the raw file contents and the resolver that was used.
6161
*/
62-
function readFile (file, options) {
62+
function readFile (file, options, $refs) {
6363
return new Promise(function (resolve, reject) {
6464
// console.log('Reading %s', file.url);
6565

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

7070
// Run the resolvers, in order, until one of them succeeds
7171
plugins.sort(resolvers);
72-
plugins.run(resolvers, "read", file)
72+
plugins.run(resolvers, "read", file, $refs)
7373
.then(resolve, onError);
7474

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

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

111111
// Run the parsers, in order, until one of them succeeds
112112
plugins.sort(parsers);
113-
plugins.run(parsers, "parse", file)
113+
plugins.run(parsers, "parse", file, $refs)
114114
.then(onParsed, onError);
115115

116116
function onParsed (parser) {

lib/util/plugins.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ exports.sort = function (plugins) {
6060
* @param {object} file - A file info object, which will be passed to each method
6161
* @returns {Promise}
6262
*/
63-
exports.run = function (plugins, method, file) {
63+
exports.run = function (plugins, method, file, $refs) {
6464
var plugin, lastError, index = 0;
6565

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

7676
try {
7777
// console.log(' %s', plugin.name);
78-
var result = getResult(plugin, method, file, callback);
78+
var result = getResult(plugin, method, file, callback, $refs);
7979
if (result && typeof result.then === "function") {
8080
// A promise was returned
8181
result.then(onSuccess, onError);
@@ -128,11 +128,11 @@ exports.run = function (plugins, method, file) {
128128
* @param {function} [callback] - A callback function, which will be passed to the method
129129
* @returns {*}
130130
*/
131-
function getResult (obj, prop, file, callback) {
131+
function getResult (obj, prop, file, callback, $refs) {
132132
var value = obj[prop];
133133

134134
if (typeof value === "function") {
135-
return value.apply(obj, [file, callback]);
135+
return value.apply(obj, [file, callback, $refs]);
136136
}
137137

138138
if (!callback) {

0 commit comments

Comments
 (0)