jsonHandler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* eslint-disable no-restricted-syntax */
  17. const fs = require('fs');
  18. const mkdirp = require('mkdirp');
  19. const path = require('path');
  20. const LOG_NAME = 'jsonHandler.js';
  21. /**
  22. * Saves the topics to json files in the given outputPath folder
  23. *
  24. * @param {Topic[]} topics
  25. * @param {string} outputPath
  26. * @param {string} [makoPath] - If set it will add the index and topic tree to this file
  27. * @return {Promise}
  28. */
  29. const saveTopics = (topics, outputPath, makoPath, isImpala) => {
  30. const index = {};
  31. const topLevel = [];
  32. const savePromises = [];
  33. const saveTopicsInternal = (topics, parent) => {
  34. topics.forEach(topic => {
  35. const entry = {
  36. title: topic.fragment.title
  37. .text()
  38. .replace(/[\n\r]/g, '')
  39. .trim(),
  40. ref: topic.ref,
  41. children: []
  42. };
  43. if (!parent) {
  44. topLevel.push(entry);
  45. } else {
  46. parent.children.push(entry);
  47. }
  48. let fileName = topic.ref.replace('.xml', '.json');
  49. if (fileName.indexOf('.json') === -1) {
  50. fileName += '.json';
  51. }
  52. index[topic.ref] = fileName;
  53. const filePath = outputPath + fileName;
  54. savePromises.push(
  55. new Promise((resolve, reject) => {
  56. mkdirp(path.dirname(filePath), err => {
  57. if (!err) {
  58. fs.writeFile(filePath, topic.toJson(), err => {
  59. if (err) {
  60. reject(err);
  61. } else {
  62. resolve();
  63. }
  64. });
  65. } else {
  66. reject(err);
  67. }
  68. });
  69. })
  70. );
  71. saveTopicsInternal(topic.children, entry);
  72. });
  73. };
  74. saveTopicsInternal(topics);
  75. const indexTypes = {
  76. impala: {
  77. staticPrefix: "':'${ static('desktop/docs/impala/",
  78. docIndexRegex: /window\.IMPALA_DOC_INDEX.*\n/,
  79. docIndexPrefix: 'window.IMPALA_DOC_INDEX = {',
  80. topLevelRegex: /window\.IMPALA_DOC_TOP_LEVEL.*\n/,
  81. topLevelPrefix: 'window.IMPALA_DOC_TOP_LEVEL = ['
  82. },
  83. hive: {
  84. staticPrefix: "':'${ static('desktop/docs/hive/",
  85. docIndexRegex: /window\.HIVE_DOC_INDEX.*\n/,
  86. docIndexPrefix: 'window.HIVE_DOC_INDEX = {',
  87. topLevelRegex: /window\.HIVE_DOC_TOP_LEVEL.*\n/,
  88. topLevelPrefix: 'window.HIVE_DOC_TOP_LEVEL = ['
  89. }
  90. };
  91. if (makoPath) {
  92. const indexType = isImpala ? indexTypes.impala : indexTypes.hive;
  93. savePromises.push(
  94. new Promise((resolve, reject) => {
  95. fs.readFile(makoPath, 'utf-8', (err, contents) => {
  96. if (err) {
  97. reject(err);
  98. return;
  99. }
  100. const indexStrings = [];
  101. Object.keys(index).forEach(key => {
  102. indexStrings.push("'" + key + indexType.staticPrefix + index[key] + "') }'");
  103. });
  104. contents = contents.replace(
  105. indexType.docIndexRegex,
  106. indexType.docIndexPrefix + indexStrings.join(',') + '};\n'
  107. );
  108. const createTopicJs = entry => {
  109. return (
  110. "{title:'" +
  111. entry.title +
  112. "',ref:'" +
  113. entry.ref +
  114. "',children:[" +
  115. entry.children.map(createTopicJs).join(',') +
  116. ']}'
  117. );
  118. };
  119. contents = contents.replace(
  120. indexType.topLevelRegex,
  121. indexType.topLevelPrefix + topLevel.map(createTopicJs).join(',') + '];\n'
  122. );
  123. fs.writeFile(makoPath.replace('.template', ''), contents, err => {
  124. if (err) {
  125. reject(err);
  126. return;
  127. }
  128. console.log('%s: %s written.', LOG_NAME, makoPath.replace('.template', ''));
  129. resolve();
  130. });
  131. });
  132. })
  133. );
  134. }
  135. return Promise.all(savePromises);
  136. };
  137. module.exports = {
  138. saveTopics: saveTopics
  139. };
  140. /* eslint-enable no-restricted-syntax */