diff --git a/packages/apidom-parser-adapter-api-design-systems-json/.eslintignore b/packages/apidom-parser-adapter-api-design-systems-json/.eslintignore index 856320e627..23853b909a 100644 --- a/packages/apidom-parser-adapter-api-design-systems-json/.eslintignore +++ b/packages/apidom-parser-adapter-api-design-systems-json/.eslintignore @@ -1,8 +1,8 @@ +/**/*.js +/**/*.mjs +/**/*.cjs /dist -/es -/cjs /types /config /.nyc_output /node_modules -/**/*.js diff --git a/packages/apidom-parser-adapter-api-design-systems-json/.gitignore b/packages/apidom-parser-adapter-api-design-systems-json/.gitignore index 10a7f0b54d..7615bb1609 100644 --- a/packages/apidom-parser-adapter-api-design-systems-json/.gitignore +++ b/packages/apidom-parser-adapter-api-design-systems-json/.gitignore @@ -1,6 +1,7 @@ +/src/**/*.mjs +/src/**/*.cjs +/test/**/*.mjs /dist -/es -/cjs /types /NOTICE /swagger-api-apidom-parser-adapter-api-design-systems-json-*.tgz diff --git a/packages/apidom-parser-adapter-api-design-systems-json/.mocharc.json b/packages/apidom-parser-adapter-api-design-systems-json/.mocharc.json index e923c24114..f4f6530232 100644 --- a/packages/apidom-parser-adapter-api-design-systems-json/.mocharc.json +++ b/packages/apidom-parser-adapter-api-design-systems-json/.mocharc.json @@ -1,5 +1,5 @@ { "recursive": true, - "spec": "test/**/*.ts", - "file": ["test/mocha-bootstrap.cjs"] + "spec": "test/**/*.mjs", + "file": ["test/mocha-bootstrap.mjs"] } diff --git a/packages/apidom-parser-adapter-api-design-systems-json/cjs/adapter.cjs b/packages/apidom-parser-adapter-api-design-systems-json/cjs/adapter.cjs new file mode 100644 index 0000000000..a33b4ab560 --- /dev/null +++ b/packages/apidom-parser-adapter-api-design-systems-json/cjs/adapter.cjs @@ -0,0 +1,32 @@ +"use strict"; + +var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default; +var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default; +exports.__esModule = true; +exports.parse = exports.namespace = exports.mediaTypes = exports.detectionRegExp = exports.detect = void 0; +var _ramda = require("ramda"); +var _ramdaAdjunct = require("ramda-adjunct"); +var _apidomCore = require("@swagger-api/apidom-core"); +var _apidomParserAdapterJson = require("@swagger-api/apidom-parser-adapter-json"); +var _apidomNsApiDesignSystems = _interopRequireWildcard(require("@swagger-api/apidom-ns-api-design-systems")); +var _mediaTypes = _interopRequireDefault(require("./media-types.cjs")); +exports.mediaTypes = _mediaTypes.default; +const detectionRegExp = exports.detectionRegExp = /"version"\s*:\s*"(?2021-05-07)"/; +const detect = async source => detectionRegExp.test(source) && (await (0, _apidomParserAdapterJson.detect)(source)); +exports.detect = detect; +const parse = async (source, options = {}) => { + const refractorOpts = (0, _ramda.propOr)({}, 'refractorOpts', options); + const parserOpts = (0, _ramda.omit)(['refractorOpts'], options); + const parseResultElement = await (0, _apidomParserAdapterJson.parse)(source, parserOpts); + const { + result + } = parseResultElement; + if ((0, _ramdaAdjunct.isNotUndefined)(result)) { + const mainElement = _apidomNsApiDesignSystems.MainElement.refract(result, refractorOpts); + mainElement.classes.push('result'); + parseResultElement.replaceResult(mainElement); + } + return parseResultElement; +}; +exports.parse = parse; +const namespace = exports.namespace = (0, _apidomCore.createNamespace)(_apidomNsApiDesignSystems.default); \ No newline at end of file diff --git a/packages/apidom-parser-adapter-api-design-systems-json/cjs/media-types.cjs b/packages/apidom-parser-adapter-api-design-systems-json/cjs/media-types.cjs new file mode 100644 index 0000000000..c6a4dfe50c --- /dev/null +++ b/packages/apidom-parser-adapter-api-design-systems-json/cjs/media-types.cjs @@ -0,0 +1,7 @@ +"use strict"; + +exports.__esModule = true; +exports.default = void 0; +var _apidomNsApiDesignSystems = require("@swagger-api/apidom-ns-api-design-systems"); +const jsonMediaTypes = new _apidomNsApiDesignSystems.ApiDesignSystemsMediaTypes(..._apidomNsApiDesignSystems.mediaTypes.filterByFormat('generic'), ..._apidomNsApiDesignSystems.mediaTypes.filterByFormat('json')); +var _default = exports.default = jsonMediaTypes; \ No newline at end of file diff --git a/packages/apidom-parser-adapter-api-design-systems-json/es/adapter.mjs b/packages/apidom-parser-adapter-api-design-systems-json/es/adapter.mjs new file mode 100644 index 0000000000..33912bf955 --- /dev/null +++ b/packages/apidom-parser-adapter-api-design-systems-json/es/adapter.mjs @@ -0,0 +1,23 @@ +import { propOr, omit } from 'ramda'; +import { isNotUndefined } from 'ramda-adjunct'; +import { createNamespace } from '@swagger-api/apidom-core'; +import { parse as parseJSON, detect as detectJSON } from '@swagger-api/apidom-parser-adapter-json'; +import apiDesignSystemsNamespace, { MainElement } from '@swagger-api/apidom-ns-api-design-systems'; +export { default as mediaTypes } from "./media-types.mjs"; +export const detectionRegExp = /"version"\s*:\s*"(?2021-05-07)"/; +export const detect = async source => detectionRegExp.test(source) && (await detectJSON(source)); +export const parse = async (source, options = {}) => { + const refractorOpts = propOr({}, 'refractorOpts', options); + const parserOpts = omit(['refractorOpts'], options); + const parseResultElement = await parseJSON(source, parserOpts); + const { + result + } = parseResultElement; + if (isNotUndefined(result)) { + const mainElement = MainElement.refract(result, refractorOpts); + mainElement.classes.push('result'); + parseResultElement.replaceResult(mainElement); + } + return parseResultElement; +}; +export const namespace = createNamespace(apiDesignSystemsNamespace); \ No newline at end of file diff --git a/packages/apidom-parser-adapter-api-design-systems-json/es/media-types.mjs b/packages/apidom-parser-adapter-api-design-systems-json/es/media-types.mjs new file mode 100644 index 0000000000..630fd0e4b4 --- /dev/null +++ b/packages/apidom-parser-adapter-api-design-systems-json/es/media-types.mjs @@ -0,0 +1,3 @@ +import { mediaTypes, ApiDesignSystemsMediaTypes } from '@swagger-api/apidom-ns-api-design-systems'; +const jsonMediaTypes = new ApiDesignSystemsMediaTypes(...mediaTypes.filterByFormat('generic'), ...mediaTypes.filterByFormat('json')); +export default jsonMediaTypes; \ No newline at end of file diff --git a/packages/apidom-parser-adapter-api-design-systems-json/package.json b/packages/apidom-parser-adapter-api-design-systems-json/package.json index 7d63bd0c54..c4c26e4878 100644 --- a/packages/apidom-parser-adapter-api-design-systems-json/package.json +++ b/packages/apidom-parser-adapter-api-design-systems-json/package.json @@ -9,24 +9,24 @@ "type": "module", "sideEffects": false, "unpkg": "./dist/apidom-parser-adapter-api-design-systems-json.browser.min.js", - "main": "./cjs/adapter.cjs", + "main": "./src/adapter.cjs", "exports": { "types": "./types/dist.d.ts", - "import": "./es/adapter.mjs", - "require": "./cjs/adapter.cjs" + "import": "./src/adapter.mjs", + "require": "./src/adapter.cjs" }, "types": "./types/dist.d.ts", "scripts": { "build": "npm run clean && run-p --max-parallel ${CPU_CORES:-2} typescript:declaration build:es build:cjs build:umd:browser", - "build:es": "cross-env BABEL_ENV=es babel src --out-dir es --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'", - "build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir cjs --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'", + "build:es": "cross-env BABEL_ENV=es babel src --out-dir src --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'", + "build:cjs": "cross-env BABEL_ENV=cjs babel src --out-dir src --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'", "build:umd:browser": "cross-env BABEL_ENV=browser webpack --config config/webpack/browser.config.js --progress", "lint": "eslint ./", "lint:fix": "eslint ./ --fix", - "clean": "rimraf ./es ./cjs ./dist ./types", + "clean": "rimraf --glob 'src/**/*.mjs' 'src/**/*.cjs' 'test/**/*.mjs' ./dist ./types", "typescript:check-types": "tsc --noEmit", "typescript:declaration": "tsc -p declaration.tsconfig.json && rollup -c config/rollup/types.dist.js", - "test": "cross-env NODE_ENV=test BABEL_ENV=cjs mocha", + "test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test mocha", "prepack": "copyfiles -u 3 ../../LICENSES/* LICENSES && copyfiles -u 2 ../../NOTICE .", "postpack": "rimraf NOTICE LICENSES" }, diff --git a/packages/apidom-parser-adapter-api-design-systems-json/test/.eslintrc b/packages/apidom-parser-adapter-api-design-systems-json/test/.eslintrc index 85759a803c..c47eea4f48 100644 --- a/packages/apidom-parser-adapter-api-design-systems-json/test/.eslintrc +++ b/packages/apidom-parser-adapter-api-design-systems-json/test/.eslintrc @@ -24,15 +24,32 @@ "mocha/no-top-level-hooks": 2, "mocha/no-identical-title": 2, "mocha/no-nested-tests": 2, - "mocha/no-exclusive-tests": 2 - }, - "overrides": [{ - "files": ["mocha-bootstrap.cjs"], - "parserOptions": { - "sourceType": "script" - }, - "rules": { - "@typescript-eslint/no-var-requires": 0 - } - }] + "mocha/no-exclusive-tests": 2, + "no-underscore-dangle": 0, + "import/no-relative-packages": 0, + "@typescript-eslint/naming-convention": [ + "error", + { + "selector": "variable", + "format": ["camelCase", "PascalCase", "UPPER_CASE"], + "leadingUnderscore": "forbid" + }, + { + "selector": "variable", + "format": null, + "filter": { + "regex": "^__dirname$", + "match": true + } + }, + { + "selector": "variable", + "format": null, + "filter": { + "regex": "^__filename$", + "match": true + } + } + ] + } } diff --git a/packages/apidom-parser-adapter-api-design-systems-json/test/__snapshots__/adapter.ts.snap b/packages/apidom-parser-adapter-api-design-systems-json/test/__snapshots__/adapter.mjs.snap similarity index 100% rename from packages/apidom-parser-adapter-api-design-systems-json/test/__snapshots__/adapter.ts.snap rename to packages/apidom-parser-adapter-api-design-systems-json/test/__snapshots__/adapter.mjs.snap diff --git a/packages/apidom-parser-adapter-api-design-systems-json/test/adapter.ts b/packages/apidom-parser-adapter-api-design-systems-json/test/adapter.ts index db3e7522da..303c1c64cf 100644 --- a/packages/apidom-parser-adapter-api-design-systems-json/test/adapter.ts +++ b/packages/apidom-parser-adapter-api-design-systems-json/test/adapter.ts @@ -1,11 +1,13 @@ import fs from 'node:fs'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { assert, expect } from 'chai'; import { isParseResultElement, sexprs } from '@swagger-api/apidom-core'; import { isMainElement } from '@swagger-api/apidom-ns-api-design-systems'; import * as adapter from '../src/adapter'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const jsonSpec = fs .readFileSync(path.join(__dirname, 'fixtures', 'api-design-systems.json')) .toString(); diff --git a/packages/apidom-parser-adapter-api-design-systems-json/test/mocha-bootstrap.cjs b/packages/apidom-parser-adapter-api-design-systems-json/test/mocha-bootstrap.cjs deleted file mode 100644 index bc8ce3c8fc..0000000000 --- a/packages/apidom-parser-adapter-api-design-systems-json/test/mocha-bootstrap.cjs +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable */ -require('@babel/register')({ extensions: ['.js', '.ts'], rootMode: 'upward' }); - -/** - * Configure snapshot testing. - */ -const chai = require('chai'); -const { jestSnapshotPlugin, addSerializer } = require('mocha-chai-jest-snapshot'); - -const jestApiDOMSerializer = require('../../../scripts/jest-serializer-apidom.cjs'); -const jestStringSerializer = require('../../../scripts/jest-serializer-string.cjs'); - -chai.use(jestSnapshotPlugin()); -addSerializer(jestApiDOMSerializer); -addSerializer(jestStringSerializer); diff --git a/packages/apidom-parser-adapter-api-design-systems-json/test/mocha-bootstrap.ts b/packages/apidom-parser-adapter-api-design-systems-json/test/mocha-bootstrap.ts new file mode 100644 index 0000000000..4f8d8adf2e --- /dev/null +++ b/packages/apidom-parser-adapter-api-design-systems-json/test/mocha-bootstrap.ts @@ -0,0 +1,11 @@ +import * as chai from 'chai'; +import { jestSnapshotPlugin, addSerializer } from 'mocha-chai-jest-snapshot'; + +// @ts-ignore +import * as jestApiDOMSerializer from '../../../scripts/jest-serializer-apidom'; +// @ts-ignore +import * as jestStringSerializer from '../../../scripts/jest-serializer-string'; + +chai.use(jestSnapshotPlugin()); +addSerializer(jestApiDOMSerializer); +addSerializer(jestStringSerializer);