@yolean/evidence-sdk 0.0.0-stage → 4.2.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 +544 -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 +18 -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 +74 -0
  181. package/src/utils/svelte/addBasePath.spec.js +108 -0
  182. package/src/utils/svelte/index.js +13 -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,106 @@
1
+ import { cp, mkdir, readdir, rm } from 'fs/promises';
2
+ import { loadLayoutPlugin } from './loadLayoutPlugin.js';
3
+ import path from 'path';
4
+ import { EvidenceError } from '../../lib/EvidenceError.js';
5
+ import { copyMethods } from './copyMethods/index.js';
6
+ import { forceLink } from '../../lib/forceLink.js';
7
+ import { projectRoot } from '../../lib/projectRoot.js';
8
+ import chalk from 'chalk';
9
+
10
+ /**
11
+ * @returns {Promise<boolean>} If a layout was found and applied.
12
+ */
13
+ export const copyToLayout = async () => {
14
+ const plugin = await loadLayoutPlugin();
15
+ if (!plugin) return false;
16
+
17
+ const templateDir = path.join(projectRoot, '.evidence', 'template');
18
+ await mkdir(templateDir, { recursive: true });
19
+
20
+ // Copy the template over
21
+ const pluginRoot = plugin.evidence.layout.root
22
+ ? path.join(plugin.dir, plugin.evidence.layout.root)
23
+ : plugin.dir;
24
+
25
+ // Get a fresh copy of the template. TODO: This might only be needed in dev mode
26
+ await rm(templateDir, { recursive: true });
27
+ await cp(pluginRoot, templateDir, {
28
+ recursive: true,
29
+ dereference: true,
30
+ filter: (from) => !from.endsWith('node_modules')
31
+ });
32
+
33
+ // TODO: Check to see if this is valid on windows, might need to copy there
34
+ // We use a link here to try and cut down on disk space usage (node modules can really blow up)
35
+
36
+ // Link plugin's node_modules to the template's node_modules
37
+ await forceLink(
38
+ path.join(plugin.dir, 'node_modules'),
39
+ path.join(templateDir, 'node_modules')
40
+ ).catch((e) => {
41
+ console.debug(
42
+ chalk.dim.yellow(
43
+ 'Failed to link template modules to .evidence/template/node_modules; this is expected with certain package managers'
44
+ )
45
+ );
46
+ console.debug(chalk.dim.yellow(e));
47
+ });
48
+
49
+ // TODO: Is this a hack?
50
+ // Virtual modules import from the context of the _project_, not the _package_. That approach may need revisiting
51
+ // to scope it to more specific things. Vite will climb the project structure (starting in .evidence/template/node_modules) to look
52
+ // for missing deps. In this case, anything that isn't provided by the template will fall back to what the sdk provides.
53
+ await forceLink(
54
+ path.join(projectRoot, 'node_modules', '@evidence-dev', 'sdk', 'node_modules'),
55
+ path.join('.evidence', 'node_modules')
56
+ ).catch((e) => {
57
+ console.debug(
58
+ chalk.dim.yellow(
59
+ 'Failed to link sdk modules to .evidence/node_modules; this is expected with certain package managers; this is expected with certain package managers'
60
+ )
61
+ );
62
+ console.debug(chalk.dim.yellow(e));
63
+ });
64
+
65
+ const pwd = await readdir(projectRoot, { withFileTypes: true });
66
+ if (!pwd.some((item) => item.name === 'pages' && item.isDirectory())) {
67
+ throw new EvidenceError('Project does not contain a `pages` directory, please create one');
68
+ }
69
+
70
+ // Copy the routes into the template
71
+ const splitPluginPath = plugin.evidence.layout.routes.destination.split(/[/\\]/);
72
+ const rootTargetDir = path.join('.evidence', 'template', ...splitPluginPath);
73
+ await copyMethods[plugin.evidence.layout.routes.style].copyAll('pages', rootTargetDir);
74
+ // TODO: Apply changes to svelte config to allow markdown files
75
+
76
+ console.debug(
77
+ chalk.dim(
78
+ `Using "${plugin.evidence.layout.routes.style}" style to move pages to ${rootTargetDir}`
79
+ )
80
+ );
81
+
82
+ const componentsTargetDir = path.join(
83
+ '.evidence',
84
+ 'template',
85
+ ...plugin.evidence.layout.components.destination.split(/[/\\]/)
86
+ );
87
+
88
+ if (pwd.some((item) => item.name === 'components' && item.isDirectory())) {
89
+ await mkdir(componentsTargetDir, { recursive: true });
90
+ await cp(path.join(projectRoot, 'components'), componentsTargetDir, { recursive: true });
91
+ }
92
+
93
+ const staticTargetDir = path.join(
94
+ '.evidence',
95
+ 'template',
96
+ ...plugin.evidence.layout.static.destination.split(/[/\\]/)
97
+ );
98
+ if (pwd.some((item) => item.name === 'static' && item.isDirectory())) {
99
+ await mkdir(staticTargetDir, { recursive: true });
100
+ await cp(path.join(projectRoot, 'static'), staticTargetDir, { recursive: true });
101
+ }
102
+
103
+ // TODO: Merge changes to svelte configuration, tailwind configuration, and possibly vite configuration
104
+
105
+ return true;
106
+ };
@@ -0,0 +1,23 @@
1
+ import { getEvidenceConfig } from '../../configuration/getEvidenceConfig.js';
2
+ import { EvidenceError } from '../../lib/EvidenceError.js';
3
+ import { loadPluginPackage } from '../loadPluginPackage.js';
4
+ import { isLayoutPlugin } from '../schemas/plugin-package.schema.js';
5
+
6
+ /**
7
+ *
8
+ * @returns {Promise<null | import("../schemas/plugin-package.schema.js").LayoutPackage & {dir: string}>}
9
+ */
10
+ export const loadLayoutPlugin = async () => {
11
+ const { layout } = getEvidenceConfig();
12
+
13
+ if (!layout) return null;
14
+ const pack = await loadPluginPackage(layout);
15
+
16
+ if (!pack || !isLayoutPlugin(pack)) {
17
+ throw new EvidenceError(
18
+ `${layout} was specified as a layout, but is not a valid layout plugin`
19
+ );
20
+ }
21
+
22
+ return pack;
23
+ };
@@ -0,0 +1,77 @@
1
+ import chalk from 'chalk';
2
+ import { PluginPackageSchema } from './schemas/plugin-package.schema.js';
3
+ import fs from 'fs/promises';
4
+ import { createRequire } from 'module';
5
+ import path from 'path';
6
+ import { log } from '../logger/index.js';
7
+
8
+ /**
9
+ *
10
+ * @param {string} name
11
+ * @returns {Promise<null | import("./schemas/plugin-package.schema.js").PluginPackage & {dir: string}>}
12
+ */
13
+ export const loadPluginPackage = async (name) => {
14
+ let pluginPackageDirectory = path.dirname(createRequire(import.meta.url).resolve(name));
15
+ let pluginPackageDirContents = await fs.readdir(pluginPackageDirectory);
16
+ while (!pluginPackageDirContents.includes('package.json')) {
17
+ pluginPackageDirectory = path.dirname(pluginPackageDirectory);
18
+ pluginPackageDirContents = await fs.readdir(pluginPackageDirectory);
19
+
20
+ if (pluginPackageDirectory === path.dirname(pluginPackageDirectory)) {
21
+ // reached root
22
+ log.warn(
23
+ chalk.yellow(
24
+ `Package ${chalk.bold(
25
+ `"${name}"`
26
+ )} not found, run again with --debug for more information`
27
+ )
28
+ );
29
+ return null;
30
+ }
31
+ }
32
+
33
+ const packagePath = path.join(pluginPackageDirectory, 'package.json');
34
+ log.debug(
35
+ chalk.green(`Loading plugin package ${chalk.bold(`"${name}"`)} from ${chalk.bold(packagePath)}`)
36
+ );
37
+
38
+ const packageContent = JSON.parse(await fs.readFile(packagePath, 'utf-8'));
39
+ const pack = PluginPackageSchema.safeParse(packageContent);
40
+
41
+ if (!pack.success) {
42
+ // Invalid Plugin
43
+ console.warn(
44
+ chalk.yellow(
45
+ `Datasource package ${chalk.bold(
46
+ `"${name}"`
47
+ )} is an invalid Evidence plugin, run again with --debug for more information`
48
+ )
49
+ );
50
+ console.debug(pack.error.format());
51
+ console.debug(JSON.stringify(packageContent, null, 1));
52
+ return null;
53
+ }
54
+
55
+ if (!pack.data.main) {
56
+ if (packageContent.svelte) pack.data.main = packageContent.svelte.toString();
57
+ else if (packageContent.exports) {
58
+ switch (typeof packageContent.exports) {
59
+ case 'string':
60
+ pack.data.main = packageContent.exports;
61
+ break;
62
+ case 'object': {
63
+ if ('.' in packageContent.exports) {
64
+ const e = packageContent.exports['.'];
65
+ if (typeof e === 'string') pack.data.main = e;
66
+ else pack.data.main = e.module ?? e.svelte;
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+
73
+ return {
74
+ ...pack.data,
75
+ dir: path.parse(packagePath).dir
76
+ };
77
+ };
@@ -0,0 +1 @@
1
+ export * from './plugin-package.schema.js';
@@ -0,0 +1,41 @@
1
+ import { z } from 'zod';
2
+
3
+ /** @typedef {z.infer<typeof PluginConfigSchema>} PluginConfig */
4
+ export const PluginConfigSchema = z.object({
5
+ components: z
6
+ .record(
7
+ z.string(),
8
+ z.object({
9
+ overrides: z
10
+ .array(z.string(), { description: 'Components that will always come from this package.' })
11
+ .default([]),
12
+ aliases: z
13
+ .record(
14
+ z.string({ description: 'Component Name' }),
15
+ z.string({ description: 'Alias to apply' })
16
+ )
17
+ .default({}),
18
+
19
+ provides: z
20
+ .array(z.string(), {
21
+ description:
22
+ 'Manual list of components exported by this plugin, will override any internal plugin configuration'
23
+ })
24
+ .default([])
25
+ })
26
+ )
27
+ .optional(),
28
+ datasources: z
29
+ .record(
30
+ z.string(),
31
+ z.object({
32
+ overrides: z
33
+ .array(z.string(), {
34
+ description:
35
+ 'The specified datasources will always use this plugin. Can only be specified once per source type'
36
+ })
37
+ .optional()
38
+ })
39
+ )
40
+ .optional()
41
+ });
@@ -0,0 +1,21 @@
1
+ import { it, describe, expect } from 'vitest';
2
+ import { PluginConfigSchema } from './plugin-config.schema.js';
3
+
4
+ describe('PluginConfigSchema', () => {
5
+ it('should pass on an empty object configuration', () => {
6
+ const v = {};
7
+ expect(() => PluginConfigSchema.parse(v)).not.toThrowError();
8
+ });
9
+ it('should pass when only a layout is provided', () => {
10
+ const v = { layout: '' };
11
+ expect(() => PluginConfigSchema.parse(v)).not.toThrowError();
12
+ });
13
+ it('should pass when only components are provided', () => {
14
+ const v = { components: {} };
15
+ expect(() => PluginConfigSchema.parse(v)).not.toThrowError();
16
+ });
17
+ it('should pass when only sources are provided', () => {
18
+ const v = { sources: {} };
19
+ expect(() => PluginConfigSchema.parse(v)).not.toThrowError();
20
+ });
21
+ });
@@ -0,0 +1,154 @@
1
+ import { z } from 'zod';
2
+ import * as evidenceIcons from '@evidence-dev/icons';
3
+ import * as simpler from '@steeze-ui/simple-icons';
4
+ import * as tabler from '@steeze-ui/tabler-icons';
5
+ import { copyMethods } from '../layouts/copyMethods/index.js';
6
+ import path from 'path';
7
+
8
+ const iconKeys = /** @type {[string, ...string[]]} */ ([
9
+ ...Object.keys(simpler),
10
+ ...Object.keys(tabler),
11
+ ...Object.keys(evidenceIcons)
12
+ ]);
13
+
14
+ /*
15
+ Each type of plugin is defined as a schema of what it expects in the .evidence field
16
+
17
+ They are then exported as types and a single merged schema with helper narrowing functions
18
+
19
+ e.g.
20
+
21
+ // ...load package
22
+ const validPluginPackage = PluginPackageSchema.parse(packageContnt)
23
+ // We now know that it is a valid plugin; but we can further narrow to what kind
24
+
25
+ if (isLayoutPlugin(validPluginPackage)) {
26
+ // it is a layout plugin
27
+ if (isDatasourcePlugin(validPluginPackage)) {
28
+ // it is a layout plugin AND a datasource plugin
29
+ }
30
+ }
31
+ if (isComponentPlugin) {
32
+ // it may or may not be a layout or datasource plugin, but it is a component plugin
33
+ }
34
+ */
35
+
36
+ const BasePluginPackageSchema = z.object({
37
+ name: z.string(),
38
+ version: z.string(),
39
+ evidence: z.object({})
40
+ });
41
+
42
+ const LayoutPluginSchema = BasePluginPackageSchema.extend({
43
+ evidence: z.object({
44
+ layout: z.object({
45
+ routes: z.object({
46
+ destination: z
47
+ .string({ description: 'Determines where files will be placed into the template' })
48
+ .default(path.join('src', 'routes')),
49
+ style: z.enum(/** @type {[string, ...string[]]} */ (Object.keys(copyMethods)), {
50
+ description: 'Determines how filenames will be transformed into the template'
51
+ })
52
+ }),
53
+ components: z
54
+ .object({
55
+ destination: z.string().default(path.join('src', 'components'))
56
+ })
57
+ .default({}),
58
+ static: z
59
+ .object({
60
+ destination: z.string().default(path.join('static'))
61
+ })
62
+ .default({}),
63
+ root: z.string().optional()
64
+ })
65
+ })
66
+ });
67
+
68
+ const DatasourcePluginSchema = BasePluginPackageSchema.extend({
69
+ evidence: z.object({
70
+ datasources: z.array(
71
+ z.union([
72
+ z.string({ description: 'Name of a supported database, this value is used in the UI' }),
73
+ z.array(z.string(), {
74
+ description:
75
+ 'Group of names that are aliases of the same database. First value is used in UI'
76
+ })
77
+ ]),
78
+ {
79
+ description: 'List of database names that this plugin supports'
80
+ }
81
+ ),
82
+ icon: z
83
+ .enum(iconKeys, {
84
+ errorMap: (e) => {
85
+ if (e.code === 'invalid_enum_value')
86
+ return {
87
+ message:
88
+ "'An invalid icon name was provided, only simple-icons and tabler-icons are valid'"
89
+ };
90
+ return {
91
+ ...e,
92
+ message: e.message ?? ''
93
+ };
94
+ }
95
+ })
96
+ .optional()
97
+ }),
98
+ main: z.string()
99
+ });
100
+
101
+ const ComponentPluginSchema = BasePluginPackageSchema.extend({
102
+ evidence: z.object({
103
+ components: z
104
+ .boolean()
105
+ .or(z.string({ description: 'Relative path to the file exporting components' })) // This is not default-false because the key may not exist, and we want to preserve that behavior
106
+ }),
107
+ main: z.string()
108
+ });
109
+
110
+ /** @typedef {z.infer<typeof LayoutPluginSchema>} LayoutPackage */
111
+ /** @typedef {z.infer<typeof DatasourcePluginSchema>} DatasourcePackage */
112
+ /** @typedef {z.infer<typeof ComponentPluginSchema>} ComponentPackage */
113
+
114
+ /** @typedef {z.infer<typeof PluginPackageSchema>} PluginPackage */
115
+ export const PluginPackageSchema = BasePluginPackageSchema
116
+ // Merge fields that are not in 'evidence' (e.g. main)
117
+ .merge(LayoutPluginSchema.partial())
118
+ .merge(DatasourcePluginSchema.partial())
119
+ .merge(ComponentPluginSchema.partial())
120
+ .extend({
121
+ // Merge 'evidence' fields (this just get overwritten above)
122
+ evidence: LayoutPluginSchema.shape.evidence
123
+ .merge(DatasourcePluginSchema.shape.evidence)
124
+ .merge(ComponentPluginSchema.shape.evidence)
125
+ .partial()
126
+ });
127
+
128
+ /**
129
+ * Type refinement function to check if a package is a layout plugin
130
+ * @param {PluginPackage} v
131
+ * @returns {v is LayoutPackage}
132
+ */
133
+ export const isLayoutPlugin = (v) => {
134
+ const result = LayoutPluginSchema.safeParse(v);
135
+ return result.success;
136
+ };
137
+ /**
138
+ * Type refinement function to check if a package is a datasource plugin
139
+ * @param {PluginPackage} v
140
+ * @returns {v is DatasourcePackage}
141
+ */
142
+ export const isDatasourcePlugin = (v) => {
143
+ const result = DatasourcePluginSchema.safeParse(v);
144
+ return result.success;
145
+ };
146
+ /**
147
+ * Type refinement function to check if a package is a datasource plugin
148
+ * @param {PluginPackage} v
149
+ * @returns {v is ComponentPackage}
150
+ */
151
+ export const isComponentPlugin = (v) => {
152
+ const result = ComponentPluginSchema.safeParse(v);
153
+ return result.success;
154
+ };
@@ -0,0 +1,30 @@
1
+ import { it, describe, expect } from 'vitest';
2
+ import { PluginPackageSchema } from './plugin-package.schema.js';
3
+
4
+ describe('PluginPackageSchema', () => {
5
+ it('should fail when an invalid icon is given', () => {
6
+ const v = {
7
+ name: 'some-package',
8
+ main: 'index.js',
9
+ version: '0.0.1',
10
+ license: 'MIT',
11
+ evidence: {
12
+ icon: 'Invalid Icon'
13
+ }
14
+ };
15
+ expect(() => PluginPackageSchema.parse(v)).toThrowError('An invalid icon name was provided');
16
+ });
17
+ it('should pass when a valid icon is given', () => {
18
+ /** @type {import("zod").infer<typeof PluginPackageSchema>} */
19
+ const v = {
20
+ name: 'some-package',
21
+ main: 'index.js',
22
+ version: '0.0.1',
23
+ license: 'MIT',
24
+ evidence: {
25
+ icon: 'Postgresql'
26
+ }
27
+ };
28
+ expect(() => PluginPackageSchema.parse(v)).not.toThrowError();
29
+ });
30
+ });
@@ -0,0 +1 @@
1
+ export * from '';
@@ -0,0 +1 @@
1
+ export * from '@uwdata/mosaic-sql';
@@ -0,0 +1,35 @@
1
+ export type DescribeResultRow = {
2
+ column_name: string;
3
+ column_type: DuckDBColumnType;
4
+ nullable: 'YES' | 'NO';
5
+ };
6
+
7
+ export type DuckDBColumnType =
8
+ | `STRUCT(${string})`
9
+ | `${string}[]`
10
+ | 'BIGINT'
11
+ | 'BIT'
12
+ | 'BOOLEAN'
13
+ | 'BLOB'
14
+ | 'DATE'
15
+ | 'DOUBLE'
16
+ | `DECIMAL(${number},${number})`
17
+ | 'HUGEINT'
18
+ | 'INTEGER'
19
+ | 'INTERVAL'
20
+ | 'FLOAT'
21
+ | 'SMALLINT'
22
+ | 'TIME'
23
+ | 'TIMESTAMP'
24
+ | 'TIMESTAMP_S'
25
+ | 'TIMESTAMP_MS'
26
+ | 'TIMESTAMP_NS'
27
+ | 'TIME WITH TIME ZONE'
28
+ | 'TIMESTAMP WITH TIME ZONE'
29
+ | 'TINYINT'
30
+ | 'UBIGINT'
31
+ | 'UINTEGER'
32
+ | 'USMALLINT'
33
+ | 'UTINYINT'
34
+ | 'UUID'
35
+ | 'VARCHAR';
@@ -0,0 +1,13 @@
1
+ declare global {
2
+ namespace svelteHTML {
3
+ // enhance elements
4
+ interface HTMLMetaAttributes {
5
+ 'evidence-query-presence'?: string;
6
+ 'evidence-query-content'?: string;
7
+ }
8
+
9
+ interface HTMLAttributes<T> {
10
+ 'evidence-query-name'?: string;
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,18 @@
1
+ // Needed to include the virtual type defs in the output
2
+ /// <reference path="./virtuals.d.ts"/>
3
+ /// <reference path="./sveltekit-autoimport.d.ts"/>
4
+ /// <reference path="./mosaic-sql.d.ts"/>
5
+ /// <reference path="./html-attributes.ts" />
6
+
7
+ export type FileMetadata = {
8
+ queries: Record<string, string>;
9
+ };
10
+
11
+ declare global {
12
+ interface Window {
13
+ __evidence_ssr?: Record<
14
+ string,
15
+ { data: unknown[]; initialQuery: string; columns: { name: string; evidenceType: string } }
16
+ >;
17
+ }
18
+ }
@@ -0,0 +1,27 @@
1
+ declare module '@uwdata/mosaic-sql' {
2
+ type BuilderFunction = <T extends any[] = unknown[]>(...args: T) => Query;
3
+
4
+ class Query {
5
+ distinct: BuilderFunction;
6
+ from: BuilderFunction;
7
+ select: BuilderFunction;
8
+ $select: BuilderFunction;
9
+ $groupby: BuilderFunction;
10
+
11
+ offset: BuilderFunction<[number]>;
12
+ limit: BuilderFunction<[string]>;
13
+ orderby: BuilderFunction;
14
+ where: BuilderFunction;
15
+ clone: BuilderFunction<[]>;
16
+ with: BuilderFunction;
17
+ }
18
+
19
+ const desc = CallableFunction;
20
+ const sql = CallableFunction;
21
+ const count = CallableFunction;
22
+ const sum = CallableFunction;
23
+ const avg = CallableFunction;
24
+ const min = CallableFunction;
25
+ const max = CallableFunction;
26
+ const median = CallableFunction;
27
+ }
@@ -0,0 +1,13 @@
1
+ declare module 'sveltekit-autoimport' {
2
+ import { MarkupPreprocessor } from 'svelte/types/compiler/preprocess/types';
3
+
4
+ type AutoImportArgs = {
5
+ include?: string[];
6
+ exclude?: string[];
7
+ module?: Record<string, string[]>;
8
+ mapping?: unknown;
9
+ components?: (string | { directory: string; flat?: boolean; prefix?: string })[];
10
+ };
11
+
12
+ export default function (args: AutoImportArgs): { markup: MarkupPreprocessor };
13
+ }
@@ -0,0 +1,113 @@
1
+ declare namespace Evidence {
2
+ interface ConfigLayoutOptions {}
3
+ }
4
+
5
+ declare module '$evidence/build' {
6
+ export const BUILD_ID: string;
7
+ export const BUILD_DATE: Date;
8
+ }
9
+
10
+ declare module '$evidence/bootstrapContexts' {
11
+ const fn: () => void;
12
+ export default fn;
13
+
14
+ export const asObj: () => Record<string, string>;
15
+ }
16
+
17
+ declare module '$evidence/contextKeys' {
18
+ export const QUERIES_CONTEXT_KEY = '__EVIDENCE_CONTEXTUAL_QUERIES';
19
+ export const ALL_QUERIES_CONTEXT_KEY = '__EVIDENCE_ALL_QUERIES';
20
+ export const ALL_QUERIES_MUTS_CONTEXT_KEY = '__EVIDENCE_ALL_QUERIES_MUTS';
21
+ }
22
+
23
+ declare module '$evidence/initUsql' {
24
+ const initUsqlPromise: Promise<void>;
25
+ export default initUsqlPromise;
26
+ }
27
+
28
+ declare module '$evidence/ssrHook.svelte.js' {
29
+ /**
30
+ * SvelteKit server hook to enable SSR
31
+ * @param {{name: string, queryString: string}[]} presentQueries
32
+ * @returns {({html: string, done: boolean}) => Promise<string>}
33
+ */
34
+ export const ssrHook: (
35
+ presentQueries: { name: string; queryString: string }[]
36
+ ) => (event: { html: string; done: boolean }) => Promise<string>;
37
+ }
38
+
39
+ declare module '$evidence/proxyStack' {
40
+ export const ProxyStack: () => {
41
+ value: Object;
42
+ push: (v: Object) => string;
43
+ rm: (id: string) => void;
44
+ };
45
+ }
46
+
47
+ declare module '$evidence/queries' {
48
+ type QueryResultRow = import('@evidence-dev/sdk/query-store').QueryResultRow;
49
+
50
+ export const setSsrHookState: (installedState: boolean) => void;
51
+
52
+ interface GetQueryResult {
53
+ [key: string]: import('@evidence-dev/sdk/query-store').Query;
54
+ }
55
+
56
+ export const runQuery: <T extends QueryResultRow = QueryResult>(
57
+ name: string,
58
+ sql: string,
59
+ opts?: import('@evidence-dev/sdk/query-store').QueryOpts
60
+ ) => import('@evidence-dev/sdk/query-store').QueryValue;
61
+ export const getQueries: () => import('svelte/store').Readable<GetQueryResult>;
62
+ }
63
+
64
+ declare module '$evidence/updateQueryContext' {
65
+ type UpdateQueryContext = () => (layerQueries: Record<string, string>) => void;
66
+ const fn: UpdateQueryContext;
67
+ export default fn;
68
+ }
69
+
70
+ declare module '$evidence/static-assets' {
71
+ /**
72
+ * @returns Promise when running in browser, string when running in node
73
+ */
74
+ export const getManifest: () => string | Promise<string>;
75
+
76
+ export const writeInitialQueryResult: (
77
+ queryName: string,
78
+ routeKey: string,
79
+ results: Array<Object>
80
+ ) => void;
81
+ export const getInitialQueryResult: (queryName: string, routeKey: string) => Array<Object> | null;
82
+ }
83
+
84
+ declare module '$evidence/QuerySSR.svelte' {
85
+ // I copied this from a packaged Sveltekit library and adapted it, much of this may not be required
86
+ import { SvelteComponentTyped } from 'svelte';
87
+ export default class QuerySSR extends SvelteComponentTyped<
88
+ {
89
+ queries: import('@evidence-dev/sdk/query-store').Query[];
90
+ },
91
+ {
92
+ [evt: string]: CustomEvent<any>;
93
+ },
94
+ {
95
+ default: {};
96
+ }
97
+ > {}
98
+ export {};
99
+ }
100
+
101
+ declare module '$evidence/projectPaths' {
102
+ export const URL_PREFIX = '_evidence' as const;
103
+
104
+ export const inTemplate: boolean;
105
+
106
+ export const evidenceDirectory: '..' | '.evidence';
107
+
108
+ export const dataDirectory: string;
109
+
110
+ export const metaDirectory: string;
111
+
112
+ export const sourcesDirectory: string;
113
+ }