-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
124 lines (108 loc) · 3.79 KB
/
generate.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
// Download the current CloudFormation schema from AWS and
// create a bunch of yasnippet snippet files that will be loaded
// when json-mode is enabled.
//
// Author: Rusty Conover (rusty@luckydinosaur.com)
// (C) Copyright 2017 Lucky Dinosaur LLC.
//
const fs = require('fs');
const _ = require('lodash');
const request = require('request');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const canon = require('canonical-json');
rimraf.sync('snippets');
mkdirp.sync('snippets/json-mode/cloudformation');
request(
{
url: 'https://dnwj8swjjbsbt.cloudfront.net/latest/CloudFormationResourceSpecification.json',
encoding: 'utf8',
gzip: true,
json: true,
},
(err, res, schema) => {
if (err) {
throw err;
}
fs.writeFileSync('snippets/json-mode/.yas-make-groups', '', 'utf8');
_.mapValues(schema.ResourceTypes, (v, key) => {
const snippetName = key
.replace(/::/g, '_')
.replace(/^AWS_/, '')
.toLowerCase();
let snippet = `# -*- mode: snippet -*-
# name: ${snippetName}
# key: ${snippetName}
# --
`;
let resolveProp;
const expressType = (parent, subtype, prop) => {
if (subtype === 'String') {
return '';
} else if (subtype === 'Boolean') {
return true;
} else if (subtype === 'Integer' || subtype === 'Long') {
return 0;
} else if (subtype === 'Timestamp') {
return '0000-00-20T00:00:00';
} else if (subtype === 'Double') {
return 0.0;
} else if (subtype === 'Configuration') {
return {};
} else if (subtype === 'Map') {
return { Key: expressType(parent, prop.PrimitiveItemType || prop.ItemType, prop) };
} else if (subtype === 'Json') {
return {};
} else if (subtype === 'SourceConfiguration') {
return '';
} else if (subtype === 'List') {
return [expressType(parent, prop.PrimitiveItemType || prop.ItemType, prop)];
}
const src = schema.PropertyTypes[`${parent}.${subtype}`] ||
schema.ResourceTypes[`${parent}.${subtype}`]
|| schema.ResourceTypes[`${subtype}`]
|| schema.PropertyTypes[`${subtype}`];
if (src == null) {
throw new Error(`Did not find2 parent=${key} sub=${subtype} ${JSON.stringify(prop, null, 2)}`);
}
return _.mapValues(src.Properties, resolveProp);
};
resolveProp = (prop) => {
if (prop.PrimitiveType) {
return expressType(key, prop.PrimitiveType, prop);
} else if (prop.Type) {
return expressType(key, prop.Type, prop);
} else if (prop.ItemType) {
return expressType(key, prop.ItemType, prop);
}
throw new Error(`Do not know how to handle prop ${JSON.stringify(prop, null, 2)}`);
};
const props = {
Type: key,
Properties: _.mapValues(v.Properties, resolveProp),
};
snippet += canon(props, null, 2);
fs.writeFileSync(`snippets/json-mode/cloudformation/${snippetName}`, snippet, 'utf8');
});
// Now that all of the types have been expressed, create some snippets for functions.
fs.writeFileSync('snippets/json-mode/cloudformation/fn_sub', `# -*- mode: snippet -*-
# name: fn_sub
# key: fn_sub
# --
{ "Fn::Sub": [$1, { "$2": $3 } ]}`);
fs.writeFileSync('snippets/json-mode/cloudformation/fn_join', `# -*- mode: snippet -*-
# name: fn_join
# key: fn_join
# --
{ "Fn::Join": [$1, [ "$2", $3 ]] }`);
fs.writeFileSync('snippets/json-mode/cloudformation/ref', `# -*- mode: snippet -*-
# name: ref
# key: ref
# --
{ "Ref": "$1" }`);
fs.writeFileSync('snippets/json-mode/cloudformation/fn_getatt', `# -*- mode: snippet -*-
# name: fn_getatt
# key: fn_getatt
# --
{ "Fn::GetAtt": ["$1", "$2"] }`);
});