<commit_before>import {Fury} from 'fury';
import APIBlueprintParser from 'fury-adapter-apib-parser';
import APIElementImporter from './APIElementImporter.js'

function promisify(func, thisArg) {
  return (...args) => new Promise((resolve, reject) => {
    func.bind(thisArg)(...args, (err, result) => {
      if (result) {
        resolve(result);
      } else {
        reject(err);
      }
    });
  });
}

@registerImporter
export default class APIBlueprintImporter {
  static identifier = 'io.apiary.PawExtensions.APIBlueprintImporter';
  static title = 'API Blueprint Importer';
  static inputs = [
    InputField("host", "Base HOST", "String", {placeholder: "https://example.com"}),
  ];

  constructor() {
    this.fury = new Fury();
    this.fury.use(APIBlueprintParser);
  }

  canImport(context, items) {
    for (const item of items) {
      if (APIBlueprintParser.detect(item.content)) {
        return true
      }
    }

    return false;
  }

  async import(context, items, options) {
    const defaultHost = options.inputs['host'];

    // FIXME remove promisify when https://github.com/apiaryio/api-elements.js/issues/37 is live
    const parse = promisify(this.fury.parse, this.fury);

    for (const item of items) {
      console.log('Importing Item');
      const parseResult = await parse({ source: item.content, mediaType: 'text/vnd.apiblueprint' });
      const importer = new APIElementImporter(context, defaultHost);
      importer.importAPI(parseResult.api);
    }

    return true;
  }
}<commit_msg>fix: expose API Blueprint parse errors to user<commit_after>import {Fury} from 'fury';
import APIBlueprintParser from 'fury-adapter-apib-parser';
import APIElementImporter from './APIElementImporter.js'

function promisify(func, thisArg) {
  return (...args) => new Promise((resolve, reject) => {
    func.bind(thisArg)(...args, (err, result) => {
      if (result) {
        resolve(result);
      } else {
        reject(err);
      }
    });
  });
}

@registerImporter
export default class APIBlueprintImporter {
  static identifier = 'io.apiary.PawExtensions.APIBlueprintImporter';
  static title = 'API Blueprint Importer';
  static inputs = [
    InputField("host", "Base HOST", "String", {placeholder: "https://example.com"}),
  ];

  constructor() {
    this.fury = new Fury();
    this.fury.use(APIBlueprintParser);
  }

  canImport(context, items) {
    for (const item of items) {
      if (APIBlueprintParser.detect(item.content)) {
        return true
      }
    }

    return false;
  }

  async import(context, items, options) {
    const defaultHost = options.inputs['host'];

    // FIXME remove promisify when https://github.com/apiaryio/api-elements.js/issues/37 is live
    const parse = promisify(this.fury.parse, this.fury);

    for (const item of items) {
      console.log('Importing Item');
      const parseResult = await parse({ source: item.content, mediaType: 'text/vnd.apiblueprint' });

      if (parseResult.api) {
        const importer = new APIElementImporter(context, defaultHost);
        importer.importAPI(parseResult.api);
      }

      parseResult.errors.forEach((error) => {
        const start = error.attributes.get('sourceMap').get(0).get(0).get(0);
        const line = start.attributes.get('line');
        const column = start.attributes.get('column');
        throw new Error(`${error.toValue()} on line ${line}:${column}`)
      });
    }

    return true;
  }
}
{'fraction_non_alphanumeric': 0.14090644973852412, 'fraction_numerical': 0.0020337013364323067, 'mean_word_length': 3.1733333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6931054', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1079, 'n_words': 241}