-
Notifications
You must be signed in to change notification settings - Fork 2
/
compile.js
133 lines (104 loc) · 4.46 KB
/
compile.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
const _ = require('lodash');
const async = require('async');
const join = require('path').join;
const debug = require('debug')('push2cloud-compiler:compile');
const mapAppDefaults = require('./lib/mapAppDefaults');
const mapEnvVars = require('./lib/mapEnvVars');
const mapRoutes = require('./lib/mapRoutes');
const mapBindings = require('./lib/mapBindings');
const mapScripts = require('./lib/mapScripts');
const mapAppScripts = require('./lib/mapAppScripts');
const mapAppSource = require('./lib/mapAppSource');
const mapServices = require('./lib/mapServices');
const omitApps = require('./lib/omitApps');
const requireJSONIn = require('./lib/requireJSON');
const convertSize = require('./lib/convertSize');
const repoHash = require('./lib/repoHash');
const initialState =
{ version: null
, target: null
, services: []
, apps: []
, utilityApps: []
, envVars: []
, serviceBindings: []
, routes: []
, scripts: {}
};
const compile = (
plugins
, manifestDir
, deploymentManifestPath
, done
) => {
manifestDir = manifestDir || join(process.cwd(), '__manifests');
deploymentManifestPath = deploymentManifestPath || 'deploymentManifest.json';
const requireJSON = requireJSONIn(manifestDir);
// require all the manifests
const deploymentManifest = requireJSON(deploymentManifestPath);
const releaseManifest = requireJSON('releaseManifest.json');
const requireAppManifests = (apps) => _.map(apps, (app, appName) => requireJSON(join(repoHash(app.source || releaseManifest.source), app.path || '', app.manifest || 'package.json')));
const config = _.assign({}, initialState,
{ version: releaseManifest.version
, target: deploymentManifest.target
});
// APPS
const apps = omitApps(deploymentManifest.excludeApps, releaseManifest.apps);
const utilityApps = omitApps(deploymentManifest.excludeApps, releaseManifest.utilityApps);
const appManifests = requireAppManifests(apps);
const utilityAppManifests = requireAppManifests(utilityApps);
config.apps = mapAppDefaults(appManifests, deploymentManifest, releaseManifest);
config.utilityApps = mapAppDefaults(utilityApps, deploymentManifest, releaseManifest);
config.apps = _.map(config.apps, (app) => {
const appManifest = _.find(appManifests, (tf) => tf.name === app.name);
if (!appManifest.deployment) {
debug(`App ${appManifest.name} has no deployment settings!`);
appManifest.deployment = {};
}
var a = _.assign({},
app,
{ scripts: mapAppScripts(deploymentManifest, appManifest)
, source: mapAppSource(releaseManifest, app.name)
}
);
a.memory = convertSize(a.memory);
a.disk = convertSize(a.disk);
a.instances = convertSize(a.instances);
if (appManifest.deployment.path || appManifest.deployment.appBits) {
a.appBits = appManifest.deployment.path || appManifest.deployment.appBits;
}
return a;
});
config.utilityApps = _.map(config.utilityApps, (utilityApp) => {
const utilityAppManifest = _.find(utilityAppManifests, (tf) => tf.name === utilityApp.name);
return _.assign({},
utilityApp,
{ scripts: mapAppScripts(deploymentManifest, utilityAppManifest)
, source: mapAppSource(releaseManifest, utilityApp.name)
}
);
});
// ROUTES
config.routes = mapRoutes(deploymentManifest, appManifests);
// SERVICE BINDINGS
config.serviceBindings = mapBindings(deploymentManifest, releaseManifest, appManifests);
// ENVIRONEMT VARIABLES
config.envVars = mapEnvVars(deploymentManifest, releaseManifest, appManifests);
// SCRIPTS
config.scripts = mapScripts(releaseManifest);
// SERVICES
config.services = mapServices(deploymentManifest, releaseManifest, appManifests);
// PLUGIN TOOLS
const tools = {};
tools.findApp = (a) => _.find(appManifests, (tf) => tf.name === a);
tools.findServiceBinding = (s) => _.find(config.serviceBindings, (tf) => tf.name === s);
tools.findEnvVars = (a) => _.find(config.envVars, (tf) => tf.name === a);
tools.isExcluded = (a) => !!_.includes(deploymentManifest.excludeApps, a);
// PLUGINS
//return _.reduce(plugins, ( result, plugin ) => plugin(result, { deployment: deploymentManifest, release: releaseManifest, apps: appManifests } , tools), config);
const manis = { deployment: deploymentManifest, release: releaseManifest, apps: appManifests };
const cleanPlugins = _.compact(plugins);
_.isEmpty(cleanPlugins) ? done(null, config) :
async.waterfall([(cb) => cb(null, config, manis, tools)].concat(plugins), done);
};
module.exports = compile;