jsonHandler.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. let index = {};
  30. let topLevel = [];
  31. let savePromises = [];
  32. let saveTopicsInternal = (topics, parent) => {
  33. topics.forEach(topic => {
  34. let entry = {
  35. title: topic.fragment.title.text().replace(/[\n\r]/g, '').trim(),
  36. ref: topic.ref,
  37. children: []
  38. };
  39. if (!parent) {
  40. topLevel.push(entry)
  41. } else {
  42. parent.children.push(entry);
  43. }
  44. let fileName = topic.ref.replace('.xml', '.json');
  45. if (fileName.indexOf('.json') === -1) {
  46. fileName += '.json';
  47. }
  48. index[topic.ref] = fileName;
  49. let filePath = outputPath + fileName;
  50. savePromises.push(new Promise((resolve, reject) => {
  51. mkdirp(path.dirname(filePath), err => {
  52. if (!err) {
  53. fs.writeFile(filePath, topic.toJson(), err => {
  54. if (err) {
  55. reject(err);
  56. } else {
  57. resolve();
  58. }
  59. });
  60. } else {
  61. reject(err);
  62. }
  63. });
  64. }));
  65. saveTopicsInternal(topic.children, entry);
  66. });
  67. };
  68. saveTopicsInternal(topics);
  69. const indexTypes = {
  70. impala: {
  71. staticPrefix: '\':\'${ static(\'desktop/docs/impala/',
  72. docIndexRegex: /window\.IMPALA_DOC_INDEX.*\n/,
  73. docIndexPrefix: 'window.IMPALA_DOC_INDEX = {',
  74. topLevelRegex: /window\.IMPALA_DOC_TOP_LEVEL.*\n/,
  75. topLevelPrefix: 'window.IMPALA_DOC_TOP_LEVEL = ['
  76. },
  77. hive: {
  78. staticPrefix: '\':\'${ static(\'desktop/docs/hive/',
  79. docIndexRegex: /window\.HIVE_DOC_INDEX.*\n/,
  80. docIndexPrefix: 'window.HIVE_DOC_INDEX = {',
  81. topLevelRegex: /window\.HIVE_DOC_TOP_LEVEL.*\n/,
  82. topLevelPrefix: 'window.HIVE_DOC_TOP_LEVEL = ['
  83. }
  84. };
  85. if (makoPath) {
  86. let indexType = isImpala ? indexTypes.impala : indexTypes.hive;
  87. savePromises.push(new Promise((resolve, reject) => {
  88. fs.readFile(makoPath, 'utf-8', (err, contents) => {
  89. if (err) {
  90. reject(err);
  91. return;
  92. }
  93. let indexStrings = [];
  94. Object.keys(index).forEach(key => {
  95. indexStrings.push('\'' + key + indexType.staticPrefix + index[key] + '\') }\'')
  96. });
  97. contents = contents.replace(indexType.docIndexRegex, indexType.docIndexPrefix + indexStrings.join(',') + '};\n');
  98. let createTopicJs = (entry) => {
  99. return '{title:\'' + entry.title +'\',ref:\'' + entry.ref + '\',children:[' + entry.children.map(createTopicJs).join(',') + ']}';
  100. };
  101. contents = contents.replace(indexType.topLevelRegex, indexType.topLevelPrefix + topLevel.map(createTopicJs).join(',') + '];\n');
  102. fs.writeFile(makoPath.replace('.template', ''), contents, (err) => {
  103. if (err) {
  104. reject(err);
  105. return;
  106. }
  107. console.log('%s: %s written.', LOG_NAME, makoPath.replace('.template', ''));
  108. resolve();
  109. })
  110. });
  111. }));
  112. }
  113. return Promise.all(savePromises);
  114. };
  115. module.exports = {
  116. saveTopics: saveTopics
  117. };