blob: 64db936cffbe85f1b3e167339e6418009317aa39 [file] [log] [blame]
Matteo Scandolo6328d9b2015-11-05 16:02:11 +01001// This was generating the files needed from https://github.com/signalfx/swagger-angular-client
2// But this module is not parsing the format in which our Swagger is generating JSON files
3
4'use strict';
5
6var fs = require('fs');
7var CodeGen = require('swagger-js-codegen').CodeGen;
8var fetchSchema = require('fetch-swagger-schema');
9var P = require('bluebird');
10var chalk = require('chalk');
11var concat = require('concat')
12
13/////////////
14// HELPERS //
15/////////////
16
17var fetchSwagger = P.promisify(function(url, done){
18 fetchSchema(url, function(error, schema){
19 if(error) {
20 return done(error);
21 }
22 done(null, schema);
23 });
24});
25
26// Write to file promisified
27var writeToFile = P.promisify(function(file, content, done) {
28 fs.writeFile(file, content, function(err) {
29 if(err) {
30 return done(err);
31 }
32
33 done(null, file + ' has been saved');
34 });
35});
36
37var concatFiles = P.promisify(function(files, dest, done){
38 concat(files, dest, function (error) {
39 if(error){
40 return done(error);
41 }
42 done();
43 })
44});
45
46////////////////////
47// generator loop //
48////////////////////
49
50P.coroutine(function*(){
51
52 var generatedFiles = [];
53
54 console.log(chalk.green('Generating APIs '));
55
56 let mainDef = yield fetchSwagger('http://localhost:9999/docs/api-docs/');
57
58 yield writeToFile(`api/ngXosApi-runtime.js`, `window.XosApi = ${JSON.stringify(mainDef)}`)
59
60 for(let i = 0; i < mainDef.apis.length; i++){
61
62 const path = mainDef.apis[i].path.replace('/', '');
63
64 process.stdout.write(chalk.green(`Starting ${path} generation `));
65
66 let loader = setInterval(function(){
67 process.stdout.write(chalk.green('.'));
68 }, 500);
69
70
71 let def = yield fetchSwagger(`http://localhost:9999/docs/api-docs/${path}`);
72
73 yield writeToFile(`api/ng-${path}.json`, JSON.stringify(def));
74 yield writeToFile(`api/ng-${path}.js`, `window.${path}Api = ${JSON.stringify(def)}`)
75
76 generatedFiles.push(`api/ng-${path}.js`);
77
78 clearInterval(loader);
79 process.stdout.write('\n');
80 }
81
82 // TODO rewrite concat to minify API
83 // evaluate to use gulp instead to manage this
84 // at least minify
85 yield concatFiles(generatedFiles, '../static/js/xosApi.js');
86
87 console.log(chalk.green('APIs Ready!'));
88
89 process.exit();
90
91})()
92.catch(function(e){
93 process.stdout.write('\n');
94 console.error(e);
95 process.exit(e.code);
96});