blob: fdb105096c5852296272c8042cc0ab87e5a1149e [file] [log] [blame]
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -07001'use strict';
2
3const protagonist = require('protagonist');
4const fs = require('fs');
5const P = require('bluebird');
6const _ = require('lodash');
7const chalk = require('chalk');
8const Handlebars = require('handlebars');
Matteo Scandolofbd486c2016-04-13 10:19:55 -07009const util = require('util');
10const path = require('path');
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070011
12P.promisifyAll(fs);
13P.promisifyAll(protagonist);
14
15const angualarModuleName = 'xos.helpers'
16
17// format href in angular format
18const formatHref = url => url.replace('{', ':').replace('}', '');
19
20const formatTitle = title => title.split(' ').join('-');
21
22const getParamName = url => url.match(/\{([^)]+)\}/) ? url.match(/\{([^)]+)\}/)[1] : '';
23
24// Get Group description
25const getGroupDescription = (group) => _.find(group, {element: 'copy'}) ? _.find(group, {element: 'copy'}).content.replace(/\n$/, '') : '';
26
27// Loop APIs endpoint
28const loopApiEndpoint = (group) => {
Matteo Scandolob82a85d2016-05-02 15:29:37 -070029 // removing description
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070030 _.remove(group, {element: 'copy'})
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070031
32 return _.map(group, g => {
33 return {
34 name: formatTitle(g.meta.title),
35 param: {href: formatHref(g.attributes.href), name: getParamName(g.attributes.href)},
36 }
37 })
38};
39
40// Loop APIs groups
41const loopApiGroups = (defs) => {
42 if (!Array.isArray(defs)) {
43 return;
44 }
45 _.forEach(defs, d => {
46 console.info(chalk.blue.bold(`Parsing Group: ${d.meta.title}`));
Matteo Scandolob82a85d2016-05-02 15:29:37 -070047
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070048 var data = {
49 description: getGroupDescription(d.content),
50 ngModule: angualarModuleName,
51 resources: loopApiEndpoint(d.content)
52 };
Matteo Scandolofbd486c2016-04-13 10:19:55 -070053 fs.writeFileSync(path.join(__dirname, `../xosHelpers/src/services/rest/${formatTitle(d.meta.title)}.js`), handlebarsTemplate(data));
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070054 });
55
56 console.info(chalk.green.bold(`Api Generated`));
57
58};
59
60// Loop the top level definitions
61const loopApiDefinitions = (defs) => {
Matteo Scandolofbd486c2016-04-13 10:19:55 -070062 // console.log(util.inspect(defs, false, null));
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070063 _.forEach(defs, d => loopApiGroups(d.content));
64};
65
66let handlebarsTemplate;
67
68// read blueprint docs and parse
Matteo Scandolofbd486c2016-04-13 10:19:55 -070069fs.readFileAsync(path.join(__dirname, './ngResourceTemplate.handlebars'), 'utf8')
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070070.then((template) => {
71 handlebarsTemplate = Handlebars.compile(template);
Matteo Scandolo4fc06db2016-04-29 09:14:58 -070072 return fs.readFileAsync(path.join(__dirname, '../../../apiary.apib'), 'utf8')
Matteo Scandolo4b3bf7f2016-04-12 11:38:40 -070073})
74.then(data => protagonist.parseAsync(data))
75.then(result => loopApiDefinitions(result.content))
76.catch(console.warn);
77