|
1
|
|
|
var raml2html = require('raml2html'); |
|
2
|
|
|
var chalk = require('chalk'); |
|
3
|
|
|
var emoji = require('node-emoji'); |
|
4
|
|
|
var fs = require('fs'); |
|
5
|
|
|
var path = require('path'); |
|
6
|
|
|
var http = require('http'); |
|
7
|
|
|
var url = require('url'); |
|
8
|
|
|
var childProcess = require('child_process'); |
|
9
|
|
|
|
|
10
|
|
|
scriptPath = 'scripts/js/api.server.js'; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
var options = { |
|
13
|
|
|
env: process.env |
|
14
|
|
|
}; |
|
15
|
|
|
|
|
16
|
|
|
var childProcessInstance = childProcess.fork(scriptPath, options); |
|
17
|
|
|
|
|
18
|
|
|
// listen for errors as they may prevent the exit event from firing |
|
19
|
|
|
childProcessInstance.on('error', function (err) { |
|
20
|
|
|
if ( err ){ |
|
21
|
|
|
process.stderr.write(chalk.bgRed.white(emoji.emojify("[:heavy_multiplication_x: ] Errore server documentazione API (raml)\n"))); |
|
22
|
|
|
process.stderr.write(chalk.red(err+"\n")); |
|
23
|
|
|
process.exit(1); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
}); |
|
26
|
|
|
|
|
27
|
|
|
var enviroment = process.env.npm_package_config_enviroment; |
|
28
|
|
|
|
|
29
|
|
|
var apiDir = path.join(__dirname, '../../api/'); |
|
30
|
|
|
|
|
31
|
|
|
process.stdout.write(chalk.gray(emoji.emojify('[ ] Generazione Documentazione API (' + enviroment + ") from "+ ramlFile +"\n"))); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
var ramlFile = path.join(__dirname, '../../api/api.raml'); |
|
34
|
|
|
|
|
35
|
|
|
var ramlHtml = path.join(__dirname, '../../api/api-generated.html'); |
|
36
|
|
|
|
|
37
|
|
|
var configWithCustomTemplates = raml2html.getDefaultConfig('template.nunjucks', path.normalize(apiDir + '/template') ); |
|
38
|
|
|
|
|
39
|
|
|
// source can either be a filename, url, file contents (string) or parsed RAML object |
|
40
|
|
|
raml2html.render(ramlFile, configWithCustomTemplates).then(function(result) { |
|
41
|
|
|
// Save the result to a file or do something else with the result |
|
42
|
|
|
// api-generated.html |
|
43
|
|
|
// |
|
44
|
|
|
// console.log(result); |
|
45
|
|
|
|
|
46
|
|
|
fs.writeFile(ramlHtml, result, function(err) { |
|
47
|
|
|
if(err) { |
|
48
|
|
|
process.stderr.write(chalk.bgRed.white(emoji.emojify("[:heavy_multiplication_x: ] Errore generazione documentazione API (raml)\n"))); |
|
49
|
|
|
process.stderr.write(chalk.red(err+"\n")); |
|
50
|
|
|
process.exit(1); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
process.stdout.write(chalk.gray(emoji.emojify('[ ] Server must stop '+"\n"))); |
|
54
|
|
|
|
|
55
|
|
|
var options = { |
|
56
|
|
|
host: 'localhost', |
|
57
|
|
|
port: 9999, |
|
58
|
|
|
path: '/end', |
|
59
|
|
|
method: 'GET' |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
|
|
var reqCloseServer = http.request(options, function(res) { |
|
63
|
|
|
statusCode = `${res.statusCode}`; |
|
|
|
|
|
|
64
|
|
|
process.stdout.write(chalk.gray(emoji.emojify('[ ] Response '+ statusCode +"\n"))); |
|
65
|
|
|
|
|
66
|
|
|
process.stdout.write(chalk.bgGreen.black(emoji.emojify('[:heavy_check_mark: ] API Documentation created!' + "\n"))); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
reqCloseServer.on('error', (e) => { |
|
70
|
|
|
error = `${e.message}`; |
|
|
|
|
|
|
71
|
|
|
process.stderr.write(chalk.bgRed.white(emoji.emojify("[:heavy_multiplication_x: ] Errore generazione documentazione API (raml)\n"))); |
|
72
|
|
|
process.stderr.write(chalk.red(error+"\n")); |
|
73
|
|
|
process.exit(1); |
|
|
|
|
|
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
reqCloseServer.end(); |
|
77
|
|
|
|
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
}, function(error) { |
|
81
|
|
|
console.log(error); |
|
|
|
|
|
|
82
|
|
|
process.stderr.write(chalk.bgRed.white(emoji.emojify("[:heavy_multiplication_x: ] Errore generazione documentazione API (raml)\n"))); |
|
83
|
|
|
process.stderr.write(chalk.red(error+"\n")); |
|
84
|
|
|
process.exit(1); |
|
|
|
|
|
|
85
|
|
|
}); |
|
86
|
|
|
|