check_for_absolute_paths.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 TARGET_EXTENSIONS = /\.(js|map|css)$/i;
  18. const FOLDERS_TO_CHECK = ['desktop/core/src/desktop/static'];
  19. const HUE_ABSOLUTE_PATH = __dirname.replace('/tools/ci', '');
  20. const scanFile = async path =>
  21. new Promise(resolve => {
  22. fs.readFile(path, (err, data) => {
  23. if (!err) {
  24. if (data.indexOf(HUE_ABSOLUTE_PATH) !== -1) {
  25. resolve(path);
  26. }
  27. }
  28. resolve();
  29. });
  30. });
  31. const appendFilesRecursively = (path, foundFiles) => {
  32. const files = fs.readdirSync(path, { withFileTypes: true });
  33. files.forEach(file => {
  34. const absolutePath = path + '/' + file.name;
  35. if (file.isFile() && TARGET_EXTENSIONS.test(file.name)) {
  36. foundFiles.push(absolutePath);
  37. } else if (file.isDirectory()) {
  38. appendFilesRecursively(absolutePath, foundFiles);
  39. }
  40. });
  41. };
  42. const runCheck = () => {
  43. // eslint-disable-next-line no-restricted-syntax
  44. console.log('Checking if files contain the absolute path "' + HUE_ABSOLUTE_PATH + '"...');
  45. const filesToScan = [];
  46. FOLDERS_TO_CHECK.forEach(folder =>
  47. appendFilesRecursively(HUE_ABSOLUTE_PATH + '/' + folder, filesToScan)
  48. );
  49. Promise.all(filesToScan.map(scanFile)).then(results => {
  50. const foundFilesWithAbsolutePath = results.filter(result => result);
  51. if (foundFilesWithAbsolutePath.length) {
  52. console.warn(
  53. `Found in ${
  54. foundFilesWithAbsolutePath.length
  55. } file(s):\n ${foundFilesWithAbsolutePath.join('\n ')}`
  56. );
  57. process.exitCode = 1;
  58. } else {
  59. // eslint-disable-next-line no-restricted-syntax
  60. console.log(`Done! Scanned ${filesToScan.length} files.`);
  61. }
  62. });
  63. };
  64. runCheck();