Skip to content

Commit 20143f1

Browse files
Merge pull request #124 from ziadsawalha/feature/98-pass_refs_to_resolvers
#98: Pass $refs to resolvers
2 parents e048c84 + 28b06aa commit 20143f1

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 @@ let myResolver = {
1414

1515
canRead: /^mongodb:/i,
1616

17-
read(file, callback) {
17+
read(file, callback, $refs) {
1818
MongoClient.connect(file.url, (err, db) => {
1919
if (err) {
2020
callback(err);
@@ -55,8 +55,7 @@ let myResolver = {
5555
};
5656
```
5757
58-
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.
59-
58+
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).
6059
6160
#### The `read` method
6261
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 @@ async function parse (path, $refs, options) {
3232
};
3333

3434
// Read the file and then parse the data
35-
const resolver = await readFile(file, options);
35+
const resolver = await readFile(file, options, $refs);
3636
$ref.pathType = resolver.plugin.name;
3737
file.data = resolver.result;
3838

39-
const parser = await parseFile(file, options);
39+
const parser = await parseFile(file, options, $refs);
4040
$ref.value = parser.result;
4141

4242
return parser.result;
@@ -57,7 +57,7 @@ async function parse (path, $refs, options) {
5757
* @returns {Promise}
5858
* The promise resolves with the raw file contents and the resolver that was used.
5959
*/
60-
function readFile (file, options) {
60+
function readFile (file, options, $refs) {
6161
return new Promise(((resolve, reject) => {
6262
// console.log('Reading %s', file.url);
6363

@@ -67,7 +67,7 @@ function readFile (file, options) {
6767

6868
// Run the resolvers, in order, until one of them succeeds
6969
plugins.sort(resolvers);
70-
plugins.run(resolvers, "read", file)
70+
plugins.run(resolvers, "read", file, $refs)
7171
.then(resolve, onError);
7272

7373
function onError (err) {
@@ -95,7 +95,7 @@ function readFile (file, options) {
9595
* @returns {Promise}
9696
* The promise resolves with the parsed file contents and the parser that was used.
9797
*/
98-
function parseFile (file, options) {
98+
function parseFile (file, options, $refs) {
9999
return new Promise(((resolve, reject) => {
100100
// console.log('Parsing %s', file.url);
101101

@@ -108,7 +108,7 @@ function parseFile (file, options) {
108108

109109
// Run the parsers, in order, until one of them succeeds
110110
plugins.sort(parsers);
111-
plugins.run(parsers, "parse", file)
111+
plugins.run(parsers, "parse", file, $refs)
112112
.then(onParsed, onError);
113113

114114
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
let plugin, lastError, index = 0;
6565

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

7676
try {
7777
// console.log(' %s', plugin.name);
78-
let result = getResult(plugin, method, file, callback);
78+
let 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
let 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)