-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (52 loc) · 1.84 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
56
57
58
59
'use strict';
const SCHEMA_CONFIG = require('screwdriver-data-schema').config.command.schemaCommand;
const Yaml = require('js-yaml');
/**
* Loads the configuration from a stringified sd-command.yaml
* @method loadCommandSpecYaml
* @param {String} yamlString Contents of sd-command.yaml
* @return {Promise} Promise that resolves to the command as a config object
*/
function loadCommandSpecYaml(yamlString) {
return new Promise(resolve => {
resolve(Yaml.load(yamlString));
});
}
/**
* Validate the command configuration
* @method validateCommand
* @param {Object} commandObj Configuration object that represents the command
* @return {Promise} Promise that resolves to the passed-in config object
*/
async function validateCommand(commandObj) {
const validCommand = await SCHEMA_CONFIG.validateAsync(commandObj, {
abortEarly: false
});
return validCommand;
}
/**
* Parses the configuration from a screwdriver-command-spec.yaml
* @async parseCommand
* @param {String} yamlString Contents of screwdriver command yaml
* @return {Promise} Promise that rejects if the configuration cannot be parsed
* The promise will eventually resolve into:
* {Object} config
* {Object} config.command The parsed command that was validated
* {Object[]} config.errors An array of objects related to validating
* the given command
*/
async function parseCommand(yamlString) {
const command = await loadCommandSpecYaml(yamlString);
try {
return {
errors: [],
command: await validateCommand(command)
};
} catch (err) {
return {
errors: err.details,
command
};
}
}
module.exports = parseCommand;