generateParserModuleImports.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-console */
  17. const fs = require('fs');
  18. const fsExtra = require('fs-extra');
  19. const pathModule = require('path');
  20. const { PARSER_FOLDER, LICENSE } = require('./generateParsers');
  21. const fsPromises = fs.promises;
  22. const SQL_PARSER_MODULES_PATH = `${PARSER_FOLDER}parserModules.ts`;
  23. const SYNTAX_IMPORT_TEMPLATE =
  24. ' KEY: () => import(/* webpackChunkName: "KEY-parser" */ \'./KEY/KEYSyntaxParser\')';
  25. const AUTOCOMPLETE_IMPORT_TEMPLATE =
  26. ' KEY: () => import(/* webpackChunkName: "KEY-parser" */ \'./KEY/KEYAutocompleteParser\')';
  27. const GENERATED_NOTICE = `// PLEASE NOTE!
  28. // Do not modify, the contents of this file is generated.
  29. // For more info see tools/jison/generateParserModuleImports.js
  30. `;
  31. const isFolder = async fileSystemEntry => {
  32. const entryPath = pathModule.join(PARSER_FOLDER, fileSystemEntry);
  33. return fsPromises.stat(entryPath).then(stats => stats.isDirectory());
  34. };
  35. const asyncFilter = async (arr, predicate) => {
  36. const orderedBooleans = await Promise.all(arr.map(predicate));
  37. return arr.filter((item, index) => orderedBooleans[index]);
  38. };
  39. const getParserNamesFromFolders = async () => {
  40. try {
  41. const fileSystemEntries = await fsPromises.readdir(PARSER_FOLDER);
  42. return await asyncFilter(fileSystemEntries, isFolder);
  43. } catch (err) {
  44. console.error(`Failed generating parser names from folders in ${PARSER_FOLDER}`);
  45. throw err;
  46. }
  47. };
  48. const getImports = async (parserNames, template, parserType) => {
  49. const parserExists = parserName =>
  50. fsExtra.pathExists(`${PARSER_FOLDER}/${parserName}/${parserName}${parserType}Parser.js`);
  51. const filtered = await asyncFilter(parserNames, parserExists);
  52. return filtered
  53. .map(parserName => template.replace(/KEY/g, parserName))
  54. .sort()
  55. .join(',\n');
  56. };
  57. const writeFile = async fileContent => {
  58. try {
  59. await fsExtra.ensureFile(SQL_PARSER_MODULES_PATH);
  60. await fsPromises.writeFile(SQL_PARSER_MODULES_PATH, fileContent);
  61. } catch (err) {
  62. console.error(`Failed writing data to file file: ${SQL_PARSER_MODULES_PATH}`, err);
  63. throw err;
  64. }
  65. };
  66. const generateParserModulesFile = async () => {
  67. try {
  68. // eslint-disable-next-line
  69. console.log('Generating file parserModules.ts...');
  70. const parserNames = await getParserNamesFromFolders();
  71. const syntaxImports = await getImports(parserNames, SYNTAX_IMPORT_TEMPLATE, 'Syntax');
  72. const autocompleteImports = await getImports(
  73. parserNames,
  74. AUTOCOMPLETE_IMPORT_TEMPLATE,
  75. 'Autocomplete'
  76. );
  77. const exportSyntaxLines = `export const SYNTAX_MODULES = {\n${syntaxImports}\n};\n`;
  78. const exportAutoCompleteLines = `export const AUTOCOMPLETE_MODULES = {\n${autocompleteImports}\n};\n`;
  79. const fileContent = `${LICENSE}\n${GENERATED_NOTICE}\n${exportSyntaxLines}${exportAutoCompleteLines}`;
  80. await writeFile(fileContent);
  81. // eslint-disable-next-line
  82. console.log('Done writing parserModules.ts!\n');
  83. } catch (err) {
  84. console.error(`Failed generating parser modules import file: ${SQL_PARSER_MODULES_PATH}`, err);
  85. }
  86. };
  87. generateParserModulesFile();