diff --git a/package.json b/package.json index 405fddc141964eef7932887933ea6b1b6dbdc21e..40ad9d5a0df186a8fdd6059f86453d79e0ba18c1 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "test": "npm run lint-errors && npm run just-test-in-node", "test-in-node": "npm run lint-errors && npm run just-test-in-node", "just-test": "karma start --config karma.conf.js", - "just-test-in-node": "mocha --recursive --compilers js:babel-core/register test/core test/components test/bugs" + "just-test-in-node": "mocha --recursive --compilers js:babel-core/register test/core test/components test/bugs test/swagger-ui-dist-package" }, "dependencies": { "babel-polyfill": "^6.23.0", diff --git a/swagger-ui-dist-package/README.md b/swagger-ui-dist-package/README.md index eff7655ca4241fefdfe0631631cc6d46eb04f5ae..662842270a5e876f886b93608b5ad957ffa409b9 100644 --- a/swagger-ui-dist-package/README.md +++ b/swagger-ui-dist-package/README.md @@ -1,8 +1,22 @@ -This module, `swagger-ui-dist`, exposes Swagger-UI's entire dist folder as a dependency-free npm module. Use `swagger-ui` instead, if you'd like to have npm install dependencies for you. +# Swagger UI Dist +[![NPM version](https://badge.fury.io/js/swagger-ui-dist.svg)](http://badge.fury.io/js/swagger-ui-dist) + +# API + +This module, `swagger-ui-dist`, exposes Swagger-UI's entire dist folder as a dependency-free npm module. +Use `swagger-ui` instead, if you'd like to have npm install dependencies for you. `SwaggerUIBundle` and `SwaggerUIStandalonePreset` can be imported: ```javascript - import { SwaggerUIBundle, SwaggerUIStandalonePreset } from 'swagger-ui-dist' + import { SwaggerUIBundle, SwaggerUIStandalonePreset } from "swagger-ui-dist" +``` + +To get an absolute path to this directory for static file serving, use the exported `getAbsoluteFSPath` method: + +```javascript +const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath() + +// then instantiate server that serves files from the swaggerUiAssetPath ``` For anything else, check the [Swagger-UI](https://github.com/swagger-api/swagger-ui) repository. diff --git a/swagger-ui-dist-package/absolute-path.js b/swagger-ui-dist-package/absolute-path.js new file mode 100644 index 0000000000000000000000000000000000000000..26a3cc01a1e0d74f87ba545b1e228f90cdb1fb89 --- /dev/null +++ b/swagger-ui-dist-package/absolute-path.js @@ -0,0 +1,14 @@ +/* + * getAbsoluteFSPath + * @return {string} When run in NodeJS env, returns the absolute path to the current directory + * When run outside of NodeJS, will return an error message + */ +const getAbsoluteFSPath = () => { + // detect whether we are running in a browser or nodejs + if (typeof module !== "undefined" && module.exports) { + return require("path").resolve(__dirname) + } + throw new Error('getAbsoluteFSPath can only be called within a Nodejs environment'); +} + +module.exports = getAbsoluteFSPath diff --git a/swagger-ui-dist-package/index.js b/swagger-ui-dist-package/index.js index f8b7191ca4797f56680ce822eff5ea5c783ea3c1..018196a9d34df5941a75cb65ef79b13e4a4dd937 100644 --- a/swagger-ui-dist-package/index.js +++ b/swagger-ui-dist-package/index.js @@ -1,2 +1,4 @@ -module.exports.SwaggerUIBundle = require('./swagger-ui-bundle.js') -module.exports.SwaggerUIStandalonePreset = require('./swagger-ui-standalone-preset.js') +module.exports.SwaggerUIBundle = require("./swagger-ui-bundle.js") +module.exports.SwaggerUIStandalonePreset = require("./swagger-ui-standalone-preset.js") +module.exports.absolutePath = require("./.absolute-path.js") + diff --git a/test/swagger-ui-dist-package/absolute-path.js b/test/swagger-ui-dist-package/absolute-path.js new file mode 100644 index 0000000000000000000000000000000000000000..252a298ab4ecf9da2f45f6cc7ff69c868c9ee6c2 --- /dev/null +++ b/test/swagger-ui-dist-package/absolute-path.js @@ -0,0 +1,13 @@ +/* eslint-env mocha */ +import expect from "expect" +import path from "path" +import getAbsoluteFSPath from "../../swagger-ui-dist-package/absolute-path" + +describe("swagger-ui-dist", function(){ + describe("getAbsoluteFSPath", function(){ + it("returns absolute path", function(){ + const expectedPath = path.resolve(__dirname, "../../swagger-ui-dist-package") + expect(getAbsoluteFSPath()).toEqual(expectedPath) + }) + }) +})