From df149c543b9dc197ed4465313d14b509242bbc66 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 10 Jun 2017 15:58:34 -0700 Subject: [PATCH] make dist absolutePath browser-safe, add test --- package.json | 2 +- swagger-ui-dist-package/absolute-path.js | 14 ++++++++++++++ swagger-ui-dist-package/index.js | 8 +++----- test/swagger-ui-dist-package/absolute-path.js | 13 +++++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 swagger-ui-dist-package/absolute-path.js create mode 100644 test/swagger-ui-dist-package/absolute-path.js diff --git a/package.json b/package.json index 405fddc1..40ad9d5a 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/absolute-path.js b/swagger-ui-dist-package/absolute-path.js new file mode 100644 index 00000000..650e7dbf --- /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) + } + return "Error! absolutePath only available when running in NodeJS" +} + +module.exports = getAbsoluteFSPath() diff --git a/swagger-ui-dist-package/index.js b/swagger-ui-dist-package/index.js index 00fa198b..018196a9 100644 --- a/swagger-ui-dist-package/index.js +++ b/swagger-ui-dist-package/index.js @@ -1,6 +1,4 @@ -const path = require('path') +module.exports.SwaggerUIBundle = require("./swagger-ui-bundle.js") +module.exports.SwaggerUIStandalonePreset = require("./swagger-ui-standalone-preset.js") +module.exports.absolutePath = require("./.absolute-path.js") -module.exports.SwaggerUIBundle = require('./swagger-ui-bundle.js') -module.exports.SwaggerUIStandalonePreset = require('./swagger-ui-standalone-preset.js') - -module.exports.absolutePath = path.resolve(__dirname) 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 00000000..aba27933 --- /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 absolutePath from "../../swagger-ui-dist-package/absolute-path" + +describe("swagger-ui-dist", function(){ + describe("absolutePath", function(){ + it("has absolute path", function(){ + const expectedPath = path.resolve(__dirname, "../../swagger-ui-dist-package") + expect(absolutePath).toEqual(expectedPath) + }) + }) +})