-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (113 loc) · 4.73 KB
/
index.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
/*
Google Cloud - Terraform Provisioner plugin
*/
const _ = require('lodash');
const fs = require('fs-extra');
const { resolve } = require('path');
const { basename } = require('path');
async function getFiles(dir) {
const subdirs = await fs.readdir(dir);
const files = await Promise.all(subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
return (await fs.stat(res)).isDirectory() ? getFiles(res) : res;
}));
return files.reduce((a, f) => a.concat(f), []);
}
var walkSync = function(dir, filelist) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
const resolved = resolve(dir, file);
if (fs.statSync(resolved).isDirectory()) {
filelist = walkSync(resolved , filelist);
} else {
filelist.push(resolved);
}
});
return filelist;
};
const hook_log_header = "Terraform Provisioner GCP";
const hook_root_dir = "node_modules/@researchdatabox/redbox-hook-terraform-gcp"
module.exports = function (sails) {
return {
initialize: function (cb) {
// Do Some initialisation tasks
// This can be for example: copy files or images to the redbox-portal front end
// Wait for the main redbox-hook-terraform plugin to load...
sails.log.info(hook_log_header+ "::Waiting on dependent on hook to init...");
sails.after(["hook:redbox-hook-terraform:loaded"], function() {
sails.log.info(hook_log_header+ "::Dependent hook loaded, initing...");
// read custom configuration and merge with sails.config
const config_dirs = [hook_root_dir + "/form-config", hook_root_dir + "/config"];
_.each(config_dirs, (config_dir) => {
if (fs.pathExistsSync(config_dir)) {
const files = walkSync(config_dir, []);
sails.log.info(hook_log_header + "::Processing:");
sails.log.info(files);
const concatArrsFn = function (objValue, srcValue, key) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
_.each(files, (file_path) => {
const config_file = require(file_path);
_.mergeWith(sails.config, config_file, concatArrsFn);
});
} else {
sails.log.info(hook_log_header + "::Skipping, directory not found:" + config_dir);
}
});
// language file updates ... only English for now
// locales directory moved out of assets directory so we can safely merge
const language_file_path = resolve("assets/locales/en/translation.json");
const hook_language_file_path = resolve(hook_root_dir, "locales/en/translation.json");
if (fs.pathExistsSync(language_file_path) && fs.pathExistsSync(hook_language_file_path)) {
sails.log.info(hook_log_header + ":: Merging English translation file...");
const mainTranslation = require(language_file_path);
const hookTranslation = require(hook_language_file_path);
_.merge(mainTranslation, hookTranslation);
fs.writeFileSync(language_file_path, JSON.stringify(mainTranslation, null, 2));
}
//If assets directory exists, there must be some assets to copy over
if(fs.pathExistsSync(hook_root_dir + "/assets/")) {
sails.log.info(hook_log_header + ":: Copying branding...");
fs.copySync(hook_root_dir + "/assets/","assets/");
fs.copySync(hook_root_dir + "/assets/",".tmp/public/");
}
//If views directory exists, there must be some views to copy over
if(fs.pathExistsSync(hook_root_dir + "/views/")) {
fs.copySync(hook_root_dir + "/views/","views/");
}
// Load up all the services ...
const servicesDir = resolve(hook_root_dir, "api/services");
if (fs.pathExistsSync(servicesDir)) {
const files = walkSync(servicesDir, []);
_.each(files, (file_path) => {
const service = require(file_path);
const serviceName = basename(file_path, '.js')
sails.services[serviceName] = service;
});
}
// Load up all controllers ...
const controllersDir = resolve(hook_root_dir, "api/controllers");
if (fs.pathExistsSync(controllersDir)) {
const files = walkSync(controllersDir, []);
_.each(files, (file_path) => {
const controller = require(file_path);
sails.controllers[basename(file_path, '.js')] = controller;
});
}
sails.log.info(hook_log_header+ "::Init ok.");
return cb();
});
},
//If each route middleware do not exist sails.lift will fail during hook.load()
routes: {
before: {},
after: {
}
},
configure: function () {
}
}
};