@yolean/evidence-sdk 0.0.0-stage → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/CHANGELOG.md +538 -0
  2. package/LICENSE.md +9 -0
  3. package/README.md +237 -2
  4. package/package.json +161 -3
  5. package/src/build-dev/cli/add/addServerHook.js +30 -0
  6. package/src/build-dev/cli/add/addTypeRef.js +19 -0
  7. package/src/build-dev/cli/add/updateGitIgnore.js +33 -0
  8. package/src/build-dev/cli/add/updatePackageJson.js +26 -0
  9. package/src/build-dev/cli/add/updateSvelteConfig.js +199 -0
  10. package/src/build-dev/cli/add/updateViteConfig.js +152 -0
  11. package/src/build-dev/cli/add.js +95 -0
  12. package/src/build-dev/cli/build.js +30 -0
  13. package/src/build-dev/cli/dev.js +49 -0
  14. package/src/build-dev/cli/index.js +14 -0
  15. package/src/build-dev/cli/upgrade.js +54 -0
  16. package/src/build-dev/svelte/index.js +54 -0
  17. package/src/build-dev/svelte/lib/isTargetFile.js +6 -0
  18. package/src/build-dev/svelte/metadatas.js +39 -0
  19. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.js +29 -0
  20. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.spec.js +82 -0
  21. package/src/build-dev/svelte/processors/addScriptTags.js +17 -0
  22. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.js +19 -0
  23. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.spec.js +123 -0
  24. package/src/build-dev/svelte/processors/convertToHtml/processMarkdown.js +59 -0
  25. package/src/build-dev/svelte/processors/extractQueries/extractQueries.js +49 -0
  26. package/src/build-dev/svelte/processors/extractQueries/extractQueries.spec.js +54 -0
  27. package/src/build-dev/svelte/processors/highlight/highlight.js +52 -0
  28. package/src/build-dev/svelte/processors/highlight/highlight.spec.js +31 -0
  29. package/src/build-dev/svelte/processors/initializeUniversalSql.js +15 -0
  30. package/src/build-dev/svelte/processors/injectComponents/injectComponents.js +56 -0
  31. package/src/build-dev/svelte/processors/injectHighlightStyles.js +14 -0
  32. package/src/build-dev/svelte/processors/injectQueries/injectQueries.js +77 -0
  33. package/src/build-dev/svelte/processors/markRootLayoutPrerenderable.js +56 -0
  34. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.js +17 -0
  35. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.spec.js +21 -0
  36. package/src/build-dev/svelte/processors/transformQueries/transformQueries.js +58 -0
  37. package/src/build-dev/vite/config-virtual.js +24 -0
  38. package/src/build-dev/vite/constants.js +7 -0
  39. package/src/build-dev/vite/datasource-plugins/source-query-hmr.js +117 -0
  40. package/src/build-dev/vite/evidence-config.d.ts +6 -0
  41. package/src/build-dev/vite/index.js +214 -0
  42. package/src/build-dev/vite/layout-plugins/copy-to-layout.js +71 -0
  43. package/src/build-dev/vite/plugins.js +4 -0
  44. package/src/build-dev/vite/query-directory-hmr.js +32 -0
  45. package/src/build-dev/vite/sidecar-app.js +79 -0
  46. package/src/build-dev/vite/virtuals/QuerySSR.svelte +13 -0
  47. package/src/build-dev/vite/virtuals/__tests/proxyStack.spec.js +52 -0
  48. package/src/build-dev/vite/virtuals/bootstrapContexts.svelte.js +27 -0
  49. package/src/build-dev/vite/virtuals/browser/static-assets.js +3 -0
  50. package/src/build-dev/vite/virtuals/contextKeys.js +3 -0
  51. package/src/build-dev/vite/virtuals/initUsql.js +23 -0
  52. package/src/build-dev/vite/virtuals/node/static-assets.js +56 -0
  53. package/src/build-dev/vite/virtuals/proxyStack.js +37 -0
  54. package/src/build-dev/vite/virtuals/queries.svelte.js +150 -0
  55. package/src/build-dev/vite/virtuals/ssrHook.svelte.js +60 -0
  56. package/src/build-dev/vite/virtuals/updateQueryContext.svelte.js +31 -0
  57. package/src/build-dev/vite/virtuals-dynamic.js +12 -0
  58. package/src/cli.js +5 -0
  59. package/src/cli.root.js +90 -0
  60. package/src/configuration/cli/create.js +20 -0
  61. package/src/configuration/cli/index.js +12 -0
  62. package/src/configuration/createEvidenceConfig.js +51 -0
  63. package/src/configuration/getEvidenceConfig.js +60 -0
  64. package/src/configuration/getEvidenceConfig.legacy.js +27 -0
  65. package/src/configuration/index.js +1 -0
  66. package/src/configuration/schemas/config.schema.js +11 -0
  67. package/src/configuration/schemas/deployment.schema.js +9 -0
  68. package/src/configuration/schemas/deployment.schema.spec.js +34 -0
  69. package/src/configuration/updateEvidenceConfig.js +25 -0
  70. package/src/legacy-compat.js +8 -0
  71. package/src/lib/EvidenceError.js +35 -0
  72. package/src/lib/b64Deep.js +55 -0
  73. package/src/lib/check-import.js +12 -0
  74. package/src/lib/cleanZodErrors.js +22 -0
  75. package/src/lib/cli/cmdFail.js +33 -0
  76. package/src/lib/cli/notices.js +44 -0
  77. package/src/lib/debug.js +40 -0
  78. package/src/lib/flows.d.ts +21 -0
  79. package/src/lib/forceLink.js +28 -0
  80. package/src/lib/history/index.js +70 -0
  81. package/src/lib/history/types.d.ts +8 -0
  82. package/src/lib/pkg-types.helpers.js +16 -0
  83. package/src/lib/processing-queue.js +96 -0
  84. package/src/lib/processing-queue.spec.js +148 -0
  85. package/src/lib/projectPaths.js +29 -0
  86. package/src/lib/projectRoot.js +12 -0
  87. package/src/lib/sharedPromise.js +77 -0
  88. package/src/lib/sharedPromise.spec.js +23 -0
  89. package/src/lib/strict.js +19 -0
  90. package/src/lib/tests/fs.js +21 -0
  91. package/src/lib/tokens.js +7 -0
  92. package/src/lib/unnest-zod-error.js +14 -0
  93. package/src/logger/EvidenceLogger.js +164 -0
  94. package/src/logger/index.js +4 -0
  95. package/src/meta.js +8 -0
  96. package/src/plugins/cli/index.js +13 -0
  97. package/src/plugins/cli/install.js +26 -0
  98. package/src/plugins/cli/install.prompt.js +121 -0
  99. package/src/plugins/components/getAllComponents.js +82 -0
  100. package/src/plugins/components/getComponentsInPlugin.js +33 -0
  101. package/src/plugins/components/loadComponentPlugins.js +48 -0
  102. package/src/plugins/components/loaders/file-loader.js +125 -0
  103. package/src/plugins/components/loaders/manifest-loader.js +53 -0
  104. package/src/plugins/components/schemas/component-manifest.schema.js +5 -0
  105. package/src/plugins/components/types.d.ts +21 -0
  106. package/src/plugins/components/validateOverrides.js +30 -0
  107. package/src/plugins/components/validateOverrides.spec.js +32 -0
  108. package/src/plugins/datasources/Datasources.js +85 -0
  109. package/src/plugins/datasources/Datasources.spec.js +78 -0
  110. package/src/plugins/datasources/SourceResultCache.js +62 -0
  111. package/src/plugins/datasources/buildSourceDirectoryProxy.js +21 -0
  112. package/src/plugins/datasources/cli/edit/Options.fixtures.js +69 -0
  113. package/src/plugins/datasources/cli/edit/Options.js +253 -0
  114. package/src/plugins/datasources/cli/edit/Options.spec.js +496 -0
  115. package/src/plugins/datasources/cli/edit/editSourceConfig.js +201 -0
  116. package/src/plugins/datasources/cli/edit.js +20 -0
  117. package/src/plugins/datasources/cli/edit.prompt.js +100 -0
  118. package/src/plugins/datasources/cli/index.js +18 -0
  119. package/src/plugins/datasources/cli/sources.js +77 -0
  120. package/src/plugins/datasources/evalSources.js +257 -0
  121. package/src/plugins/datasources/getPublishedDatasources.js +62 -0
  122. package/src/plugins/datasources/index.js +6 -0
  123. package/src/plugins/datasources/loadSourceConfig.js +147 -0
  124. package/src/plugins/datasources/loadSourceConfig.spec.js +127 -0
  125. package/src/plugins/datasources/loadSourcePlugins.js +49 -0
  126. package/src/plugins/datasources/loadSources.js +48 -0
  127. package/src/plugins/datasources/schemas/datasource.schema.js +52 -0
  128. package/src/plugins/datasources/schemas/datasourceCache.schema.js +3 -0
  129. package/src/plugins/datasources/schemas/datasourcePlugin.schema.js +37 -0
  130. package/src/plugins/datasources/schemas/datasourcePluginOptions.schema.js +64 -0
  131. package/src/plugins/datasources/schemas/index.js +2 -0
  132. package/src/plugins/datasources/schemas/manifest.schema.js +8 -0
  133. package/src/plugins/datasources/schemas/results.schema.js +126 -0
  134. package/src/plugins/datasources/sub-source-vars.js +55 -0
  135. package/src/plugins/datasources/sub-source-vars.spec.js +46 -0
  136. package/src/plugins/datasources/types.d.ts +53 -0
  137. package/src/plugins/datasources/updateManifest.js +115 -0
  138. package/src/plugins/datasources/updateManifest.spec.js +200 -0
  139. package/src/plugins/datasources/wrapSimpleConnector.js +95 -0
  140. package/src/plugins/datasources/writeSourceConfig.js +60 -0
  141. package/src/plugins/index.js +2 -0
  142. package/src/plugins/layouts/copyMethods/index.js +10 -0
  143. package/src/plugins/layouts/copyMethods/svelte.js +53 -0
  144. package/src/plugins/layouts/copyMethods/sveltekit.js +75 -0
  145. package/src/plugins/layouts/copyToLayout.js +106 -0
  146. package/src/plugins/layouts/loadLayoutPlugin.js +23 -0
  147. package/src/plugins/loadPluginPackage.js +77 -0
  148. package/src/plugins/schemas/index.js +1 -0
  149. package/src/plugins/schemas/plugin-config.schema.js +41 -0
  150. package/src/plugins/schemas/plugin-config.schema.spec.js +21 -0
  151. package/src/plugins/schemas/plugin-package.schema.js +154 -0
  152. package/src/plugins/schemas/plugin-package.schema.spec.js +30 -0
  153. package/src/query-builder.d.ts +1 -0
  154. package/src/query-builder.js +1 -0
  155. package/src/types/duckdb-wellknown.d.ts +35 -0
  156. package/src/types/html-attributes.ts +13 -0
  157. package/src/types/index.d.ts +18 -0
  158. package/src/types/mosaic-sql.d.ts +27 -0
  159. package/src/types/sveltekit-autoimport.d.ts +13 -0
  160. package/src/types/virtuals.d.ts +113 -0
  161. package/src/types.d.ts +1 -0
  162. package/src/usql/index.d.ts +4 -0
  163. package/src/usql/index.js +3 -0
  164. package/src/usql/query/Query.js +1391 -0
  165. package/src/usql/query/Query.spec.js +827 -0
  166. package/src/usql/query/queryScore.js +92 -0
  167. package/src/usql/query/queryScore.spec.js +26 -0
  168. package/src/usql/query/sterilizeQuery.js +89 -0
  169. package/src/usql/query/sterilizeQuery.spec.js +172 -0
  170. package/src/usql/setTrackProxy/setTrackProxy.js +80 -0
  171. package/src/usql/setTrackProxy/setTrackProxy.spec.js +166 -0
  172. package/src/usql/types.ts +86 -0
  173. package/src/usql/utilities/duckdbSerialize.js +29 -0
  174. package/src/usql/utilities/index.js +2 -0
  175. package/src/usql/utilities/resolveMaybePromise.js +30 -0
  176. package/src/utils/batchUp.js +22 -0
  177. package/src/utils/batchUp.spec.js +69 -0
  178. package/src/utils/browserDebounce.js +16 -0
  179. package/src/utils/index.js +7 -0
  180. package/src/utils/svelte/addBasePath.js +45 -0
  181. package/src/utils/svelte/addBasePath.spec.js +86 -0
  182. package/src/utils/svelte/index.js +5 -0
  183. package/src/utils/svelte/inputs.js +138 -0
  184. package/tsconfig.json +22 -0
  185. package/vite.config.js +3 -0
  186. package/vitest.config.js +15 -0
