forked from screwdriver-cd/config-parser
/
index.js
55 lines (51 loc) · 1.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
'use strict';
const Yaml = require('js-yaml');
const Hoek = require('hoek');
const phaseValidateStructure = require('./lib/phase/structural');
const phaseFlatten = require('./lib/phase/flatten');
const phaseValidateFunctionality = require('./lib/phase/functional');
const phaseGeneratePermutations = require('./lib/phase/permutation');
/**
* Parses a yaml file
* @method parseYaml
* @param {String} yaml Raw yaml
* @return {Promise} resoves POJO containing yaml data
*/
const parseYaml = yaml => (new Promise(resolve => resolve(Yaml.safeLoad(yaml))));
/**
* Parse the configuration from a screwdriver.yaml
* @method configParser
* @param {String} yaml Contents of screwdriver.yaml
* @param {Function} callback Function to call when done (error, { workflow, jobs })
*/
module.exports = function configParser(yaml) {
// Convert from YAML to JSON
return parseYaml(yaml)
// Basic validation
.then(phaseValidateStructure)
// Flatten structures
.then(phaseFlatten)
// Functionality validation
.then(phaseValidateFunctionality)
// Generate Permutations
.then(phaseGeneratePermutations)
// Output in the right format
.then(doc => ({
jobs: Hoek.reach(doc, 'jobs'),
workflow: Hoek.reach(doc, 'workflow')
}))
.catch(err => ({
jobs: {
main: [{
image: 'node:6',
commands: [{
name: 'config-parse-error',
command: `echo "${err}"; exit 1`
}],
secrets: [],
environment: {}
}]
},
workflow: ['main']
}));
};