forked from adaptlearning/adapt_authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installPlugins.js
145 lines (124 loc) · 3.99 KB
/
installPlugins.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
const chalk = require('chalk');
const fs = require('fs-extra');
const path = require('path');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
let frontendPlugins = [];
let backendPlugins = [];
let config;
async function setupPlugins() {
await checkConfig();
console.log(chalk.bgCyan('\n🔌 (Step 1/6) Getting plugin configs'));
await getPluginConfigs();
console.log(chalk.bgCyan('\n🔌 (Step 2/6) Cleanup frontend'));
await cleanUpFrontend();
console.log(chalk.bgCyan('\n🔌 (Step 3/6) Cleanup backend'));
await cleanUpBackend();
console.log(chalk.bgCyan('\n🔌 (Step 4/6) Copy frontend plugins'));
await copyFrontendPlugins();
console.log(chalk.bgCyan('\n🔌 (Step 5/6) Copy backend plugins'));
await copyBackendPlugins();
console.log(
chalk.bgCyan('\n🔌 (Step 6/6) Installing backend plugin dependencies')
);
await installBackendPlugins();
console.log(chalk.bgCyan('\n🔌 Done '));
}
async function checkConfig() {
try {
config = await fs.readJson(path.join('conf', 'config.json'));
} catch (error) {
// no config.json found, could be a fresh install
process.exit(1);
}
}
async function getPluginConfigs() {
const pluginsConfig = config?.plugins;
if (!pluginsConfig) {
console.log(chalk.yellow('No plugins config found in config.json'));
process.exit(1);
}
for (const [pluginName, configValues] of Object.entries(pluginsConfig)) {
if (!configValues?.isEnabled) {
console.log(
`Plugin ${chalk.yellow(pluginName)} is not enabled. Skipping...`
);
continue;
}
console.log(`👀 Checking config for plugin: ${chalk.blue(pluginName)}`);
// Add frontend plugins
if (configValues.frontend) {
frontendPlugins.push(configValues.frontend);
}
// Add backend plugins
if (configValues.backend) {
backendPlugins.push({ ...configValues.backend, name: pluginName });
}
}
}
async function cleanUpFrontend() {
// remove all plugins from frontend/src/plugins
const pluginsDir = path.join(__dirname, 'frontend', 'src', 'plugins');
await fs.remove(pluginsDir);
console.log(`🗑 Removed all frontend plugins from ${chalk.blue(pluginsDir)}`);
}
async function cleanUpBackend() {
// restore backend plugins folder
await fs.remove('plugins');
const checkout = await exec(`git checkout ./plugins`);
console.log(`♻ Checkout ./plugins from git`);
console.log(checkout.stdout);
}
async function copyFrontendPlugins() {
return Promise.all(
frontendPlugins.map(async (config) => {
const pluginDir = config.path;
const pluginName = path.basename(pluginDir);
const dest = path.join(
__dirname,
'frontend',
'src',
'plugins',
pluginName
);
await fs.copy(pluginDir, dest);
console.log(
`Copied frontend ${chalk.blue(pluginName)} to ${chalk.blue(dest)}`
);
})
);
}
async function copyBackendPlugins() {
return Promise.all(
backendPlugins.map(async (config) => {
if (!config.path) {
console.log(`💢 No path found for plugin ${chalk.red(config.name)}`);
return;
}
if (!config.dest) {
console.log(`💢 No dest found for plugin ${chalk.red(config.name)}`);
return;
}
const pluginDir = config.path;
const pluginName = path.basename(pluginDir);
const dest = path.join(__dirname, 'plugins', config.dest, pluginName);
await fs.ensureDir(dest);
await fs.copy(pluginDir, dest);
console.log(
`Copied backend plugin ${chalk.blue(pluginName)} to ${chalk.blue(dest)}`
);
})
);
}
async function installBackendPlugins() {
return Promise.all(
backendPlugins.map(async (config) => {
const pluginDir = config.path;
const pluginName = path.basename(pluginDir);
const dest = path.join(__dirname, 'plugins', config.dest, pluginName);
const install = await exec(`npm install`, { cwd: dest });
console.log(install.stdout);
})
);
}
setupPlugins();