blob: b24bf9b6627f9ccf915e5fca1e9465a765b5c548 [file] [log] [blame]
Matteo Scandolo1279fc32015-11-02 17:35:40 +01001'use strict';
2
3var fs = require('fs');
4var CodeGen = require('swagger-js-codegen').CodeGen;
5var fetchSchema = require('fetch-swagger-schema');
6var P = require('bluebird');
7var chalk = require('chalk');
Matteo Scandolodc88bf12015-11-03 16:27:07 +01008var concat = require('concat')
Matteo Scandolo1279fc32015-11-02 17:35:40 +01009
10/////////////
11// HELPERS //
12/////////////
13
14var 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
24var 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 Scandolodc88bf12015-11-03 16:27:07 +010034var 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 Scandolo1279fc32015-11-02 17:35:40 +010043////////////////////
44// generator loop //
45////////////////////
46
Matteo Scandolo1279fc32015-11-02 17:35:40 +010047P.coroutine(function*(){
48
Matteo Scandolodc88bf12015-11-03 16:27:07 +010049 var generatedFiles = [];
50
Matteo Scandolo1279fc32015-11-02 17:35:40 +010051 console.log(chalk.green('Generating APIs '));
52
Matteo Scandolof0830892015-11-05 11:58:53 +010053 let mainDef = yield fetchSwagger('http://localhost:9999/docs/api-docs/');
Matteo Scandolo1279fc32015-11-02 17:35:40 +010054
Matteo Scandolof0830892015-11-05 11:58:53 +010055 for(let i = 0; i < mainDef.apis.length; i++){
Matteo Scandolo1279fc32015-11-02 17:35:40 +010056
Matteo Scandolof0830892015-11-05 11:58:53 +010057 const path = mainDef.apis[i].path.replace('/', '');
58
59 process.stdout.write(chalk.green(`Starting ${path} generation `));
Matteo Scandolo1279fc32015-11-02 17:35:40 +010060
61 let loader = setInterval(function(){
62 process.stdout.write(chalk.green('.'));
63 }, 500);
64
Matteo Scandolof0830892015-11-05 11:58:53 +010065
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 Scandolodc88bf12015-11-03 16:27:07 +010070 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 Scandolo1279fc32015-11-02 17:35:40 +010078
Matteo Scandolof0830892015-11-05 11:58:53 +010079 generatedFiles.push(`api/ng-${path}.js`);
Matteo Scandolodc88bf12015-11-03 16:27:07 +010080
Matteo Scandolo1279fc32015-11-02 17:35:40 +010081 clearInterval(loader);
82 process.stdout.write('\n');
83 }
84
Matteo Scandolodc88bf12015-11-03 16:27:07 +010085 // 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 Scandolo1279fc32015-11-02 17:35:40 +010089
90 console.log(chalk.green('APIs Ready!'));
91
92 process.exit();
93
94})()
95.catch(function(e){
Matteo Scandolo46485ac2015-11-03 10:48:01 +010096 process.stdout.write('\n');
Matteo Scandolo1279fc32015-11-02 17:35:40 +010097 console.error(e);
Matteo Scandolo46485ac2015-11-03 10:48:01 +010098 process.exit(e.code);
Matteo Scandolo1279fc32015-11-02 17:35:40 +010099});