2016-01-22 08:22:11 -06:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import Console from './readers/Console'
|
|
|
|
import BasicConsole from './readers/BasicConsole'
|
|
|
|
import Inspector from './transforms/Inspector'
|
|
|
|
import ExpressInspector from './transforms/ExpressInspector'
|
|
|
|
import ExpressExtractor from './transforms/ExpressExtractor'
|
2016-01-24 18:03:01 -06:00
|
|
|
import JSON2Converter from './transforms/JSON2Converter'
|
2016-01-22 08:22:11 -06:00
|
|
|
import ErrorExtractor from './transforms/ErrorExtractor'
|
|
|
|
import MongoDB from './writers/MongoDB'
|
|
|
|
import SocketIO from './writers/SocketIO'
|
|
|
|
import DefaultConsole from './writers/DefaultConsole'
|
|
|
|
import {create} from './routers/viewer'
|
|
|
|
import NwBuilder from 'nw-builder'
|
|
|
|
import rc from 'rc'
|
2016-01-22 19:16:53 -06:00
|
|
|
import nativePackage from './../native/package.json'
|
2016-01-24 18:03:01 -06:00
|
|
|
import extend from 'extend'
|
2016-01-22 08:22:11 -06:00
|
|
|
|
|
|
|
export const Writer = {MongoDB, DefaultConsole};
|
|
|
|
export const Reader = {BasicConsole, Console};
|
2016-01-24 18:03:01 -06:00
|
|
|
export const Transform = {Inspector, ExpressInspector, ExpressExtractor, ErrorExtractor, JSON2Converter};
|
2016-01-22 08:22:11 -06:00
|
|
|
|
|
|
|
const defaultOpts = {
|
|
|
|
name: 'Scribe',
|
|
|
|
mongoUri: 'mongodb://localhost/scribe',
|
2016-01-24 18:03:01 -06:00
|
|
|
mongo: true,
|
2016-01-22 08:22:11 -06:00
|
|
|
basePath: 'scribe/',
|
|
|
|
socketPort: 4000,
|
2016-01-24 18:03:01 -06:00
|
|
|
socket: true,
|
2016-01-29 01:31:33 -06:00
|
|
|
inspector: {
|
|
|
|
colors: true,
|
|
|
|
showHidden: false,
|
|
|
|
depth: 5,
|
|
|
|
pre: true,
|
|
|
|
callsite: true,
|
|
|
|
tags: true,
|
|
|
|
args: true,
|
|
|
|
metrics: true
|
|
|
|
},
|
2016-01-22 08:22:11 -06:00
|
|
|
web: {
|
|
|
|
router: {
|
|
|
|
username: 'build',
|
|
|
|
password: 'build',
|
|
|
|
authentication: true,
|
|
|
|
sessionSecret: 'scribe-session',
|
|
|
|
useBodyParser: true,
|
|
|
|
useSession: true
|
|
|
|
},
|
|
|
|
client: {
|
|
|
|
socketPorts: [4000],
|
|
|
|
exposed: {
|
|
|
|
all: {label: 'all', query: {expose: {$exists: true}}},
|
2016-02-02 14:18:22 -06:00
|
|
|
//error: {label: 'error', query: {expose: 'error'}},
|
|
|
|
//express: {label: 'express', query: {expose: 'express'}},
|
|
|
|
//info: {label: 'info', query: {expose: 'info'}},
|
|
|
|
//log: {label: 'log', query: {expose: 'log'}},
|
|
|
|
//warn: {label: 'warn', query: {expose: 'warn'}},
|
|
|
|
//trace: {label: 'trace', query: {expose: 'trace'}}
|
2016-01-22 08:22:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
nwjs: {},
|
|
|
|
debug: false
|
|
|
|
};
|
|
|
|
|
2016-01-25 22:44:36 -06:00
|
|
|
export default function (id = process.pid, opts = rc('scribe', defaultOpts), ...exposers) {
|
2016-01-24 18:03:01 -06:00
|
|
|
opts = extend(true, {}, defaultOpts, opts);
|
2016-01-22 08:22:11 -06:00
|
|
|
|
|
|
|
var console = new BasicConsole(opts.name, id || opts.instanceId);
|
|
|
|
|
2016-01-24 18:03:01 -06:00
|
|
|
function appendTransforms(args) {
|
|
|
|
if (opts.mongo && opts.mongoUri && opts.socket && opts.socketPort) {
|
|
|
|
args.push(new JSON2Converter());
|
|
|
|
args.push(new MongoDB(opts.mongoUri, opts.debug));
|
|
|
|
args.push(new SocketIO(opts.socketPort, opts.debug));
|
|
|
|
} else if (opts.mongo && opts.mongoUri) {
|
|
|
|
args.push(new JSON2Converter());
|
|
|
|
args.push(new MongoDB(opts.mongoUri, opts.debug));
|
|
|
|
} else if (opts.socket && opts.socketPort) {
|
|
|
|
args.push(new JSON2Converter());
|
|
|
|
args.push(new SocketIO(opts.socketPort, opts.debug));
|
2016-01-22 08:22:11 -06:00
|
|
|
}
|
|
|
|
|
2016-01-24 18:03:01 -06:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2016-01-25 22:44:36 -06:00
|
|
|
console.exposed().concat(exposers).forEach(expose => {
|
|
|
|
console.expose(expose);
|
|
|
|
|
2016-01-24 18:03:01 -06:00
|
|
|
let args = appendTransforms([expose, 'mongo-socket', new ErrorExtractor()]);
|
|
|
|
|
|
|
|
console.pipe.apply(console, args);
|
|
|
|
|
2016-01-22 08:22:11 -06:00
|
|
|
console.pipe(expose, 'bash',
|
2016-01-29 01:31:33 -06:00
|
|
|
new Inspector(opts.inspector),
|
2016-01-22 08:22:11 -06:00
|
|
|
new DefaultConsole());
|
|
|
|
});
|
|
|
|
|
2016-01-24 18:03:01 -06:00
|
|
|
let args = appendTransforms(['express', 'mongo-socket', new ErrorExtractor(), new ExpressExtractor()]);
|
|
|
|
|
|
|
|
console.pipe.apply(console, args);
|
2016-01-22 08:22:11 -06:00
|
|
|
|
|
|
|
console.pipe('express', 'bash',
|
|
|
|
new ExpressExtractor(),
|
|
|
|
new ExpressInspector(),
|
2016-01-29 01:31:33 -06:00
|
|
|
new Inspector(opts.inspector),
|
2016-01-22 08:22:11 -06:00
|
|
|
new DefaultConsole());
|
|
|
|
|
2016-01-24 18:03:01 -06:00
|
|
|
console.viewer = create.bind(null, opts.mongo && opts.mongoUri, opts.web.router, opts.web.client, opts.debug);
|
2016-01-22 08:22:11 -06:00
|
|
|
|
|
|
|
console.build = ()=> {
|
|
|
|
|
|
|
|
// update
|
|
|
|
nativePackage.main = `${opts.publicUri}:${path.join(String(opts.web.client.port), opts.basePath)}`;
|
|
|
|
|
|
|
|
// save
|
2016-01-22 19:25:52 -06:00
|
|
|
fs.writeFileSync(`${__dirname}/../native/package.json`, JSON.stringify(nativePackage, null, 4), {encoding: 'utf8'});
|
2016-01-22 08:22:11 -06:00
|
|
|
|
2016-01-24 18:03:01 -06:00
|
|
|
const nw = new NwBuilder(extend(true, {
|
2016-01-22 08:22:11 -06:00
|
|
|
platforms: ['win', 'osx', 'linux'],
|
2016-01-22 19:25:52 -06:00
|
|
|
buildDir: `${__dirname}/../public/native`,
|
2016-01-22 08:22:11 -06:00
|
|
|
version: '0.12.3',
|
|
|
|
zip: true
|
2016-01-22 19:25:52 -06:00
|
|
|
}, opts.nwjs, {files: `${__dirname}/../native/**/**`}));
|
2016-01-22 08:22:11 -06:00
|
|
|
|
|
|
|
if (opts.debug) {
|
|
|
|
nw.on('log', d => console.log(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
return nw.build();
|
|
|
|
};
|
|
|
|
|
|
|
|
process.on('uncaughtException', e => console.error(e).then(() => process.exit(1)));
|
|
|
|
|
|
|
|
return console;
|
|
|
|
};
|