@@ -0,0 +1,121 @@
1
+ import * as prompt from '@clack/prompts';
2
+ import { getEvidenceConfig } from '../../configuration/getEvidenceConfig.js';
3
+ import { getPublishedDatasources } from '../datasources/getPublishedDatasources.js';
4
+ import { EvidenceError } from '../../lib/EvidenceError.js';
5
+ import chalk from 'chalk';
6
+ import { readPackageJSON, resolvePackageJSON, writePackageJSON } from 'pkg-types';
7
+ import { updateEvidenceConfig } from '../../configuration/updateEvidenceConfig.js';
8
+
9
+ /**
10
+ * @param {boolean} [installOnly = false]
11
+ * @param {"datasource"} [installType]
12
+ * @param {boolean} [skipCancel]
13
+ * @returns {Promise<boolean>}
14
+ */
15
+ export const installPluginPrompt = async (installOnly, installType, skipCancel) => {
16
+ let action;
17
+
18
+ const bail = skipCancel ? prompt.log.error : prompt.cancel;
19
+
20
+ if (!installOnly) {
21
+ action = await prompt.select({
22
+ options: [
23
+ { label: 'Install Plugins', value: 'install' },
24
+ { label: 'Remove Plugins', value: 'remove' }
25
+ ],
26
+ message: 'What would you like to do?'
27
+ });
28
+ if (action === 'remove') throw new EvidenceError('This action is not currently supported');
29
+ if (prompt.isCancel(action)) {
30
+ bail('Cancelled');
31
+ return false;
32
+ }
33
+ } else {
34
+ action = 'install';
35
+ }
36
+ let pluginType;
37
+ if (!installType) {
38
+ pluginType = await prompt.select({
39
+ message: 'What kind of plugin would you like to install?',
40
+ options: [{ label: 'Datasource', value: 'datasource' }]
41
+ });
42
+ if (prompt.isCancel(pluginType)) {
43
+ bail('Cancelled');
44
+ return false;
45
+ }
46
+ } else {
47
+ pluginType = installType;
48
+ }
49
+
50
+ switch (pluginType) {
51
+ case 'datasource': {
52
+ const availableLoad = prompt.spinner();
53
+
54
+ availableLoad.start('Fetching available sources');
55
+ const [availableSources, evidenceConfig] = await Promise.all([
56
+ getPublishedDatasources(),
57
+ getEvidenceConfig()
58
+ ]);
59
+ availableLoad.stop('Done!', 0);
60
+
61
+ const maxNameLength = Math.max(...availableSources.map((s) => s.name.length));
62
+ const options = availableSources.map((source) => {
63
+ const installed = source.name in (evidenceConfig.plugins.datasources ?? {});
64
+
65
+ let message = '';
66
+
67
+ if (installed) message += chalk.green('[Installed] ');
68
+ else message += ' ';
69
+
70
+ message += source.name.padEnd(maxNameLength);
71
+ message += chalk.dim(` (${source.version})`);
72
+
73
+ message += ` | (${source.evidence.datasources
74
+ .map((s) => (Array.isArray(s) ? s[0] : s))
75
+ .join(', ')})`;
76
+
77
+ return {
78
+ label: message,
79
+ value: source
80
+ };
81
+ });
82
+
83
+ /** @type {symbol | import("../schemas/plugin-package.schema.js").DatasourcePackage} */
84
+ const selectedSource = await prompt.select({
85
+ message: 'Which source would you like to install?',
86
+ options
87
+ });
88
+ if (prompt.isCancel(selectedSource) || !selectedSource) {
89
+ bail('Cancelled');
90
+ return false;
91
+ }
92
+
93
+ if (!selectedSource.name.startsWith('@evidence-dev')) {
94
+ const unsafe = await prompt.confirm({
95
+ message: `${chalk.yellow.bold(
96
+ `${selectedSource.name}`
97
+ )} is a 3rd party plugin, and was not created by Evidence. Are you sure you would like to install it?`,
98
+ initialValue: false
99
+ });
100
+ if (prompt.isCancel(unsafe) || !unsafe) {
101
+ bail('Cancelled');
102
+ return false;
103
+ }
104
+ }
105
+
106
+ const pack = await readPackageJSON();
107
+ if (!pack.dependencies) pack.dependencies = {};
108
+ pack.dependencies[selectedSource.name] = selectedSource.version;
109
+ await writePackageJSON(await resolvePackageJSON(), pack);
110
+ prompt.log.info('package.json updated');
111
+
112
+ if (!evidenceConfig.plugins.datasources) evidenceConfig.plugins.datasources = {};
113
+ evidenceConfig.plugins.datasources[selectedSource.name] = {};
114
+ await updateEvidenceConfig(evidenceConfig);
115
+ prompt.log.info('evidence.config.yaml updated');
116
+
117
+ break;
118
+ }
119
+ }
120
+ return true;
121
+ };
@@ -0,0 +1,82 @@
1
+ import { getComponentsInPlugin } from './getComponentsInPlugin.js';
2
+ import { loadComponentPlugins } from './loadComponentPlugins.js';
3
+ import fs from 'fs';
4
+ import { findSvelteComponents } from './loaders/file-loader.js';
5
+ import path from 'path';
6
+ import chalk from 'chalk';
7
+
8
+ /**
9
+ * @typedef {Object} ComponentResolution
10
+ * @property {string} [aliasOf]
11
+ */
12
+
13
+ export const getAllComponents = async () => {
14
+ const plugins = await loadComponentPlugins();
15
+ const allComponents = await Promise.all(
16
+ plugins.map(
17
+ /** @returns {Promise<[import("./loadComponentPlugins.js").ComponentInfo, Set<string>]>} */
18
+ async (plugin) => [plugin, await getComponentsInPlugin(plugin)]
19
+ )
20
+ );
21
+
22
+ const mappedComponents = allComponents.reduce(
23
+ (
24
+ /** @type {Record<string, import("./loadComponentPlugins.js").ComponentInfo & ComponentResolution>} */ c,
25
+ [plugin, components]
26
+ ) => {
27
+ for (const component of components) {
28
+ let componentName = component;
29
+ /** @type {import("./loadComponentPlugins.js").ComponentInfo & ComponentResolution} */
30
+ const input = { ...plugin };
31
+
32
+ // Handle aliasing logic
33
+ const alias = plugin.options.aliases[component];
34
+ if (alias) {
35
+ input.aliasOf = component;
36
+ componentName = alias;
37
+ }
38
+
39
+ // This component (or alias) has already been specified, and this package does not override it.
40
+ if (c[componentName] && !plugin.options.overrides.includes(componentName)) {
41
+ const existingPackage = c[componentName].package.name;
42
+ console.warn(
43
+ chalk.yellow(
44
+ `[!] ${plugin.package.name} and ${existingPackage} both provide ${componentName}. ${existingPackage} will be used, to use ${plugin.package.name}, specify an alias or explicit override. (https://docs.evidence.dev/plugins#component-aliases)`
45
+ )
46
+ );
47
+ // no-op
48
+ return c;
49
+ }
50
+
51
+ c[componentName] = plugin;
52
+ }
53
+ return c;
54
+ },
55
+ {}
56
+ );
57
+
58
+ const rootDir = process.cwd();
59
+
60
+ if (fs.existsSync(`${rootDir}/components`)) {
61
+ const user_components = await findSvelteComponents(`${rootDir}/components`);
62
+ for (const component_file of user_components) {
63
+ const component = path.basename(component_file, '.svelte');
64
+ if (mappedComponents[component]) {
65
+ console.warn(
66
+ chalk.yellow(
67
+ [
68
+ `${chalk.bold(
69
+ `[!] The components folder and ${mappedComponents[component].package} both provide ${component}`
70
+ )}.`,
71
+ 'The component from the components folder will be used.',
72
+ `To use the component from ${mappedComponents[component].package}, specify an alias (https://docs.evidence.dev/plugins/using-plugins/#component-aliases) or explicitly import the component.`
73
+ ].join('\n\t')
74
+ )
75
+ );
76
+ delete mappedComponents[component];
77
+ }
78
+ }
79
+ }
80
+
81
+ return mappedComponents;
82
+ };
@@ -0,0 +1,33 @@
1
+ import path from 'path';
2
+ import fs from 'fs/promises';
3
+ import { fileLoader } from './loaders/file-loader.js';
4
+ import { manifestLoader } from './loaders/manifest-loader.js';
5
+
6
+ /**
7
+ * @param {import("./loadComponentPlugins.js").ComponentInfo} plugin
8
+ */
9
+ export const getComponentsInPlugin = async (plugin) => {
10
+ /** @type {Set<string>} */
11
+ const providedComponents = new Set();
12
+
13
+ if (plugin.options.provides.length) {
14
+ plugin.options.provides.forEach((c) => providedComponents.add(c));
15
+ } else {
16
+ const manifestPath = path.resolve(plugin.package.dir, 'evidence.manifest.yaml');
17
+ const manifestExists = await fs
18
+ .access(manifestPath)
19
+ .then(() => true)
20
+ .catch(() => false);
21
+ if (manifestExists) {
22
+ const manifestComponents = await manifestLoader(plugin.package.dir).catch(() => []);
23
+ manifestComponents.forEach((c) => providedComponents.add(c));
24
+ }
25
+
26
+ const mainFilePath = path.parse(path.resolve(plugin.package.dir, plugin.package.main)).dir;
27
+
28
+ const fileComponents = await fileLoader(mainFilePath);
29
+ fileComponents.forEach((c) => providedComponents.add(c));
30
+ }
31
+
32
+ return providedComponents;
33
+ };
@@ -0,0 +1,48 @@
1
+ import { getEvidenceConfig } from '../../configuration/getEvidenceConfig.js';
2
+ import { loadPluginPackage } from '../loadPluginPackage.js';
3
+ import { isComponentPlugin } from '../schemas/plugin-package.schema.js';
4
+ import { validateOverrides } from './validateOverrides.js';
5
+
6
+ /** @typedef {import("../schemas/plugin-package.schema.js").ComponentPackage} ComponentPackage */
7
+ /**
8
+ * @typedef {Object} ComponentInfo
9
+ * @property {string} name
10
+ * @property {ComponentPackage & {dir: string}} package
11
+ * @property {NonNullable<import("../schemas/plugin-config.schema.js").PluginConfig["components"]>[string]} options
12
+ */
13
+
14
+ /**
15
+ *
16
+ * @returns {Promise<ComponentInfo[]>}
17
+ */
18
+ export const loadComponentPlugins = async () => {
19
+ const { plugins, layout } = getEvidenceConfig();
20
+ const allComponentPlugins = plugins.components ?? {};
21
+
22
+ if (layout) {
23
+ const layoutPlugin = await loadPluginPackage(layout);
24
+
25
+ if (layoutPlugin && isComponentPlugin(layoutPlugin)) {
26
+ // If the layout provides components, include them automatically
27
+ allComponentPlugins[layout] = { overrides: [], provides: [], aliases: {} };
28
+ }
29
+ }
30
+
31
+ /** @type {ComponentInfo[]} */
32
+ const components = [];
33
+
34
+ await Promise.all(
35
+ Object.entries(allComponentPlugins).map(async ([name, spec]) => {
36
+ const pack = await loadPluginPackage(name);
37
+ // TODO: Is it actually useful to be able to manually specify a plugin
38
+ // e.g. I load an arbitrary svelte lib and use the provides field
39
+ if (!pack || !isComponentPlugin(pack)) return;
40
+
41
+ components.push({ name, package: pack, options: spec });
42
+ })
43
+ );
44
+
45
+ validateOverrides(components);
46
+
47
+ return components;
48
+ };
@@ -0,0 +1,125 @@
1
+ import fs from 'fs/promises';
2
+ import path from 'path';
3
+ import { parse as svelteParse } from 'svelte/compiler';
4
+
5
+ /**
6
+ * Searches recursively within a directory for svelte files
7
+ * Ignores anything beginning with . (hidden) or + (special sveltekit files)
8
+ *
9
+ * @param {string} root
10
+ * @returns {Promise<string[]>}
11
+ */
12
+ export const findSvelteComponents = async (root) => {
13
+ /**
14
+ * @type {string[]}
15
+ */
16
+ const output = [];
17
+ // Scan & iterate directory
18
+ const directoryContents = await fs.readdir(root, { withFileTypes: true });
19
+ for (const item of directoryContents) {
20
+ // Ignore these cases
21
+ if (item.name === 'node_modules') continue; // Don't touch any dependencies
22
+ if (item.name.startsWith('.')) continue; // Don't touch hidden files
23
+ if (item.name.startsWith('+')) continue; // Don't touch sveltekit files
24
+
25
+ // Build path; get item stats
26
+ const itemPath = path.resolve(root, item.name);
27
+
28
+ if (item.isDirectory()) {
29
+ // Recurse on directories
30
+ output.push(...(await findSvelteComponents(itemPath)));
31
+ } else if (item.name.endsWith('.svelte')) {
32
+ // Keep track of svelte components
33
+ output.push(itemPath);
34
+ }
35
+ }
36
+
37
+ // Return relative filepaths
38
+ return output.map((p) => path.relative('.', p));
39
+ };
40
+
41
+ /**
42
+ * Reduce function to run on a svelte AST.
43
+ * Searches for the evidenceInclude declaration.
44
+ * @example
45
+ * <script context="module">
46
+ * export const evidenceInclude = true
47
+ * </script>
48
+ *
49
+ * @param {import("estree").Node} currentNode
50
+ * @returns {boolean}
51
+ */
52
+ const nodeIsEvidenceDeclaration = (currentNode) => {
53
+ // If this isn't the right kind of declaration, ignore it
54
+ if (currentNode.type !== 'ExportNamedDeclaration') return false;
55
+
56
+ const rootDeclaration = currentNode.declaration;
57
+
58
+ if (rootDeclaration?.type !== 'VariableDeclaration') return false;
59
+
60
+ // const only, this is somewhat inline with sveltekit's patterns
61
+ if (rootDeclaration.kind !== 'const') return false;
62
+
63
+ // This shouldn't be hit, but type safety
64
+ if (!rootDeclaration?.declarations) return false;
65
+
66
+ // Iterate through sub-declarations, I've only ever seen 1 here
67
+ for (const declaration of rootDeclaration.declarations) {
68
+ const { id, init } = declaration;
69
+ // Check to see if this is a declaration for evidenceInclude
70
+ if (id.type !== 'Identifier') continue;
71
+ if (id.name !== 'evidenceInclude') continue;
72
+ // Check to see if the value it is declared with is a true constant
73
+ // We could shorten this; but this reads better
74
+ if (init?.type !== 'Literal') continue;
75
+ if (init.value !== true) continue;
76
+ // We found what we want!
77
+ return true;
78
+ }
79
+ // We never found the right declaration; continue the reduction
80
+ return false;
81
+ };
82
+
83
+ /**
84
+ * Generates an AST and searches it for the special declaration
85
+ * @param {string} fileContent
86
+ */
87
+ export const isLibraryComponent = async (fileContent) => {
88
+ let result = false;
89
+
90
+ // remove style tags, postcss can screw this up
91
+ fileContent = fileContent.replace(/<style.*>(.|[\s])*<\/style>/g, '');
92
+ // First parse the passed in file
93
+ const parseResult = svelteParse(fileContent);
94
+ // If there is a <script> tag, check there
95
+ if (parseResult.instance) {
96
+ result = result || Boolean(parseResult.instance.content.body.find(nodeIsEvidenceDeclaration));
97
+ }
98
+ // If there is a <script context="module"> tag, check there
99
+ if (parseResult.module) {
100
+ result = result || Boolean(parseResult.module.content.body.find(nodeIsEvidenceDeclaration));
101
+ }
102
+ return result;
103
+ };
104
+
105
+ /**
106
+ * @param {string} rootDir
107
+ *
108
+ * @returns {Promise<string[]>}
109
+ */
110
+ export async function fileLoader(rootDir) {
111
+ const componentPaths = await findSvelteComponents(rootDir);
112
+ const results = await Promise.all(
113
+ componentPaths.map(async (componentPath) => ({
114
+ include: await isLibraryComponent(
115
+ await fs.readFile(componentPath).then((p) => p.toString())
116
+ ).catch((e) => {
117
+ throw new Error(`Error while identifying ${componentPath}`, { cause: e });
118
+ }),
119
+ // Get the name of the component, takes the last part of the path and removes the file extension
120
+ componentName: path.basename(componentPath, path.extname(componentPath))
121
+ }))
122
+ );
123
+
124
+ return results.filter((r) => r.include).map((r) => r.componentName);
125
+ }
@@ -0,0 +1,53 @@
1
+ import fs from 'fs/promises';
2
+ import chalk from 'chalk';
3
+ import path from 'path';
4
+ import yaml from 'yaml';
5
+ import { cleanZodErrors } from '../../../lib/cleanZodErrors.js';
6
+ import { ComponentManifestSchema } from '../schemas/component-manifest.schema.js';
7
+ import { EvidenceError } from '../../../lib/EvidenceError.js';
8
+
9
+ /**
10
+ * @param {string} packagePath
11
+ *
12
+ * @returns {Promise<string[]>}
13
+ */
14
+ export async function manifestLoader(packagePath) {
15
+ try {
16
+ // Load Config
17
+ const manifestContent = await fs
18
+ .readFile(path.resolve(packagePath, 'evidence.manifest.yaml'))
19
+ .then((r) => r.toString());
20
+ // Parse YAML, then validate data
21
+ const manifest = yaml.parse(manifestContent);
22
+ const parsedManifest = ComponentManifestSchema.safeParse(manifest);
23
+
24
+ if (!parsedManifest.success) {
25
+ // Shape is wrong
26
+ console.error(chalk.bold.red(`[!] evidence.manifest.yaml has errors`));
27
+ const formattedError = cleanZodErrors(parsedManifest.error.format());
28
+ console.error(chalk.red('| Discovered Errors:'));
29
+ const redPipe = chalk.red('|');
30
+ console.error(
31
+ `${redPipe} ${yaml.stringify(formattedError).replace(/\n/g, `\n${redPipe} `)}`
32
+ );
33
+
34
+ throw new EvidenceError('Evidence Component Manifest is Malformed', [
35
+ 'File location:',
36
+ packagePath
37
+ ]);
38
+ }
39
+
40
+ return parsedManifest.data.components;
41
+ } catch (e) {
42
+ if (!(e instanceof Error)) throw e;
43
+ if (e.message === 'ENOENT') {
44
+ console.error(
45
+ chalk.red.bold(
46
+ `[!] evidence.manifest.yaml file not found in ${packagePath}.\n This is probably a bug in Evidence; please file a report at https://github.com/evidence-dev/evidence/issues/new?assignees=&labels=bug%2C+to-review&projects=&template=bug_report.md`
47
+ )
48
+ );
49
+ throw e;
50
+ }
51
+ throw e;
52
+ }
53
+ }
@@ -0,0 +1,5 @@
1
+ import { z } from 'zod';
2
+
3
+ export const ComponentManifestSchema = z.object({
4
+ components: z.array(z.string())
5
+ });
@@ -0,0 +1,21 @@
1
+ declare interface PluginComponent {
2
+ /**
3
+ * Name of originating package
4
+ */
5
+ package: string;
6
+ /**
7
+ * Name of exported component from package
8
+ */
9
+ aliasOf?: string;
10
+
11
+ /**
12
+ * If this component is overridden, contains related metadata
13
+ */
14
+ overriden?: PluginComponent;
15
+ }
16
+ /**
17
+ * Map of component names to their metadata
18
+ */
19
+ declare interface PluginComponents {
20
+ [componentName: string]: PluginComponent;
21
+ }
@@ -0,0 +1,30 @@
1
+ import { EvidenceError } from '../../lib/EvidenceError.js';
2
+
3
+ /**
4
+ * @param {import("./loadComponentPlugins.js").ComponentInfo[]} plugins
5
+ * @returns {true}
6
+ */
7
+ export const validateOverrides = (plugins) => {
8
+ /** @type {Set<string>} */
9
+ const overrides = new Set();
10
+
11
+ /** @type {Record<string, string[]>} */
12
+ const duplicates = {};
13
+ for (const plugin of plugins) {
14
+ for (const override of plugin.options.overrides) {
15
+ if (overrides.has(override)) {
16
+ if (!duplicates[override]) duplicates[override] = [];
17
+ duplicates[override].push(plugin.name);
18
+ }
19
+ overrides.add(override);
20
+ }
21
+ }
22
+
23
+ if (Object.keys(duplicates).length) {
24
+ throw new EvidenceError(
25
+ 'Invalid component plugin configuration; some components are overriden more than once',
26
+ ...Object.entries(duplicates).map(([k, v]) => `${k}: Overridden by ${v.join(', ')}`)
27
+ );
28
+ }
29
+ return true;
30
+ };
@@ -0,0 +1,32 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { validateOverrides } from './validateOverrides.js';
3
+
4
+ describe('validateOverrides', () => {
5
+ it('Should return true when given no components', () => {
6
+ expect(validateOverrides([])).toBe(true);
7
+ });
8
+ it('Should return true when given components with no overrides', () => {
9
+ expect(
10
+ validateOverrides([
11
+ { name: 'plugin1', options: { overrides: [] } },
12
+ { name: 'plugin2', options: { overrides: [] } }
13
+ ])
14
+ ).toBe(true);
15
+ });
16
+ it('Should return true when given components with non-conflicting overrides', () => {
17
+ expect(
18
+ validateOverrides([
19
+ { name: 'plugin1', options: { overrides: ['a'] } },
20
+ { name: 'plugin2', options: { overrides: ['b'] } }
21
+ ])
22
+ ).toBe(true);
23
+ });
24
+ it('Should throw true when given components with conflicting overrides', () => {
25
+ expect(() =>
26
+ validateOverrides([
27
+ { name: 'plugin1', options: { overrides: ['a'] } },
28
+ { name: 'plugin2', options: { overrides: ['a'] } }
29
+ ])
30
+ ).toThrowError();
31
+ });
32
+ });
@@ -0,0 +1,85 @@
1
+ /** @typedef {import("../schemas/plugin-package.schema.js").DatasourcePackage} DatasourcePackage */
2
+ /** @typedef {import("./schemas/datasourcePlugin.schema.js").Datasource} Datasource */
3
+
4
+ import { EvidenceError } from '../../lib/EvidenceError.js';
5
+
6
+ /** @typedef {[DatasourcePackage, Datasource]} DatasourceTuple */
7
+ export class Datasources {
8
+ /** @type {Record<string, DatasourceTuple>} */
9
+ #byPackage = {};
10
+
11
+ get byPackage() {
12
+ return this.#byPackage;
13
+ }
14
+
15
+ /** @type {Record<string, DatasourceTuple>} */
16
+ #bySource = {};
17
+
18
+ get bySource() {
19
+ return this.#bySource;
20
+ }
21
+
22
+ /**
23
+ * Forced map of source name to package name
24
+ * @type {Record<string,string>}
25
+ */
26
+ #overrides = {};
27
+
28
+ /**
29
+ * @param {DatasourcePackage} pack
30
+ * @param {Datasource} source
31
+ * @param {string[]} [overrides]
32
+ */
33
+ add(pack, source, overrides) {
34
+ if (!pack.evidence.datasources?.length)
35
+ throw new EvidenceError(
36
+ `${pack.name} is not a valid datasource!`,
37
+ 'package.json is missing evidence.datasources array'
38
+ );
39
+
40
+ overrides?.forEach((override) => {
41
+ if (Object.keys(this.#overrides).includes(override))
42
+ throw new EvidenceError(
43
+ `${override} has already been overriden by ${this.#bySource[override]}`
44
+ );
45
+ this.#overrides[override] = pack.name;
46
+ this.#bySource[override] = [pack, source];
47
+ });
48
+
49
+ pack.evidence.datasources.flat().forEach((sourceName) => {
50
+ if (this.#overrides[sourceName] && this.#overrides[sourceName] !== pack.name) {
51
+ console.debug(
52
+ `Ignoring source type "${sourceName}" from ${pack.name}, already overriden by ${this.#overrides[sourceName]}`
53
+ );
54
+ }
55
+ this.#bySource[sourceName] = [pack, source];
56
+ });
57
+
58
+ this.#byPackage[pack.name] = [pack, source];
59
+ }
60
+
61
+ /** @param {string} packageName */
62
+ getByPackageName(packageName) {
63
+ return this.#byPackage[packageName];
64
+ }
65
+ /** @param {string} sourceName */
66
+ getBySource(sourceName) {
67
+ return this.#bySource[sourceName];
68
+ }
69
+
70
+ /** @returns {string[]} */
71
+ getCanonicalSources() {
72
+ return Object.values(this.#byPackage)
73
+ .map(([pack]) => {
74
+ if (!pack.evidence.datasources) {
75
+ throw new EvidenceError(
76
+ 'Non-Datasource plugin was loaded as a plugin. (This is an Evidence Error)'
77
+ );
78
+ }
79
+ return pack.evidence.datasources.map((source) =>
80
+ Array.isArray(source) ? source[0] : source
81
+ );
82
+ })
83
+ .flat();
84
+ }
85
+ }