walling / gulp-raml2html

A simple RAML to HTML documentation generator, wrapped for Gulp.
MIT License
21 stars 23 forks source link

Not able to resolve !includes #5

Closed amr closed 10 years ago

amr commented 10 years ago

Hi,

I encountered a problem in resolving my !include statements. The error:

Error: ../api-resources/target/apidocs/apispec.raml:8:21: Parse error while reading ../json-schema/account-update.schema.json: cannot read ../json-schema/account-update.schema.json (Error: ENOENT, open '../json-schema/account-update.schema.json') at ../api-resources/node_modules/gulp-raml2html/index.js:26:16 at process._tickCallback (node.js:419:13)

The directory structure is:

src/
└── main
    ├── json-schema
    │   ├── account.schema.json
    │   ├── accounts.schema.json
    │   ├── account-update.schema.json
    │   ├── app.schema.json
    │   ├── apps.schema.json
    │   └── app-update.schema.json
    └── raml
        └── apispec.raml

In my spec.raml file, I include the JSON schema relative to the location of the RAML file, therefore: !include ../json-schema/account.schema.json

My gulpfile:

'use strict';

var gulp = require('gulp')
var raml2html = require('gulp-raml2html')

gulp.task('default', function() {
  return gulp.src('src/main/raml/apispec.raml')
    .pipe(raml2html())
    .pipe(gulp.dest('target/apidocs'))
})

It appears to me that the problem is that while processing, the tools involved lose track of the base path to use when resolving includes. A quick & dirty fix I applied is changing the cwd manually. The new relevant code in gulpRaml2html looks like this:

function gulpRaml2html(options) {
  options = options || {};
  var supportJsonInput = !!options.supportJsonInput;

  return through2.obj(function(file, enc, origCallback) {
    // Needed so resolving !include statements works relative to the file
    var prevCwd = process.cwd()
    process.chdir(file.base)

    var callback = function () {
      process.chdir(prevCwd)
      origCallback()
    }

    this.push(file); // always send the same file through

    // Rest of the original code ...

That probably is not the right fix, but I'm not able to pin point where the correct fix should go.

walling commented 10 years ago

Sorry for the delay! I think it will be solved in PR #4, when it gets merged in. As I state there, I think the real issue is to be fixed in the raml-parser module, ie. by allowing you to specify a path when parsing RAML from a string.

walling commented 10 years ago

Fixed.

amr commented 10 years ago

Thanks for the heads up, but as you noted in #4, this doesn't fix this issue of including JSON schemas.

repocho commented 10 years ago

Yes, this PR should fix your issue. The other problem is when you import a json-schema from another json-schema.

Raml-parser only allows you to get the json-schema as string and if you want to parse it with another plugin you don't know the path of the file.

But again if the issue is to load a json-schema from a raml file with a relative path should be fixed with the https://github.com/walling/gulp-raml2html/pull/4

walling commented 10 years ago

@amr, do you have a concrete test case that exhibits the error you are experiencing when including JSON schemas. It should be solved by #4, I think. Please try it out with the latest gulp-raml2html.

@RePoChO, I think you right. Once again, it really should be solved in raml-parser and not in this module, since my only mission is to package raml2html for gulp usage. The issue though is that gulp use virtual files and raml-parser is programmed to work directly with file system or with a string (ignoring the path). There really should be an option to parse a string and providing a path as well. I'm not sure if they have a similar issue regarding JSON schemas, so they need to pass the path to the JSON schema parser, so it can recursively include other JSON schemas. I'm not really into their code base, I'm only hypothesizing here.

amr commented 10 years ago

I'm going to retest with latest master and update you.