1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
4 | var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
|
---|
5 | exports.__esModule = true;
|
---|
6 | exports.default = void 0;
|
---|
7 | var _ramda = require("ramda");
|
---|
8 | var url = _interopRequireWildcard(require("../util/url.cjs"));
|
---|
9 | var _File = _interopRequireDefault(require("../util/File.cjs"));
|
---|
10 | var plugins = _interopRequireWildcard(require("../util/plugins.cjs"));
|
---|
11 | var _ParseError = _interopRequireDefault(require("../errors/ParseError.cjs"));
|
---|
12 | var _UnmatchedResolverError = _interopRequireDefault(require("../errors/UnmatchedResolverError.cjs"));
|
---|
13 | var _util = require("../resolve/util.cjs");
|
---|
14 | /**
|
---|
15 | * Parses the given file's contents, using the configured parser plugins.
|
---|
16 | */
|
---|
17 | const parseFile = async (file, options) => {
|
---|
18 | const optsBoundParsers = options.parse.parsers.map(parser => {
|
---|
19 | const clonedParser = Object.create(parser);
|
---|
20 | return Object.assign(clonedParser, options.parse.parserOpts);
|
---|
21 | });
|
---|
22 | const parsers = await plugins.filter('canParse', file, optsBoundParsers);
|
---|
23 |
|
---|
24 | // we couldn't find any parser for this File
|
---|
25 | if ((0, _ramda.isEmpty)(parsers)) {
|
---|
26 | throw new _UnmatchedResolverError.default(file.uri);
|
---|
27 | }
|
---|
28 | try {
|
---|
29 | const {
|
---|
30 | plugin,
|
---|
31 | result
|
---|
32 | } = await plugins.run('parse', [file], parsers);
|
---|
33 |
|
---|
34 | // empty files handling
|
---|
35 | if (!plugin.allowEmpty && result.isEmpty) {
|
---|
36 | return Promise.reject(new _ParseError.default(`Error while parsing file "${file.uri}". File is empty.`));
|
---|
37 | }
|
---|
38 | return result;
|
---|
39 | } catch (error) {
|
---|
40 | throw new _ParseError.default(`Error while parsing file "${file.uri}"`, {
|
---|
41 | cause: error
|
---|
42 | });
|
---|
43 | }
|
---|
44 | };
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Parses a file into ApiDOM.
|
---|
48 | */
|
---|
49 | const parse = async (uri, options) => {
|
---|
50 | /**
|
---|
51 | * If the path is a filesystem path, then convert it to a URL.
|
---|
52 | *
|
---|
53 | * NOTE: According to the JSON Reference spec, these should already be URLs,
|
---|
54 | * but, in practice, many people use local filesystem paths instead.
|
---|
55 | * So we're being generous here and doing the conversion automatically.
|
---|
56 | * This is not intended to be a 100% bulletproof solution.
|
---|
57 | * If it doesn't work for your use-case, then use a URL instead.
|
---|
58 | */
|
---|
59 | const file = (0, _File.default)({
|
---|
60 | uri: url.sanitize(url.stripHash(uri)),
|
---|
61 | mediaType: options.parse.mediaType
|
---|
62 | });
|
---|
63 | const data = await (0, _util.readFile)(file, options);
|
---|
64 | return parseFile((0, _File.default)({
|
---|
65 | ...file,
|
---|
66 | data
|
---|
67 | }), options);
|
---|
68 | };
|
---|
69 | var _default = exports.default = parse; |
---|