@miso.ai/doggoganger 0.9.0-beta.2 → 0.9.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/index.js +48 -0
- package/cli/server.js +1 -1
- package/package.json +6 -7
- package/src/index.js +11 -3
- package/src/utils.js +26 -0
- package/cli/nodemon.js +0 -21
package/cli/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { dirname, resolve, join } from 'path';
|
|
4
|
+
import nodemon from 'nodemon';
|
|
5
|
+
import yargs from 'yargs/yargs';
|
|
6
|
+
import { hideBin } from 'yargs/helpers';
|
|
7
|
+
import doggoganger from '../src/index.js';
|
|
8
|
+
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const SRC_DIR = join(__dirname, '../src');
|
|
11
|
+
|
|
12
|
+
let { watch, ...argv } = yargs(hideBin(process.argv))
|
|
13
|
+
.option('port', {
|
|
14
|
+
alias: 'p',
|
|
15
|
+
describe: 'Port of mock API endpoint',
|
|
16
|
+
default: 9901,
|
|
17
|
+
type: 'number',
|
|
18
|
+
})
|
|
19
|
+
.option('watch', {
|
|
20
|
+
alias: 'w',
|
|
21
|
+
describe: 'Watch files for changes',
|
|
22
|
+
type: 'boolean',
|
|
23
|
+
})
|
|
24
|
+
.option('serve', {
|
|
25
|
+
alias: 's',
|
|
26
|
+
describe: 'Serve static files as well',
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
})
|
|
29
|
+
.argv;
|
|
30
|
+
|
|
31
|
+
const { port, serve } = argv;
|
|
32
|
+
argv = { port, serve };
|
|
33
|
+
|
|
34
|
+
if (watch) {
|
|
35
|
+
const exec = `node ${resolve(__dirname, 'server.js')} ${JSON.stringify(JSON.stringify(argv))}`;
|
|
36
|
+
nodemon({
|
|
37
|
+
exec,
|
|
38
|
+
watch: argv.static ? [SRC_DIR, '.'] : SRC_DIR,
|
|
39
|
+
ignore: ['*/node_modules/*'],
|
|
40
|
+
//ext: 'js json yaml',
|
|
41
|
+
verbose: true,
|
|
42
|
+
}).on('log', ({ colour }) => {
|
|
43
|
+
console.log(colour);
|
|
44
|
+
}).on('quit', () => process.exit());
|
|
45
|
+
|
|
46
|
+
} else {
|
|
47
|
+
doggoganger(argv);
|
|
48
|
+
}
|
package/cli/server.js
CHANGED
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miso.ai/doggoganger",
|
|
3
|
-
"version": "0.9.0-beta.
|
|
3
|
+
"version": "0.9.0-beta.4",
|
|
4
4
|
"description": "A dummy miso endpoint for demo and testing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
|
-
"
|
|
8
|
-
"doggoganger": "cli/server.js"
|
|
7
|
+
"doggoganger": "cli/index.js"
|
|
9
8
|
},
|
|
10
9
|
"publishConfig": {
|
|
11
10
|
"access": "public"
|
|
@@ -32,9 +31,9 @@
|
|
|
32
31
|
"js-yaml": "^4.1.0",
|
|
33
32
|
"koa": "^2.14.1",
|
|
34
33
|
"koa-body": "^6.0.1",
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
34
|
+
"koa-static": "^5.0.0",
|
|
35
|
+
"nodemon": "^2.0.15",
|
|
36
|
+
"uuid": "^8.3.2",
|
|
37
|
+
"yargs": "^17.5.1"
|
|
39
38
|
}
|
|
40
39
|
}
|
package/src/index.js
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
import Koa from 'koa';
|
|
2
2
|
import Router from '@koa/router';
|
|
3
3
|
import cors from '@koa/cors';
|
|
4
|
+
import serveStatic from 'koa-static';
|
|
4
5
|
import { koaBody } from 'koa-body';
|
|
5
6
|
import api from './api/index.js';
|
|
7
|
+
import { exclusion } from './utils.js';
|
|
6
8
|
|
|
7
|
-
export default function doggoganger() {
|
|
9
|
+
export default function doggoganger({ port = 9901, serve = false } = {}) {
|
|
8
10
|
const app = new Koa();
|
|
9
11
|
const router = new Router();
|
|
10
12
|
|
|
11
13
|
router.use('/api', api.routes(), api.allowedMethods());
|
|
12
|
-
|
|
14
|
+
|
|
15
|
+
if (serve) {
|
|
16
|
+
app
|
|
17
|
+
.use(exclusion)
|
|
18
|
+
.use(serveStatic('.'));
|
|
19
|
+
}
|
|
20
|
+
|
|
13
21
|
app
|
|
14
22
|
.use(cors())
|
|
15
23
|
.use(koaBody())
|
|
16
24
|
.use(router.routes())
|
|
17
25
|
.use(router.allowedMethods())
|
|
18
|
-
.listen(
|
|
26
|
+
.listen(port);
|
|
19
27
|
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
function shallServe(path) {
|
|
2
|
+
if (path.startsWith('/node_modules/') || path.startsWith('.')) {
|
|
3
|
+
return false;
|
|
4
|
+
}
|
|
5
|
+
const i = path.lastIndexOf('.');
|
|
6
|
+
const ext = i < 0 ? path.substring(i + 1) : '';
|
|
7
|
+
switch (ext) {
|
|
8
|
+
case 'md':
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
switch (path) {
|
|
12
|
+
case 'package.json':
|
|
13
|
+
case 'package-lock.json':
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function exclusion(ctx, next) {
|
|
20
|
+
const { path } = ctx;
|
|
21
|
+
if (shallServe(path)) {
|
|
22
|
+
await next();
|
|
23
|
+
} else {
|
|
24
|
+
ctx.status = 404;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/cli/nodemon.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { fileURLToPath } from 'url';
|
|
3
|
-
import { dirname, resolve, join } from 'path';
|
|
4
|
-
import nodemon from 'nodemon';
|
|
5
|
-
|
|
6
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
-
|
|
8
|
-
nodemon({
|
|
9
|
-
script: resolve(__dirname, 'server.js'),
|
|
10
|
-
watch: join(__dirname, '../src'),
|
|
11
|
-
ext: 'js json yaml',
|
|
12
|
-
verbose: true,
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
nodemon.on('log', ({ colour }) => {
|
|
16
|
-
console.log(colour);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
nodemon.on('quit', () => {
|
|
20
|
-
process.exit();
|
|
21
|
-
});
|