8000 Base functionality and tests · Codermar/openapi-editor@58c8000 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58c8000

Browse files
committed
Base functionality and tests
1 parent ca616f7 commit 58c8000

File tree

5 files changed

+168
-1
lines changed

5 files changed

+168
-1
lines changed

src/browser.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import browser from './browser';
2+
import { expect } from 'chai';
3+
import 'mocha';
4+
5+
describe('Browser', () => {
6+
7+
it('It Should expect open to be function', () => {
8+
expect(typeof browser.open).to.equals('function');
9+
});
10+
});

src/browser.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
import Child from 'child_process';
3+
import config from './config';
4+
5+
const platformOpeners: any = {
6+
darwin(url: string, cb: (err: any) => void) {
7+
const browser = escape(config.browser);
8+
if (browser) {
9+
open('open -a ' + browser, url, cb);
10+
} else {
11+
open('open', url, cb);
12+
}
13+
},
14+
15+
win32(url: string, cb: (err: any) => void) {
16+
const browser = escape(config.browser);
17+
if (browser) {
18+
open('start "" "' + browser + '"', url, cb);
19+
} else {
20+
open('start ""', url, cb);
21+
}
22+
},
23+
24+
linux(url: string, cb: (err: any) => void) {
25+
const browser = escape(config.browser);
26+
if (browser) {
27+
open(browser, url, cb);
28+
} else {
29+
open('xdg-open', url, cb);
30+
}
31+
},
32+
33+
other(url: string, cb: (err: any) => void) {
34+
const browser = escape(config.browser);
35+
if (browser) {
36+
open(browser, url, cb);
37+
} else {
38+
cb(new Error('must specify browser in config'));
39+
}
40+
},
41+
};
42+
43+
// note: platform parameter is just for testing...
44+
function platformOpen(url: string, cb: (err: any) => void, platform: string) {
45+
platform = platform || process.platform;
46+
if (!platformOpeners[platform]) { platform = 'other'; }
47+
platformOpeners[platform](url, cb);
48+
}
49+
50+
function open(command: any, url: string, cb: (err: any) => void) {
51+
if (config.debug) { console.log('command: ' + command); }
52+
console.log('Opening browser to: ' + url);
53+
Child.exec(command + ' "' + escape(url) + '"', cb);
54+
}
55+
56+
function escape(s: string) {
57+
if (!s) { return s; }
58+
return s.replace(/"/g, '\\\"');
59+
}
60+
// const escape = (s: string) => !s ? s : s.replace(/"/g, '\\\"');
61+
62+
export default {
63+
open: platformOpen,
64+
};

src/config.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import config from './config';
2+
import { expect } from 'chai';
3+
import 'mocha';
4+
5+
describe('Config', () => {
6+
7+
it('It Should expect config to be object', () => {
8+
expect(typeof config).to.equals('object');
9+
});
10+
});

src/config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import path from 'path';
3+
4+
interface Config {
5+
rootDir: string;
6+
editorDir: string;
7+
userHome?: string;
8+
debug: boolean;
9+
nodeModules: string;
10+
editorConfig: object;
11+
browser: string;
12+
}
13+
14+
const config: Config = {
15+
rootDir: path.resolve(__dirname, '..'),
16+
editorDir: '',
17+
userHome: process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'],
18+
debug: !!process.env.DEBUG,
19+
nodeModules: '',
20+
editorConfig: {}, // placeholder for now. Not much is configurable in swagger-editor 3.x
21+
browser: '',
22+
};
23+
24+
config.nodeModules = path.resolve(config.rootDir, 'node_modules');
25+
config.editorDir = path.resolve(config.nodeModules, 'swagger-editor-dist');
26+
27+
export default config;

src/index.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,63 @@
33
*
44
* OpenAPI Editor
55
*/
6+
import path from 'path';
7+
import fs from 'fs';
8+
import colors from 'colors';
9+
import express from 'express';
10+
import serveStatic from 'serve-static';
11+
import config from './config';
612

7-
export const edit = (options: object): void => {
13+
interface Options {
14+
oasFilePath: string;
15+
port?: number;
16+
silent?: boolean; // invoque browser or run silently
17+
}
18+
19+
// swagger-editor must be served from root
20+
const SWAGGER_EDITOR_SERVE_PATH = '/';
21+
// swagger-editor expects to GET the file here
22+
const SWAGGER_EDITOR_LOAD_PATH = '/oas/spec';
23+
// swagger-editor PUTs the file back here
24+
const SWAGGER_EDITOR_SAVE_PATH = '/oas/spec';
25+
// map dir for UI
26+
const SWAGGER_EDITOR_UI_PATH = '/swagger-editor';
27+
// swagger-editor GETs the configuration files
28+
const SWAGGER_EDITOR_CONFIG_PATH = '/config/defaults.json';
29+
30+
export const edit = (options: Options): void => {
831
console.log('*** OpenAPI Editor Options:', options);
32+
console.log('config: ', config);
33+
34+
if (!fs.existsSync(options.oasFilePath)) {
35+
console.error(colors.red(`The OAS file provided ${options.oasFilePath} does not exist.`));
36+
return;
37+
}
38+
const app = require('connect')();
39+
// save the file from swagger-editor
40+
app.use(SWAGGER_EDITOR_SAVE_PATH, (req: express.Request, res: express.Response, next: express.NextFunction) => {
41+
if (req.method !== 'PUT') { return next(); }
42+
43+
const stream = fs.createWriteStream(options.oasFilePath);
44+
req.pipe(stream);
45+
46+
stream.on('finish', () => {
47+
res.end('ok');
48+
});
49+
});
50+
51+
// retrieve the project swagger file for the swagger-editor
52+
app.use(SWAGGER_EDITOR_LOAD_PATH, serveStatic(options.oasFilePath));
53+
54+
app.use(SWAGGER_EDITOR_CONFIG_PATH, (req: express.Request, res: express.Response, next: express.NextFunction) => {
55+
if (req.method !== 'GET') { return next(); }
56+
res.end(JSON.stringify(config.editorConfig));
57+
});
58+
59+
// load swagger-editor with use custom index
60+
app.use(SWAGGER_EDITOR_SERVE_PATH, serveStatic(path.resolve(__dirname, '..', 'src/')));
61+
app.use(SWAGGER_EDITOR_UI_PATH, serveStatic(config.editorDir));
62+
63+
const editorIndexFile = `${config.editorDir}/index.html`;
64+
965
};

0 commit comments

Comments
 (0)
0