-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·145 lines (110 loc) · 4.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
'use strict';
const fs = require('fs-extra');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const process = require('process');
const parentPath = process.argv.slice(2)[0];
const intermediateRecompile = process.argv.slice(2)[1];
const recompileGradleValue = (typeof intermediateRecompile === 'string')
? intermediateRecompile.toLowerCase() === 'yes' || intermediateRecompile.toLowerCase() === 'y'
: false;
const yamlPath = `${parentPath}/main/resources/rpc-docs`
runProcess(recompileGradleValue);
async function runProcess(recompileGradle) {
try {
const currentDirectory = process.cwd();
const currentBuildDirectory = `${currentDirectory}/build`;
const currentBuildDirectoryDist = `${currentBuildDirectory}/dist`;
const htmlGeneratorPath = `${currentDirectory}/rskj-docs-html-generator/rskj-templating-runner`;
const htmlProjectPath = `${currentDirectory}/rskj-docs-html-generator/rskj-docs`
fs.emptyDirSync(currentBuildDirectory);
const extractedJson = `${currentBuildDirectory}/input.json`;
await buildGradle(recompileGradle);
const jsonResult = await extractJson();
fs.writeFileSync(extractedJson, jsonResult);
process.chdir(htmlGeneratorPath);
const result = await generateHtml(extractedJson);
if(!result.includes('HTML-SUCCESS')) {
console.log(`Unexpected result ${result}`);
throw 'We got an unexpected result, was expecting \'HTML-SUCCESS\'';
}
process.chdir(htmlProjectPath);
await compileHtml();
const distLocation = `${htmlProjectPath}/dist`;
fs.existsSync(currentBuildDirectoryDist);
fs.copySync(distLocation, currentBuildDirectoryDist);
console.log('Build success');
}
catch(e) {
throw e;
}
}
async function compileHtml() {
try {
console.log('Starting html compiling process');
const command = `npm run build`;
const { stdout, stderr } = await exec(command);
if(!stdout.includes('Build complete.')) {
throw stderr;
}
console.log('Completed html compiling process');
}
catch(e) {
console.error(e)
throw e;
}
}
async function generateHtml(inputJson) {
try {
console.log('Starting html generating process');
const command = `npm run compile --inputJson=${inputJson}`;
const { stdout, stderr } = await exec(command);
if(!stdout.includes("HTML-SUCCESS") && stderr) {
throw stderr;
}
console.log('Completed html generating process');
return stdout;
}
catch(e) {
throw e;
}
}
async function extractJson() {
if (fs.existsSync('./rskj-docs-json-extractor/build/libs/javaparser-all-1.0-SNAPSHOT.jar')) {
try {
console.log('Starting json extraction process');
const command = `java -cp ./rskj-docs-json-extractor/build/libs/javaparser-all-1.0-SNAPSHOT.jar org.rsk.doc.extractor.StartExtractor ${parentPath} ${yamlPath}`;
const { stdout, stderr } = await exec(command);
if(stderr) {
throw stderr;
}
const processedJson = JSON.parse(stdout);
const prettifiedJson = JSON.stringify(processedJson, null, 4);
console.log('Completed json extraction process');
return prettifiedJson;
}
catch(e) {
throw e;
}
}
}
async function buildGradle(recompileGradle) {
const jarPath = './rskj-docs-json-extractor/build/libs/javaparser-all-1.0-SNAPSHOT.jar';
const jarCompiled = fs.existsSync(jarPath);
if (!jarCompiled || recompileGradle) {
try {
console.log('Starting gradle build process');
const buildCommand = ` ./rskj-docs-json-extractor/gradlew -p ./rskj-docs-json-extractor/ clean build fatJar`;
const { stdout, stderr } = await exec(buildCommand);
if(stderr) {
console.log('There were errors in stderr by exec, these are possibly warnings. Continuing build (Check if an unexpected error occurs later).');
}
console.log('Completed gradle build process');
return stderr;
}
catch(e) {
throw e;
}
}
}