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) => { |
Matteo Scandolo | d5f3d05 | 2016-05-02 15:29:37 -0700 | [diff] [blame] | 29 | // removing description |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 30 | _.remove(group, {element: 'copy'}) |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 31 | |
| 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 |
| 41 | const 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 Scandolo | d5f3d05 | 2016-05-02 15:29:37 -0700 | [diff] [blame] | 47 | |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 48 | var data = { |
| 49 | description: getGroupDescription(d.content), |
| 50 | ngModule: angualarModuleName, |
| 51 | resources: loopApiEndpoint(d.content) |
| 52 | }; |
Matteo Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 53 | 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] | 54 | }); |
| 55 | |
| 56 | console.info(chalk.green.bold(`Api Generated`)); |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | // Loop the top level definitions |
| 61 | const loopApiDefinitions = (defs) => { |
Matteo Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 62 | // console.log(util.inspect(defs, false, null)); |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 63 | _.forEach(defs, d => loopApiGroups(d.content)); |
| 64 | }; |
| 65 | |
| 66 | let handlebarsTemplate; |
| 67 | |
| 68 | // read blueprint docs and parse |
Matteo Scandolo | 1dc8f3f | 2016-04-13 10:19:55 -0700 | [diff] [blame] | 69 | fs.readFileAsync(path.join(__dirname, './ngResourceTemplate.handlebars'), 'utf8') |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 70 | .then((template) => { |
| 71 | handlebarsTemplate = Handlebars.compile(template); |
Matteo Scandolo | 67e79cb | 2016-04-29 09:14:58 -0700 | [diff] [blame] | 72 | return fs.readFileAsync(path.join(__dirname, '../../../apiary.apib'), 'utf8') |
Matteo Scandolo | c7987cd | 2016-04-12 11:38:40 -0700 | [diff] [blame] | 73 | }) |
| 74 | .then(data => protagonist.parseAsync(data)) |
| 75 | .then(result => loopApiDefinitions(result.content)) |
| 76 | .catch(console.warn); |
| 77 | |