This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugins.js
78 lines (69 loc) · 2.01 KB
/
plugins.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
const fs = require('fs');
const path = require('path');
const styles = require('ansi-styles');
const exec = require('@actions/exec');
/***********************************************
* Plug-ins!!
*
* The 2fa.config.json format will be...
*
* [
* {
* "name": "@expo-apple-2fa/google-cloud",
* "async": false,
* "config": {}
* }
* ]
*
* Plug-ins must be a standard npm module, with
* a default export, that accept the url as an
* argument, and return a Promise.
*
*/
async function installPlugins(pluginNames) {
const results = await exec.exec('npm', ['install', ...pluginNames], {
cwd: path.join(__dirname, '..'),
});
if (results == 0) {
return;
}
else {
throw Error(`npm exited with code ${results}`);
}
}
async function handlePlugins(url) {
// First, load the plugins, if any
const configPath = path.join(process.cwd(), '2fa.config.json');
const exists = fs.existsSync(configPath);
if (!exists) {
console.log(`${styles.greenBright.open}===> Did not find any plug-ins to run`);
return;
}
// Load our configuration
const pluginConfig = require(configPath);
// First, install our plug-ins...
console.log(`${styles.greenBright.open}===> Installing plug-ins...`);
const pluginNames = pluginConfig.map(pn => pn.name);
await installPlugins(pluginNames);
// Now, load/run our plug-ins!
console.log(`${styles.greenBright.open}===> Running plug-ins...`);
let syncPlugins = [];
pluginConfig.forEach(pn => {
const plugin = require(pn.name);
if ('async' in pn) {
if (pn.async) {
plugin(url);
}
else {
syncPlugins.push(plugin(url));
}
}
else {
// If no async defined, assume it's synchronous
syncPlugins.push(plugin(url));
}
});
// Trigger our async plugins, and wait for our sync plugins
await Promise.all(syncPlugins);
}
module.exports = handlePlugins;