1 | "use strict";
|
2 |
|
3 | var yaml = require("js-yaml"),
|
4 | glob = require("glob"),
|
5 | path = require("path"),
|
6 | fs = require("fs"),
|
7 | async = require("async");
|
8 |
|
9 | function exists(path, cb) {
|
10 | fs.exists(path, function (exists) {
|
11 | cb(null, exists);
|
12 | });
|
13 | }
|
14 |
|
15 | function collectYaml(folder, cb) {
|
16 | var options = path.resolve(folder, "options.yml"),
|
17 | typeFile = path.resolve(folder, "type.js");
|
18 |
|
19 | async.parallel({
|
20 | hasOptions: exists.bind(fs, options),
|
21 | hasTypes: exists.bind(fs, typeFile),
|
22 | testFiles: glob.bind(null, path.resolve(folder, "tests/*.yml"))
|
23 | }, function (err, data) {
|
24 | if (err) {
|
25 | return cb(err);
|
26 | }
|
27 | var schema,
|
28 | all = {
|
29 | tests: async.map.bind(async, data.testFiles, loadYml)
|
30 | },
|
31 | type = {};
|
32 | if (data.hasOptions) {
|
33 | all.options = loadYml.bind(null, options);
|
34 | }
|
35 | if (data.hasTypes) {
|
36 | try {
|
37 | global.joi = require("joi");
|
38 | type = require(typeFile);
|
39 | } catch(e) {
|
40 | return cb(e);
|
41 | }
|
42 | }
|
43 | schema = require("./customYamlSchema")(type);
|
44 | function loadYml(file, cb) {
|
45 | fs.readFile(file, "utf8", function (err, data) {
|
46 | if (err) {
|
47 | err.message = "Error while loading '" + file + "': " + err.message;
|
48 | return cb(err);
|
49 | }
|
50 | data = data.toString();
|
51 | try {
|
52 | var doc = yaml.safeLoad(data, {
|
53 | schema: schema
|
54 | });
|
55 | } catch(e) {
|
56 | e.message = "Error while parsing '" + file + "': " + e.message;
|
57 | e.stack = e.message;
|
58 | return cb(e);
|
59 | }
|
60 | cb(null, doc);
|
61 | });
|
62 | }
|
63 | async.parallel(all, function(err, config) {
|
64 | if (err) {
|
65 | return cb(err);
|
66 | }
|
67 | var combined,
|
68 | options = data.hasOptions ? config.options : {};
|
69 | config.tests.forEach(function (testList) {
|
70 | if (!combined) {
|
71 | combined = [];
|
72 | }
|
73 | combined = combined.concat(testList);
|
74 | });
|
75 | if (combined) {
|
76 | options.tests = (options.tests || []).concat(combined);
|
77 | }
|
78 | cb(null, options);
|
79 | });
|
80 | });
|
81 | }
|
82 |
|
83 | module.exports = collectYaml; |
\ | No newline at end of file |