-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (77 loc) · 2.34 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
*Copyright (C) 2017 Create-Net / FBK.
*All rights reserved. This program and the accompanying materials
*are made available under the terms of the Eclipse Public License 2.0
*which accompanies this distribution, and is available at
*https://www.eclipse.org/legal/epl-2.0/
*
*SPDX-License-Identifier: EPL-2.0
*
*Contributors:
* Create-Net / FBK - initial API and implementation
******************************************************************************/
var parser = require("./lib/parser");
var config = require('./config.json');
var _ = require("lodash");
var d = require("debug")("agile:gen");
module.exports.loadApi = function () {
return parser.run();
};
module.exports.export = function (format) {
module.exports.loadApi()
.then(function (definition) {
d("Loaded %s classes", _.size(definition.classes));
return definition.render(format);
})
.then(function (outputs) {
if(typeof outputs === 'string') {
outputs = [
{
filename: "/api." + format,
content: outputs
}
];
}
if(!outputs) {
throw new Error("No output available for " + format);
}
return Promise.all(outputs.map(function(file) {
try {
var fs = require('fs');
var path = require('path');
var filepath = config.outputDir +
path.sep + format +
path.sep + file.filename;
var dirname = path.dirname(filepath);
if(!fs.existsSync(dirname)) {
d("Create directory %s", dirname);
require('mkdirp').sync(dirname);
}
d("Writing %s", filepath);
require('fs').writeFileSync(
filepath,
file.content
);
return Promise.resolve();
}
catch(e) {
return Promise.reject(e);
}
}));
})
.catch(function (e) {
throw e;
});
};
module.exports.exportAll = function () {
var glob = require("glob");
var path = require("path");
glob("./lib/renderer/*.js", function (err, files) {
if(err) throw err;
files.forEach(function (file) {
var format = path.basename(file, '.js');
d("Render format %s", format);
module.exports.export(format);
});
});
};