1 |
|
2 | (function() {
|
3 | var cson, extend, fs, init, load, loadSrc, marked, path, readdir, setNestedPropertyValue, skeemas, userHome, xml, yaml;
|
4 |
|
5 | cson = require('season');
|
6 |
|
7 | extend = require('extend-combine');
|
8 |
|
9 | fs = require('fs');
|
10 |
|
11 | marked = require('marked');
|
12 |
|
13 | path = require('path');
|
14 |
|
15 | readdir = require('fs-readdir-recursive');
|
16 |
|
17 | skeemas = require('skeemas');
|
18 |
|
19 | userHome = require('user-home');
|
20 |
|
21 | xml = require('xml-to-jsobj');
|
22 |
|
23 | yaml = require('js-yaml');
|
24 |
|
25 | setNestedPropertyValue = function(obj, fields, val) {
|
26 | var cur, last;
|
27 | fields = fields.split('.');
|
28 | cur = obj;
|
29 | last = fields.pop();
|
30 | fields.forEach(function(field) {
|
31 | cur[field] = {};
|
32 | return cur = cur[field];
|
33 | });
|
34 | cur[last] = val;
|
35 | return obj;
|
36 | };
|
37 |
|
38 | init = function(project_path, validate) {
|
39 | var autocode_config, autocode_path, config, config_schema;
|
40 | if (validate == null) {
|
41 | validate = true;
|
42 | }
|
43 | project_path = project_path || this.path;
|
44 | if (!project_path) {
|
45 | throw new Error('project_path for Autocode config is required');
|
46 | }
|
47 | config = load(project_path);
|
48 | if (!config) {
|
49 | return false;
|
50 | }
|
51 | autocode_path = path.resolve(__dirname + "/../..");
|
52 | if (validate === false) {
|
53 | autocode_config = yaml.safeLoad(fs.readFileSync(autocode_path + "/.autocode/config.yml"));
|
54 | return autocode_config.exports.ConfigSchema.schema;
|
55 | } else {
|
56 | config_schema = this.load(autocode_path, false);
|
57 | }
|
58 | validate = skeemas.validate(config, config_schema);
|
59 | if (!validate.valid) {
|
60 | console.log("Configuration failed validation:");
|
61 | console.log(validate.errors);
|
62 | throw new Error("Invalid Configuration for path: " + project_path);
|
63 | }
|
64 | if (!config.host) {
|
65 | config.host = 'github.com';
|
66 | }
|
67 | this.config = config;
|
68 | this.path = project_path;
|
69 | return config;
|
70 | };
|
71 |
|
72 | load = function(project_path) {
|
73 | var config, ext, file, i, len, ref;
|
74 | ref = ['yml', 'yaml', 'cson', 'json', 'xml'];
|
75 | for (i = 0, len = ref.length; i < len; i++) {
|
76 | ext = ref[i];
|
77 | file = project_path + "/.autocode/config." + ext;
|
78 | if (fs.existsSync(file)) {
|
79 | config = fs.readFileSync(file);
|
80 | config = (function() {
|
81 | switch (ext) {
|
82 | case 'yml':
|
83 | case 'yaml':
|
84 | return yaml.safeLoad(config);
|
85 | case 'cson':
|
86 | return cson.readFileSync(file);
|
87 | case 'json':
|
88 | return JSON.parse(config);
|
89 | case 'xml':
|
90 | return xml.parseFromString(config);
|
91 | }
|
92 | })();
|
93 | break;
|
94 | }
|
95 | }
|
96 | return config;
|
97 | };
|
98 |
|
99 | loadSrc = function(file) {
|
100 | var ext, src;
|
101 | if (fs.existsSync(file)) {
|
102 | src = fs.readFileSync(file);
|
103 | ext = file.split('.');
|
104 | ext = ext[ext.length - 1];
|
105 | src = (function() {
|
106 | switch (ext) {
|
107 | case 'yml':
|
108 | case 'yaml':
|
109 | return yaml.safeLoad(fs.readFileSync(file, 'utf8'));
|
110 | case 'cson':
|
111 | return cson.readFileSync(file);
|
112 | case 'json':
|
113 | return JSON.parse(fs.readFileSync(file, 'utf8'));
|
114 | case 'xml':
|
115 | return xml.parseFromString(fs.readFileSync(file, 'utf8'));
|
116 | }
|
117 | })();
|
118 | }
|
119 | return src;
|
120 | };
|
121 |
|
122 | module.exports = init;
|
123 |
|
124 | }).call(this);
|