Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const protagonist = require('protagonist'); |
| 4 | const fs = require('fs'); |
| 5 | const P = require('bluebird'); |
| 6 | const _ = require('lodash'); |
| 7 | const chalk = require('chalk'); |
| 8 | const Handlebars = require('handlebars'); |
Matteo Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 9 | const util = require('util'); |
| 10 | const path = require('path'); |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 11 | |
| 12 | P.promisifyAll(fs); |
| 13 | P.promisifyAll(protagonist); |
| 14 | |
| 15 | const angualarModuleName = 'xos.helpers' |
| 16 | |
| 17 | // format href in angular format |
| 18 | const formatHref = url => url.replace('{', ':').replace('}', ''); |
| 19 | |
| 20 | const formatTitle = title => title.split(' ').join('-'); |
| 21 | |
| 22 | const getParamName = url => url.match(/\{([^)]+)\}/) ? url.match(/\{([^)]+)\}/)[1] : ''; |
| 23 | |
| 24 | // Get Group description |
| 25 | const getGroupDescription = (group) => _.find(group, {element: 'copy'}) ? _.find(group, {element: 'copy'}).content.replace(/\n$/, '') : ''; |
| 26 | |
| 27 | // Loop APIs endpoint |
| 28 | const 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 |
| 43 | const 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 Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 54 | fs.writeFileSync(path.join(__dirname, `../xosHelpers/src/services/rest/${formatTitle(d.meta.title)}.js`), handlebarsTemplate(data)); |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 55 | }); |
| 56 | |
| 57 | console.info(chalk.green.bold(`Api Generated`)); |
| 58 | |
| 59 | }; |
| 60 | |
| 61 | // Loop the top level definitions |
| 62 | const loopApiDefinitions = (defs) => { |
Matteo Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 63 | // console.log(util.inspect(defs, false, null)); |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 64 | _.forEach(defs, d => loopApiGroups(d.content)); |
| 65 | }; |
| 66 | |
| 67 | let handlebarsTemplate; |
| 68 | |
| 69 | // read blueprint docs and parse |
Matteo Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 70 | fs.readFileAsync(path.join(__dirname, './ngResourceTemplate.handlebars'), 'utf8') |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 71 | .then((template) => { |
| 72 | handlebarsTemplate = Handlebars.compile(template); |
Matteo Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 73 | return fs.readFileAsync(path.join(__dirname, '../../../xos/tests/api/apiary.apib'), 'utf8') |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 74 | }) |
| 75 | .then(data => protagonist.parseAsync(data)) |
| 76 | .then(result => loopApiDefinitions(result.content)) |
| 77 | .catch(console.warn); |
| 78 | |