-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
124 lines (108 loc) · 3.19 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const EventEmitter = require('events').EventEmitter
const defaultsDeep = require('lodash.defaultsdeep')
/**
* @class Trailpack
* @see {@link https://trailsjs.io/doc/en/ref/trailpack}
*/
module.exports = class Trailpack {
/**
* The Trailpack lifecycle. At each stage (configure, initialize) define
* preconditions ("listen") and postconditions ("emit") of the trailpack.
*/
get lifecycle () {
return {
configure: {
listen: [ ],
emit: [ ]
},
initialize: {
listen: [ ],
emit: [ ]
}
}
}
/**
* @constructor
* @param app TrailsApp instance
* @param pack.api The api entities defined in this trailpack (api/ folder)
* @param pack.config The trailpack configuration (config/ folder)
* @param pack.pkg The trailpack package.json
*
* Instantiate the Trailpack and set some initial properties. All Trailpacks
* should implement their own constructors, and call super(app, pack) with
* their own pack definitions. Implementing application logic in the trailpack
* constructor is not recommended.
*/
constructor (app, { pkg, config = { }, api = { } }) {
if (!(app instanceof EventEmitter)) {
throw new Error('The "app" argument must be of type EventEmitter')
}
if (!pkg) {
throw new Error('Trailpack is missing package definition ("pack.pkg")')
}
Object.defineProperties(this, {
app: {
enumberable: false,
writable: false,
value: app
},
pkg: {
value: Object.freeze(pkg),
writable: false,
enumerable: false
},
api: {
value: api,
writable: true
},
config: {
value: config,
enumerable: false
}
})
defaultsDeep(this.config.lifecycle, this.lifecycle)
this.app.emit(`trailpack:${this.name}:constructed`, this)
}
/**
* Validate any necessary preconditions for this trailpack. We strongly
* recommend that all Trailpacks override this method and use it to check
* preconditions.
*/
validate () {
}
/**
* Set any configuration required before the trailpacks are initialized.
* Trailpacks that require configuration, or need to alter/extend the app's
* configuration, should override this method.
*/
configure () {
}
/**
* Start any services or listeners necessary for this pack. Trailpacks that
* run daemon-like services should override this method.
*/
async initialize () {
}
/**
* Unload this Trailpack. This method will instruct the trailpack to perform
* any necessary cleanup with the expectation that the app will stop or reload
* soon thereafter. If your trailpack runs a daemon or any other thing that may
* occupy the event loop, implementing this method is important for Trails to
* exit correctly.
*/
async unload () {
}
/**
* Return the name of this Trailpack. By default, this is the name of the
* npm module (in package.json). This method can be overridden for trailpacks
* which do not follow the "trailpack-" prefix naming convention.
*
* @return String
*/
get name () {
return this.pkg.name.replace(/trailpack\-/, '')
}
get type () {
return 'misc'
}
}