-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanda.js
107 lines (92 loc) · 2.6 KB
/
panda.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
((a, b) => { process.env.PANDA_PATHS = (a || '').concat((a || '').split(';').find(e => e.startsWith(`${b}=`)) ? '' : `${b}=${require('path').dirname(__filename)};`) })(process.env.PANDA_PATHS, 'panda')
const EventEmitter = require('events')
const Context = require('./src/context')
const Utility = require('./src/utility')
/**
* Panda
*/
class Panda extends EventEmitter {
/**
* Panda constructor
*/
constructor () {
if (Panda._instance) return Panda._instance
super()
Panda._instance = this
this.Panda = this
// emit init
this.emit('init', this)
// return a Proxy so we can fetch dynamic entities
return new Proxy(this, {
get(target, key, receiver) {
if (Reflect.has(target, key, receiver))
return Reflect.get(target, key)
const entity = target.entity(key)
if (entity) return entity
return undefined
}
})
}
class = {
Singleton: require('./src/class/singleton'),
Entity: require('./src/class/entity')
}
get Context () { return Context }
get Factory () { return require('./src/factory') }
get Hub () { return require('./src/hub') }
get Logger () { return require('./src/logger') }
get Terminal () { return require('./src/terminal') }
get Utility () { return Utility }
get ctx () { return Context.ctx }
get Router () { return require('@koa/router') }
/**
* Fetch an Entity by type
*
* @param {String} entity the name of the entity type
* @returns entity class
*/
entity (entity) {
const name = Utility.pascalify(entity)
const slug = Utility.slugify(entity)
if (!name in this._entities) return undefined
if (this._entities[name] !== null) return this._entities[name]
const ref = this._entities[name] = require(`./src/entity/${slug}`)
return ref
}
_entities = {
App: null,
Command: null,
Component: null,
Package: null,
Project: null,
Route: null,
Scaffold: null,
Service: null,
Static: null,
View: null
}
/**
* Cheap way to access dependencies
*
* @returns {Object} list of modules
*/
modules () {
return {
Koa: require('koa'),
bodyParser: require('koa-bodyparser'),
cors: require('@koa/cors'),
serve: require('koa-static'),
session: require('koa-session'),
mount: require('koa-mount'),
path: require('path'),
render: require('./base/lib/koa-render'),
chalk: require('chalk'),
dotenv: require('dotenv'),
ejs: require('ejs'),
glob: require('glob'),
inquirer: require('inquirer')
}
}
}
module.exports = new Panda()