|
| 1 | +import { existsSync, readdirSync, statSync } from 'node:fs'; |
| 2 | +import { createRequire } from 'node:module'; |
| 3 | +import { dirname, join } from 'node:path'; |
| 4 | +import { normalizePath } from 'vite'; |
| 5 | + |
| 6 | +const require = createRequire(import.meta.url); |
| 7 | +/** |
| 8 | + * Temporary function to copy the built Kaoto Camel Catalog into the assets folder |
| 9 | + * |
| 10 | + * When dynamically importing the Camel Catalog is supported, this function can be removed |
| 11 | + * and this file can be restored to a .ts file. |
| 12 | + * Related issue: https://github.com/sveltejs/vite-plugin-svelte/issues/141#issuecomment-898900239 |
| 13 | + */ |
| 14 | +export const getCatalogFiles = () => { |
| 15 | + let camelCatalogPath = ''; |
| 16 | + |
| 17 | + try { |
| 18 | + const camelCatalogIndexJsonPath = require.resolve('@kaoto/camel-catalog/index.json'); |
| 19 | + camelCatalogPath = normalizePath(dirname(camelCatalogIndexJsonPath)); |
| 20 | + } catch (error) { |
| 21 | + throw new Error(`Could not find '@kaoto/camel-catalog' \n\n ${error}`); |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + if (readdirSync(camelCatalogPath).length === 0) { |
| 26 | + throw new Error(); |
| 27 | + } |
| 28 | + } catch (error) { |
| 29 | + const message = [ |
| 30 | + `The '${camelCatalogPath}' folder is empty.`, |
| 31 | + 'No files found in the Camel Catalog directory.', |
| 32 | + 'Please check the dependency is installed', |
| 33 | + 'or run `yarn add @kaoto/camel-catalog` to install it', |
| 34 | + ]; |
| 35 | + |
| 36 | + throw new Error(message.join('\n\n')); |
| 37 | + } |
| 38 | + |
| 39 | + console.info(`Found Camel in '@kaoto/camel-catalog' using ${camelCatalogPath}`, '\n'); |
| 40 | + |
| 41 | + /** Recursively list all the JSON and XSD files in the Camel Catalog folder and subfolders */ |
| 42 | + const catalogFiles = []; |
| 43 | + getFilesRecursively(camelCatalogPath, catalogFiles); |
| 44 | + |
| 45 | + /** Add Citrus catalog files */ |
| 46 | + catalogFiles.push(... getCitrusCatalogFiles()); |
| 47 | + |
| 48 | + const basePath = normalizePath(dirname(require.resolve('@kaoto/camel-catalog'))); |
| 49 | + |
| 50 | + return { |
| 51 | + basePath: basePath, |
| 52 | + files: catalogFiles.filter((file) => file.endsWith('.json') || file.endsWith('.xsd')), |
| 53 | + }; |
| 54 | +}; |
| 55 | + |
| 56 | +function getCitrusCatalogFiles() { |
| 57 | + let citrusCatalogPath = ''; |
| 58 | + |
| 59 | + try { |
| 60 | + const citrusCatalogIndexJsonPath = require.resolve('@kaoto/camel-catalog/citrus/index.json'); |
| 61 | + citrusCatalogPath = normalizePath(dirname(citrusCatalogIndexJsonPath)); |
| 62 | + } catch (error) { |
| 63 | + console.warn(`Could not find Citrus in '@kaoto/camel-catalog' \n\n ${error}`); |
| 64 | + return []; |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + if (readdirSync(citrusCatalogPath).length === 0) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + } catch (error) { |
| 72 | + const message = [ |
| 73 | + `The '${citrusCatalogPath}' folder is empty.`, |
| 74 | + 'No files found in the Citrus Catalog directory.', |
| 75 | + 'Please check the dependency is installed', |
| 76 | + 'or run `yarn add @kaoto/camel-catalog` to install it', |
| 77 | + ]; |
| 78 | + |
| 79 | + console.warn(message.join('\n\n')); |
| 80 | + } |
| 81 | + |
| 82 | + console.info(`Found Citrus in '@kaoto/camel-catalog' using ${citrusCatalogPath}`, '\n'); |
| 83 | + |
| 84 | + /** Recursively list all the JSON and XSD files in the Citrus Catalog folder and subfolders */ |
| 85 | + const catalogFiles = []; |
| 86 | + getFilesRecursively(citrusCatalogPath, catalogFiles); |
| 87 | + |
| 88 | + return catalogFiles.filter((file) => file.endsWith('.json') || file.endsWith('.xsd')); |
| 89 | +} |
| 90 | + |
| 91 | +function getFilesRecursively(source, files) { |
| 92 | + const exists = existsSync(source); |
| 93 | + const stats = exists && statSync(source); |
| 94 | + const isDirectory = exists && stats.isDirectory(); |
| 95 | + |
| 96 | + if (isDirectory) { |
| 97 | + const directoryFiles = readdirSync(source); |
| 98 | + |
| 99 | + for (const file of directoryFiles) { |
| 100 | + getFilesRecursively(join(source, file), files); |
| 101 | + } |
| 102 | + } else { |
| 103 | + files.push(source); |
| 104 | + } |
| 105 | +} |
0 commit comments