jsonHandler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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) => {
  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. index[topic.ref] = fileName;
  46. let filePath = outputPath + fileName;
  47. savePromises.push(new Promise((resolve, reject) => {
  48. mkdirp(path.dirname(filePath), err => {
  49. if (!err) {
  50. fs.writeFile(filePath, topic.toJson(), err => {
  51. if (err) {
  52. reject(err);
  53. } else {
  54. resolve();
  55. }
  56. });
  57. } else {
  58. reject(err);
  59. }
  60. });
  61. }));
  62. saveTopicsInternal(topic.children, entry);
  63. });
  64. };
  65. saveTopicsInternal(topics);
  66. if (makoPath) {
  67. savePromises.push(new Promise((resolve, reject) => {
  68. fs.readFile(makoPath, 'utf-8', (err, contents) => {
  69. if (err) {
  70. reject(err);
  71. return;
  72. }
  73. let indexStrings = [];
  74. Object.keys(index).forEach(key => {
  75. indexStrings.push('\'' + key + '\':\'${ static(\'desktop/docs/impala/' + index[key] + '\') }\'')
  76. });
  77. contents = contents.replace(/var IMPALA_DOC_INDEX.*\n/, 'var IMPALA_DOC_INDEX = {' + indexStrings.join(',') + '};\n');
  78. let createTopicJs = (entry) => {
  79. return '{title:\'' + entry.title +'\',ref:\'' + entry.ref + '\',children:[' + entry.children.map(createTopicJs).join(',') + ']}';
  80. };
  81. contents = contents.replace(/var IMPALA_DOC_TOP_LEVEL.*\n/, 'var IMPALA_DOC_TOP_LEVEL = [' + topLevel.map(createTopicJs).join(',') + '];\n');
  82. fs.writeFile(makoPath.replace('.template', ''), contents, (err) => {
  83. if (err) {
  84. reject(err);
  85. return;
  86. }
  87. console.log('%s: %s written.', LOG_NAME, makoPath.replace('.template', ''));
  88. resolve();
  89. })
  90. });
  91. }));
  92. }
  93. return Promise.all(savePromises);
  94. };
  95. module.exports = {
  96. saveTopics: saveTopics
  97. };