blob: 21d65e722e56144ee7ec54d2799da69b8fb5b2d8 [file] [log] [blame]
Matteo Scandoloc7987cd2016-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 Scandolo1dc8f3f2016-04-13 10:19:55 -07009const util = require('util');
10const path = require('path');
Matteo Scandoloc7987cd2016-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) => {
29 // {name: 'ResourceName', attributes: {href: '/ahhsiiis'}}
30 _.remove(group, {element: 'copy'})
31 // console.log(group);
32 // _.forEach(group, d => console.log(d));
33
34 return _.map(group, g => {
35 return {
36 name: formatTitle(g.meta.title),
37 param: {href: formatHref(g.attributes.href), name: getParamName(g.attributes.href)},
38 }
39 })
40};
41
42// Loop APIs groups
43const loopApiGroups = (defs) => {
44 if (!Array.isArray(defs)) {
45 return;
46 }
47 _.forEach(defs, d => {
48 console.info(chalk.blue.bold(`Parsing Group: ${d.meta.title}`));
49 var data = {
50 description: getGroupDescription(d.content),
51 ngModule: angualarModuleName,
52 resources: loopApiEndpoint(d.content)
53 };
Matteo Scandolo1dc8f3f2016-04-13 10:19:55 -070054 fs.writeFileSync(path.join(__dirname, `../xosHelpers/src/services/rest/${formatTitle(d.meta.title)}.js`), handlebarsTemplate(data));
Matteo Scandoloc7987cd2016-04-12 11:38:40 -070055 });
56
57 console.info(chalk.green.bold(`Api Generated`));
58
59};
60
61// Loop the top level definitions
62const loopApiDefinitions = (defs) => {
Matteo Scandolo1dc8f3f2016-04-13 10:19:55 -070063 // console.log(util.inspect(defs, false, null));
Matteo Scandoloc7987cd2016-04-12 11:38:40 -070064 _.forEach(defs, d => loopApiGroups(d.content));
65};
66
67let handlebarsTemplate;
68
69// read blueprint docs and parse
Matteo Scandolo1dc8f3f2016-04-13 10:19:55 -070070fs.readFileAsync(path.join(__dirname, './ngResourceTemplate.handlebars'), 'utf8')
Matteo Scandoloc7987cd2016-04-12 11:38:40 -070071.then((template) => {
72 handlebarsTemplate = Handlebars.compile(template);
Matteo Scandolo67e79cb2016-04-29 09:14:58 -070073 return fs.readFileAsync(path.join(__dirname, '../../../apiary.apib'), 'utf8')
Matteo Scandoloc7987cd2016-04-12 11:38:40 -070074})
75.then(data => protagonist.parseAsync(data))
76.then(result => loopApiDefinitions(result.content))
77.catch(console.warn);
78