jsonHandler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. const fs = require('fs');
  17. const mkdirp = require('mkdirp');
  18. const path = require('path');
  19. const LOG_NAME = 'jsonHandler.js';
  20. /**
  21. * Saves the topics to json files in the given outputPath folder
  22. *
  23. * @param {Topic[]} topics
  24. * @param {string} outputPath
  25. * @param {string} [makoPath] - If set it will add the index and topic tree to this file
  26. * @return {Promise}
  27. */
  28. const saveTopics = (topics, outputPath, makoPath, isImpala) => {
  29. const index = {};
  30. const topLevel = [];
  31. const savePromises = [];
  32. const saveTopicsInternal = (topics, parent) => {
  33. topics.forEach(topic => {
  34. const entry = {
  35. title: topic.fragment.title
  36. .text()
  37. .replace(/[\n\r]/g, '')
  38. .trim(),
  39. ref: topic.ref,
  40. children: []
  41. };
  42. if (!parent) {
  43. topLevel.push(entry);
  44. } else {
  45. parent.children.push(entry);
  46. }
  47. let fileName = topic.ref.replace('.xml', '.json');
  48. if (fileName.indexOf('.json') === -1) {
  49. fileName += '.json';
  50. }
  51. index[topic.ref] = fileName;
  52. const filePath = outputPath + fileName;
  53. savePromises.push(
  54. new Promise((resolve, reject) => {
  55. mkdirp(path.dirname(filePath), err => {
  56. if (!err) {
  57. fs.writeFile(filePath, topic.toJson(), err => {
  58. if (err) {
  59. reject(err);
  60. } else {
  61. resolve();
  62. }
  63. });
  64. } else {
  65. reject(err);
  66. }
  67. });
  68. })
  69. );
  70. saveTopicsInternal(topic.children, entry);
  71. });
  72. };
  73. saveTopicsInternal(topics);
  74. const indexTypes = {
  75. impala: {
  76. staticPrefix: "':'${ static('desktop/docs/impala/",
  77. docIndexRegex: /window\.IMPALA_DOC_INDEX.*\n/,
  78. docIndexPrefix: 'window.IMPALA_DOC_INDEX = {',
  79. topLevelRegex: /window\.IMPALA_DOC_TOP_LEVEL.*\n/,
  80. topLevelPrefix: 'window.IMPALA_DOC_TOP_LEVEL = ['
  81. },
  82. hive: {
  83. staticPrefix: "':'${ static('desktop/docs/hive/",
  84. docIndexRegex: /window\.HIVE_DOC_INDEX.*\n/,
  85. docIndexPrefix: 'window.HIVE_DOC_INDEX = {',
  86. topLevelRegex: /window\.HIVE_DOC_TOP_LEVEL.*\n/,
  87. topLevelPrefix: 'window.HIVE_DOC_TOP_LEVEL = ['
  88. }
  89. };
  90. if (makoPath) {
  91. const indexType = isImpala ? indexTypes.impala : indexTypes.hive;
  92. savePromises.push(
  93. new Promise((resolve, reject) => {
  94. fs.readFile(makoPath, 'utf-8', (err, contents) => {
  95. if (err) {
  96. reject(err);
  97. return;
  98. }
  99. const indexStrings = [];
  100. Object.keys(index).forEach(key => {
  101. indexStrings.push("'" + key + indexType.staticPrefix + index[key] + "') }'");
  102. });
  103. contents = contents.replace(
  104. indexType.docIndexRegex,
  105. indexType.docIndexPrefix + indexStrings.join(',') + '};\n'
  106. );
  107. const createTopicJs = entry => {
  108. return (
  109. "{title:'" +
  110. entry.title +
  111. "',ref:'" +
  112. entry.ref +
  113. "',children:[" +
  114. entry.children.map(createTopicJs).join(',') +
  115. ']}'
  116. );
  117. };
  118. contents = contents.replace(
  119. indexType.topLevelRegex,
  120. indexType.topLevelPrefix + topLevel.map(createTopicJs).join(',') + '];\n'
  121. );
  122. fs.writeFile(makoPath.replace('.template', ''), contents, err => {
  123. if (err) {
  124. reject(err);
  125. return;
  126. }
  127. console.log('%s: %s written.', LOG_NAME, makoPath.replace('.template', ''));
  128. resolve();
  129. });
  130. });
  131. })
  132. );
  133. }
  134. return Promise.all(savePromises);
  135. };
  136. module.exports = {
  137. saveTopics: saveTopics
  138. };