-
Notifications
You must be signed in to change notification settings - Fork 639
/
ungit-plugin.js
118 lines (113 loc) · 3.55 KB
/
ungit-plugin.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
const fsSync = require('fs');
const fs = fsSync.promises;
const path = require('path');
const express = require('express');
const logger = require('./utils/logger');
const config = require('./config');
const assureArray = (obj) => {
return Array.isArray(obj) ? obj : [obj];
};
class UngitPlugin {
constructor(args) {
this.dir = args.dir;
this.path = args.path;
this.httpBasePath = args.httpBasePath;
this.manifest = JSON.parse(
fsSync.readFileSync(path.join(this.path, 'ungit-plugin.json'), { encoding: 'utf8' })
);
this.name = this.manifest.name || this.dir;
this.config = config.pluginConfigs[this.name] || {};
}
init(env) {
if (this.manifest.server) {
const serverScript = require(path.join(this.path, this.manifest.server));
serverScript.install({
app: env.app,
httpServer: env.httpServer,
ensureAuthenticated: env.ensureAuthenticated,
ensurePathExists: env.ensurePathExists,
git: require('./git-promise'),
config: env.config,
socketIO: env.socketIO,
socketsById: env.socketsById,
pluginConfig: this.config,
httpPath: `${env.pathPrefix}/plugins/${this.name}`,
pluginApiVersion: require('../package.json').ungitPluginApiVersion,
});
}
env.app.use(`/plugins/${this.name}`, express.static(this.path));
}
compile() {
logger.info(`Compiling plugin ${this.path}`);
const exports = this.manifest.exports || {};
return Promise.resolve()
.then(() => {
if (exports.raw) {
return Promise.all(
assureArray(exports.raw).map((rawSource) => {
return fs
.readFile(path.join(this.path, rawSource), { encoding: 'utf8' })
.then((text) => {
return text + '\n';
});
})
).then((result) => {
return result.join('\n');
});
} else {
return '';
}
})
.then((result) => {
if (exports.javascript) {
return (
result +
assureArray(exports.javascript)
.map((filename) => {
return `<script type="text/javascript" src="${config.rootPath}/plugins/${this.name}/${filename}"></script>`;
})
.join('\n')
);
} else {
return result;
}
})
.then((result) => {
if (exports.knockoutTemplates) {
return Promise.all(
Object.keys(exports.knockoutTemplates).map((templateName) => {
return fs
.readFile(path.join(this.path, exports.knockoutTemplates[templateName]), {
encoding: 'utf8',
})
.then((text) => {
return `<script type="text/html" id="${templateName}">\n${text}\n</script>`;
});
})
).then((templates) => {
return result + templates.join('\n');
});
} else {
return result;
}
})
.then((result) => {
if (exports.css) {
return (
result +
assureArray(exports.css)
.map((cssSource) => {
return `<link rel="stylesheet" type="text/css" href="${config.rootPath}/plugins/${this.name}/${cssSource}" />`;
})
.join('\n')
);
} else {
return result;
}
})
.then((result) => {
return `<!-- Component: ${this.name} -->\n${result}`;
});
}
}
module.exports = UngitPlugin;