Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | var fs = require('fs'); |
| 4 | var CodeGen = require('swagger-js-codegen').CodeGen; |
| 5 | var fetchSchema = require('fetch-swagger-schema'); |
| 6 | var P = require('bluebird'); |
| 7 | var chalk = require('chalk'); |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 8 | var concat = require('concat') |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 9 | |
| 10 | ///////////// |
| 11 | // HELPERS // |
| 12 | ///////////// |
| 13 | |
| 14 | var fetchSwagger = P.promisify(function(url, done){ |
| 15 | fetchSchema(url, function(error, schema){ |
| 16 | if(error) { |
| 17 | return done(error); |
| 18 | } |
| 19 | done(null, schema); |
| 20 | }); |
| 21 | }); |
| 22 | |
| 23 | // Write to file promisified |
| 24 | var writeToFile = P.promisify(function(file, content, done) { |
| 25 | fs.writeFile(file, content, function(err) { |
| 26 | if(err) { |
| 27 | return done(err); |
| 28 | } |
| 29 | |
| 30 | done(null, file + ' has been saved'); |
| 31 | }); |
| 32 | }); |
| 33 | |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 34 | var concatFiles = P.promisify(function(files, dest, done){ |
| 35 | concat(files, dest, function (error) { |
| 36 | if(error){ |
| 37 | return done(error); |
| 38 | } |
| 39 | done(); |
| 40 | }) |
| 41 | }); |
| 42 | |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 43 | //////////////////// |
| 44 | // generator loop // |
| 45 | //////////////////// |
| 46 | |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 47 | P.coroutine(function*(){ |
| 48 | |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 49 | var generatedFiles = []; |
| 50 | |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 51 | console.log(chalk.green('Generating APIs ')); |
| 52 | |
Matteo Scandolo | f083089 | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 53 | let mainDef = yield fetchSwagger('http://localhost:9999/docs/api-docs/'); |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 54 | |
Matteo Scandolo | f083089 | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 55 | for(let i = 0; i < mainDef.apis.length; i++){ |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 56 | |
Matteo Scandolo | f083089 | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 57 | const path = mainDef.apis[i].path.replace('/', ''); |
| 58 | |
| 59 | process.stdout.write(chalk.green(`Starting ${path} generation `)); |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 60 | |
| 61 | let loader = setInterval(function(){ |
| 62 | process.stdout.write(chalk.green('.')); |
| 63 | }, 500); |
| 64 | |
Matteo Scandolo | f083089 | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 65 | |
| 66 | let def = yield fetchSwagger(`http://localhost:9999/docs/api-docs/${path}`); |
| 67 | yield writeToFile(`api/ng-${path}.js`, CodeGen.getAngularCode({ |
| 68 | moduleName: `xos.${path}`, |
| 69 | className: `${path}`, |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 70 | swagger: def, |
| 71 | lint: false, |
| 72 | template: { |
| 73 | class: fs.readFileSync('apiTemplates/custom-angular-class.mustache', 'utf-8'), |
| 74 | method: fs.readFileSync('node_modules/swagger-js-codegen/templates/method.mustache', 'utf-8'), |
| 75 | request: fs.readFileSync('node_modules/swagger-js-codegen/templates/angular-request.mustache', 'utf-8') |
| 76 | } |
| 77 | })); |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 78 | |
Matteo Scandolo | f083089 | 2015-11-05 11:58:53 +0100 | [diff] [blame] | 79 | generatedFiles.push(`api/ng-${path}.js`); |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 80 | |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 81 | clearInterval(loader); |
| 82 | process.stdout.write('\n'); |
| 83 | } |
| 84 | |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 85 | // TODO rewrite concat to minify API |
| 86 | // evaluate to use gulp instead to manage this |
| 87 | // at least minify |
| 88 | yield concatFiles(generatedFiles, '../static/js/xosApi.js'); |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 89 | |
| 90 | console.log(chalk.green('APIs Ready!')); |
| 91 | |
| 92 | process.exit(); |
| 93 | |
| 94 | })() |
| 95 | .catch(function(e){ |
Matteo Scandolo | 46485ac | 2015-11-03 10:48:01 +0100 | [diff] [blame] | 96 | process.stdout.write('\n'); |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 97 | console.error(e); |
Matteo Scandolo | 46485ac | 2015-11-03 10:48:01 +0100 | [diff] [blame] | 98 | process.exit(e.code); |
Matteo Scandolo | 1279fc3 | 2015-11-02 17:35:40 +0100 | [diff] [blame] | 99 | }); |