-
Notifications
You must be signed in to change notification settings - Fork 1
/
husky.js
193 lines (157 loc) · 5.26 KB
/
husky.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const { readdirSync } = require('fs')
const { join } = require('path')
const casex = require('casex')
const dayjs = require('dayjs')
const axios = require('axios')
const redis = require('async-redis')
const utils = require('./utils')
const defaultModules = require('./modules')
/** Represents a set of plugin configuration */
class Husky {
constructor() {
this.pages = new Map()
this.contentTypes = new Map()
this.requestedLists = new Set()
this.trello = axios.create({
baseURL: 'https://api.trello.com/1',
params: {
key: process.env.TRELLO_APP_KEY,
token: process.env.TRELLO_TOKEN
}
})
this.redis = redis.createClient(process.env.REDIS_URL)
this.utils = utils
this.setupJobs()
}
/** Load a Husky from a modules directory (absolute path) */
static from(path) {
let husky = new Husky()
// Add the default modules
Object.values(defaultModules).forEach(mod => mod(husky, utils))
// Loop through the module directory and look for modules
readdirSync(path).map(filename => {
if (/^.*.js$/.test(filename) === false) return
try {
// Try to require the .js file
let fn = require(join(path, filename))
// Fail if it didn't export a function
if (typeof fn !== 'function') {
throw new Error(`Bad plugin '${filename}'`)
}
// Call the function with the instance and utils
fn(husky, utils)
} catch (error) {
console.log(error)
}
})
return husky
}
/** Get all the registered templates */
get templates() {
return utils.collectArrayFromMap(this.pages, 'templates')
}
/** Get the pages which are active according to the environemt vars set */
activePages() {
let pages = new Map()
this.pages.forEach((Page, type) => {
if (Page.variables.some(name => process.env[name] === undefined)) return
pages.set(type, Page)
})
return pages
}
/** Get the site mode based on the environemt vars set */
getSitemode() {
if (process.env.PAGE_LIST) return 'multi'
// let active = this.activePages()
//
// if (active.length > 1) return 'multi'
//
// return active[0]
let found = null
this.pages.forEach((Page, type) => {
if (found || Page.variables.some(v => process.env[v] === undefined))
return
found = type
})
return found
}
/** Adds a content html onto a card using ordered content parsers */
processCard(card) {
let blobs = []
card.slug = utils.slug(card.name)
// Process each content type into a html blob
this.contentTypes.forEach((Content, type) => {
blobs.push(Object.assign({ content: Content.parser(card) }, Content))
})
// Sort the blobs using their orderingg, lowest first
blobs.sort((a, b) => a.order - b.order)
const wrap = blob =>
`<div class="content-${blob.type}">${blob.content}</div>`
// Join up the blobs, optionally wrapping in a div, and place it on the card
card.content = blobs.map(b => (b.noWrapper ? b.content : wrap(b))).join('')
// Also add the timestamp to the card
card.timestamp = dayjs(card.dateLastActivity).format('dddd D MMMM YYYY')
// Add custom fields like
if(card.customFieldItems) {
if (card.customFieldItems.length != 0){
let card_date = card.customFieldItems.find(o => o.idCustomField === process.env.TIMELINE_DATE_ID)
if (card_date) {
card.date = dayjs(card_date.value.date).format('dddd D MMMM YYYY')
}
}
}
}
/** Register a new page type */
registerPage(type, Page) {
Page.name = utils.undefOr(Page.name, casex(type, 'Ca Se'))
Page.variables = Page.variables || []
Page.templates = Page.templates || []
Page.routes = Page.routes || {}
this.pages.set(type, Page)
}
/** Register a content type, a way of processing a card into html */
registerContentType(name, Content) {
if (Content.order === undefined) Content.order = 25
if (Content.noWrapper === undefined) Content.noWrapper = false
Content.type = name
this.contentTypes.set(name, Content)
}
setupJobs() {
let interval = parseInt(process.env.POLL_INTERVAL, 10)
if (Number.isNaN(interval)) interval = 5000
setInterval(async () => {
for (let listId of this.requestedLists) {
await this.fetchAndCacheList(listId)
}
}, interval)
}
async fetchAndCacheList(listId) {
const params = {
fields:
'desc,descData,labels,name,pos,url,idAttachmentCover,dateLastActivity,',
attachments: true,
members: true,
customFieldItems: true
}
try {
const result = await this.trello.get(`/lists/${listId}/cards`, { params })
return this.redis.set(`list_${listId}`, JSON.stringify(result.data))
} catch (error) {
if (error.response) {
console.log(error.message, error.response.data)
} else {
console.log(error)
}
}
}
async fetchCards(listId) {
// If the list hasn't been requested, remember it and do an initial request
if (!this.requestedLists.has(listId)) {
this.requestedLists.add(listId)
await this.fetchAndCacheList(listId)
}
// Return the parsed list
return JSON.parse(await this.redis.get(`list_${listId}`) || '[]')
}
}
module.exports = { Husky }