-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.js
More file actions
executable file
·88 lines (71 loc) · 2.51 KB
/
compiler.js
File metadata and controls
executable file
·88 lines (71 loc) · 2.51 KB
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
#!/usr/bin/env node
var jp = require('jsonpath')
, jsonfile = require('jsonfile')
, fs = require('fs')
, winston = require('winston')
, options = require('yargs')
.usage('\nCompiles componentized CloudFormation templates into a stand-alone file\n\nUsage: $0 [options] TEMPLATE')
.help()
.demand(1)
.alias('o', 'out')
.default('out', 'out.json')
.describe('out', 'Name for compiled template')
.alias('l', 'log-level')
.default('log-level', 'info')
.describe('log-level', 'error, warn, info, verbose, debug, silly')
.alias('c', 'component-set')
.describe('component-set', 'selects a set of component overrides')
.alias('h', 'help')
.argv;
var log = new winston.Logger({
transports: [
new (winston.transports.Console)({
level: options.logLevel,
colorize: true,
prettyPrint: true,
})
]});
log.debug('Options', JSON.stringify(options));
var inputFile = options._[0];
var outputFile = options.out;
var inputObject = jsonfile.readFileSync(inputFile);
var cset = options.componentSet;
var componentSets = ['default'];
if(options.componentSet) {
log.verbose('Adding component set %s', cset)
componentSets.push(cset);
componentSets.reverse();
}
log.info('Component sets: %s', componentSets);
log.info('Processing template: %s', inputFile);
jp.nodes(inputObject, '$..Ref.URI').forEach(resolveRef, inputObject);
log.debug('Tada', inputObject);
jsonfile.writeFileSync(options.out, inputObject, {spaces: 4});
function resolveRef(r) {
log.info('Loading Component: %s', r.value);
var component = jsonfile.readFileSync(findComponentPath(r.value));
log.debug(component);
jp.nodes(component, '$..Ref.URI').forEach(resolveRef, component);
log.verbose('Build ref path');
var path = jp.paths(this, '$..Ref.URI')[0];
path.splice(-2, 2);
path = jp.stringify(path);
log.debug(path);
log.verbose('Inserting component');
var y =jp.value(this, path, component);
//console.log(y);
log.debug(this);
return this;
}
function findComponentPath(ref) {
log.verbose('Searching for component %s', ref);
var cs = componentSets.find(function(s) {
var path = ['components', s, this + '.json'].join('/');
log.verbose('Checking path %s', path);
var found = fs.existsSync(path);
log.verbose(found?'Found it!':'Nope');
return found;
}, ref);
log.verbose('Found component in set %s', cs);
return ['components', cs, ref + '.json'].join('/');
}