|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +const { join } = require('path'); |
| 21 | +const { readdirSync, readFileSync, writeFileSync, renameSync } = require('fs'); |
| 22 | +const ora = require('ora'); |
| 23 | + |
| 24 | +const generatedAntlrFolder = join(__dirname, '..', 'src', 'painless', 'antlr'); |
| 25 | + |
| 26 | +const generatedAntlrFolderContents = readdirSync(generatedAntlrFolder); |
| 27 | + |
| 28 | +const log = ora('Updating generated antlr grammar').start(); |
| 29 | + |
| 30 | +// The generated TS produces some TS linting errors |
| 31 | +// This script adds a //@ts-nocheck comment at the top of each generated file |
| 32 | +// so that the errors can be ignored for now |
| 33 | +generatedAntlrFolderContents |
| 34 | + .filter((file) => { |
| 35 | + const fileExtension = file.split('.')[1]; |
| 36 | + return fileExtension.includes('ts'); |
| 37 | + }) |
| 38 | + .forEach((file) => { |
| 39 | + try { |
| 40 | + const fileContentRows = readFileSync(join(generatedAntlrFolder, file), 'utf8') |
| 41 | + .toString() |
| 42 | + .split('\n'); |
| 43 | + |
| 44 | + fileContentRows.unshift('// @ts-nocheck'); |
| 45 | + |
| 46 | + const filePath = join(generatedAntlrFolder, file); |
| 47 | + const fileContent = fileContentRows.join('\n'); |
| 48 | + |
| 49 | + writeFileSync(filePath, fileContent, { encoding: 'utf8' }); |
| 50 | + } catch (err) { |
| 51 | + return log.fail(err.message); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | +// Rename generated parserListener file to snakecase to satisfy file casing check |
| 56 | +// There doesn't appear to be a way to fix this OOTB with antlr4ts-cli |
| 57 | +try { |
| 58 | + renameSync( |
| 59 | + join(generatedAntlrFolder, 'painless_parserListener.ts'), |
| 60 | + join(generatedAntlrFolder, 'painless_parser_listener.ts') |
| 61 | + ); |
| 62 | +} catch (err) { |
| 63 | + log.warn(`Unable to rename parserListener file to snakecase: ${err.message}`); |
| 64 | +} |
| 65 | + |
| 66 | +log.succeed('Updated generated antlr grammar successfully'); |
0 commit comments