Skip to content

Commit 86a6481

Browse files
christophdlordrip
authored andcommitted
Get and copy Citrus catalog from @kaoto/camel-catalog
- Include Citrus catalog files loaded from @kaoto/camel-catalog - Copy Citrus catalog with vite-plugin - Include Citrus catalog when copying catalog files
1 parent 2153508 commit 86a6481

File tree

5 files changed

+117
-77
lines changed

5 files changed

+117
-77
lines changed

packages/ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"build:esm": "tsc --project tsconfig.esm.json && copyfiles -u 1 './src/**/*.scss' './src/assets/**' ./lib/esm",
4343
"build:cjs": "tsc --project tsconfig.cjs.json && copyfiles -u 1 './src/**/*.scss' './src/assets/**' ./lib/cjs",
4444
"write-version": "node ./scripts/write-version-file.mjs",
45-
"copy-catalog": "node ./scripts/copy-camel-catalog-files.mjs ./lib/camel-catalog",
45+
"copy-catalog": "node ./scripts/copy-catalog-files.mjs ./lib",
4646
"preview": "vite preview",
4747
"test": "jest",
4848
"test:watch": "jest --watch",

packages/ui/scripts/copy-camel-catalog-files.mjs renamed to packages/ui/scripts/copy-catalog-files.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { copyFileSync, existsSync, mkdirSync } from 'node:fs';
22
import { basename, dirname, relative, resolve } from 'node:path';
33

44
/**
5-
* Copy the built Kaoto Camel Catalog files into the assets/camel-catalog folder
5+
* Copy the built Kaoto Camel Catalog files into the assets folder
66
*/
7-
async function copyCamelCatalogFiles(destinationFolder) {
8-
const { getCamelCatalogFiles } = await import('./get-camel-catalog-files.mjs');
9-
const { basePath, files: camelCatalogFiles } = getCamelCatalogFiles();
7+
async function copyCatalogFiles(destinationFolder) {
8+
const { getCatalogFiles } = await import('./get-catalog-files.mjs');
9+
const { basePath, files: catalogFiles } = getCatalogFiles();
1010

11-
camelCatalogFiles.forEach((file) => {
11+
catalogFiles.forEach((file) => {
1212
const relativePath = relative(basePath, file);
1313
const destDir = resolve(destinationFolder, dirname(relativePath));
1414

@@ -25,7 +25,7 @@ async function copyCamelCatalogFiles(destinationFolder) {
2525

2626
// eslint-disable-next-line no-undef
2727
const dest = process.argv[2];
28-
console.info(`Copying Kaoto Camel Catalog files to '${dest}'`, '\n');
28+
console.info(`Copying Kaoto catalog files to '${dest}'`, '\n');
2929

3030
if (!dest) {
3131
throw new Error('Missing destination folder');
@@ -35,4 +35,4 @@ if (!existsSync(dest)) {
3535
mkdirSync(dest, { recursive: true });
3636
}
3737

38-
copyCamelCatalogFiles(dest);
38+
copyCatalogFiles(dest);

packages/ui/scripts/get-camel-catalog-files.mjs

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

packages/ui/vite.config.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import { dirname, relative } from 'node:path';
44
import { defineConfig, normalizePath } from 'vite';
55
import { viteStaticCopy } from 'vite-plugin-static-copy';
66
import packageJson from './package.json';
7-
import { getCamelCatalogFiles } from './scripts/get-camel-catalog-files.mjs';
7+
import { getCatalogFiles } from './scripts/get-catalog-files.mjs';
88
import { getLastCommitInfo } from './scripts/get-last-commit-info.mjs';
99

1010
// https://vitejs.dev/config/
1111

1212
export default defineConfig(async () => {
1313
const outDir = './dist';
1414
const lastCommitInfo = await getLastCommitInfo();
15-
const { basePath, files: camelCatalogFiles } = getCamelCatalogFiles();
15+
const { basePath, files: catalogFiles } = getCatalogFiles();
1616

1717
return {
1818
plugins: [
1919
react(),
2020
viteStaticCopy({
21-
targets: camelCatalogFiles.map((file) => {
21+
targets: catalogFiles.map((file) => {
2222
const normalizedFile = normalizePath(file);
2323
const relativePath = relative(basePath, file);
24-
const dest = normalizePath('./camel-catalog/' + dirname(relativePath));
24+
const dest = normalizePath('./' + dirname(relativePath));
2525

2626
return {
2727
src: normalizedFile,

0 commit comments

Comments
 (0)