-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.ts
164 lines (139 loc) · 3.63 KB
/
plugin.ts
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
import { PluginContext } from '@rcv-prod-toolkit/types'
import { join } from 'path'
import { writeFile, opendir, readFile, stat } from 'fs/promises'
import { emptyDir, copy } from 'fs-extra'
import { compileAsync } from 'sass'
interface ThemeConfig {
name: string
author: string
version: string
}
interface Theme {
config: ThemeConfig
folder: string
scss: string
id: string
}
let themes: Theme[]
const getThemes = async (ctx: PluginContext): Promise<Theme[]> => {
const themesPath = join(__dirname, '../themes')
const themes: Theme[] = []
const dir = await opendir(themesPath)
for await (const folder of dir) {
if (!folder.isDirectory()) continue
const themePath = join(themesPath, folder.name)
let themeConfig: ThemeConfig
let scss: string
try {
themeConfig = require(join(themePath, 'theme.json'))
scss = await readFile(join(themePath, 'index.scss'), 'utf-8')
} catch (e) {
ctx.log.warn(`Failed to load theme in ${themePath}`, e)
continue
}
const theme: Theme = {
config: themeConfig,
folder: themePath,
scss,
id: folder.name
}
themes.push(theme)
}
return themes
}
/**
* Returns the currently active theme, by reading the 'id' file on the file system,
* or null if there currently is no theme active
*/
const getActiveTheme = async (): Promise<string | null> => {
const idFilePath = join(__dirname, '../frontend/active/id')
try {
await stat(idFilePath)
} catch (e) {
return null
}
const activeTheme = (await readFile(idFilePath, 'utf-8')).trim()
return activeTheme
}
module.exports = async (ctx: PluginContext) => {
const namespace = ctx.plugin.module.getName()
// Register new UI page
ctx.LPTE.emit({
meta: {
type: 'add-pages',
namespace: 'ui',
version: 1
},
pages: [
{
name: 'Theming',
frontend: 'frontend',
id: `op-${namespace}`
}
]
})
let activeTheme = await getActiveTheme()
// Emit event that we're ready to operate
ctx.LPTE.emit({
meta: {
type: 'plugin-status-change',
namespace: 'lpt',
version: 1
},
status: 'RUNNING'
})
themes = await getThemes(ctx)
ctx.LPTE.on(namespace, 'get-themes', (event) => {
ctx.LPTE.emit({
meta: {
type: event.meta.reply as string,
namespace: 'reply',
version: 1
},
themes,
activeTheme
})
})
ctx.LPTE.on(namespace, 'reload-themes', async (event) => {
themes = await getThemes(ctx)
ctx.LPTE.emit({
meta: {
type: event.meta.reply as string,
namespace: 'reply',
version: 1
},
themes,
activeTheme
})
})
ctx.LPTE.on(namespace, 'activate-theme', async (event) => {
activeTheme = event.theme as string
const themePath = join(__dirname, '../themes/', activeTheme)
const activePath = join(__dirname, '../frontend/active')
const idFilePath = join(activePath, 'id')
const gitKeepFilePath = join(activePath, '.gitkeep')
try {
await emptyDir(activePath)
await copy(themePath, activePath)
await writeFile(idFilePath, activeTheme)
await writeFile(gitKeepFilePath, '')
} catch (e) {
ctx.log.error('Applying theme failed', e)
}
try {
const result = await compileAsync(join(activePath, 'index.scss'))
await writeFile(join(activePath, 'index.css'), result.css)
} catch (error) {
ctx.log.error('Failed to compile scss', error)
}
ctx.LPTE.emit({
meta: {
type: event.meta.reply as string,
namespace: 'reply',
version: 1
},
themes,
activeTheme
})
})
}