1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const fs = require('fs');
|
8 | const yaml = require('js-yaml');
|
9 |
|
10 |
|
11 | const refYamlType = new yaml.Type('!Ref', {
|
12 | kind: 'scalar',
|
13 | resolve: data => !!data && typeof data === 'string' && data.trim(),
|
14 | construct: data => `!Ref ${data}`
|
15 | });
|
16 |
|
17 |
|
18 | const getattYamlType = new yaml.Type('!GetAtt', {
|
19 | kind: 'scalar',
|
20 | resolve: data => !!data && typeof data === 'string' && data.trim(),
|
21 | construct: data => `!GetAtt ${data}`
|
22 | });
|
23 |
|
24 |
|
25 | const awsSchema = yaml.Schema.create(yaml.JSON_SCHEMA, [refYamlType, getattYamlType]);
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function load(filePath) {
|
32 | const fileData = fs.readFileSync(filePath, 'utf-8');
|
33 | return yaml.safeLoad(fileData, { schema: awsSchema });
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function dump(filePath, content) {
|
42 | fs.writeFileSync(filePath, yaml.dump(content).replace(/: *'(.+)'/g, ': $1'));
|
43 | }
|
44 |
|
45 | module.exports = {
|
46 | load,
|
47 | dump
|
48 | };
|