@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,77 @@
1
+ import { checkImport } from '../../../../lib/check-import.js';
2
+ import { getFileMetadata } from '../../metadatas.js';
3
+
4
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
5
+ export const injectQueries = {
6
+ script: ({ filename, content, attributes }) => {
7
+ if (!filename) return;
8
+ if (attributes.context === 'module') return;
9
+ /** @type {import('../../metadatas.js').FileMetadata} */
10
+ const meta = getFileMetadata(filename) ?? { queries: {} };
11
+
12
+ const fileQueries = meta?.queries ?? {};
13
+ const queryNames = Object.keys(fileQueries);
14
+ // There are no queries on this page; so we don't need to do anything.
15
+ if (!queryNames.length) return;
16
+
17
+ // TODO: Consider using https://unjs.io/packages/knitwork
18
+
19
+ const initialDeclarations = queryNames
20
+ .map((queryName) => `let __evidence_query_text_${queryName} = ''`)
21
+ .join('\n');
22
+
23
+ const queryTextAssignments = Object.entries(fileQueries).map(
24
+ ([queryName, queryText]) => `__evidence_query_text_${queryName} = \`${queryText}\``
25
+ );
26
+
27
+ const queryEffects = queryTextAssignments.map((a) => `$: ${a}`).join('\n');
28
+ const queryMountFn = `onMount(() => {${queryTextAssignments.join(';')}})`;
29
+
30
+ // TODO: Do we need to move imports to the top of the file wrt user content?
31
+ const queryDeclarations = `
32
+ ${
33
+ checkImport('getQueries', '$evidence/queries', content)
34
+ ? ''
35
+ : `import {getQueries} from "$evidence/queries"`
36
+ }
37
+ const queries = getQueries()
38
+ let {${queryNames.map((k) => `${k}: __evidence_query_${k}`).join(',')}} = $queries
39
+ $: ({${queryNames.map((k) => `${k}: __evidence_query_${k}`).join(',')}} = $queries)
40
+
41
+ ${queryNames.map((k) => `let ${k} = $__evidence_query_${k}`).join('\n')}
42
+ ${queryNames.map((k) => `$: ${k} = $__evidence_query_${k}`).join('\n')}
43
+
44
+ // Layout queries cannot be accessed directly; and must use query.
45
+ // We unwrap these to the value to make sure that proxied values are used
46
+ let query = Object.fromEntries(Object.entries($queries).map(([k,v]) => [k, v.value]))
47
+ $: query = Object.fromEntries(Object.entries($queries).map(([k,v]) => [k, v.value]))
48
+ `;
49
+
50
+ const adjusted = `
51
+ /******** Preprocessed Code ********/
52
+ ${
53
+ checkImport('updateQueryContext', '$evidence/updateQueryContext.svelte.js', content)
54
+ ? ''
55
+ : `import updateQueryContext from "$evidence/updateQueryContext.svelte.js"`
56
+ }
57
+ ${checkImport('onMount', 'svelte', content) ? '' : `import {onMount} from "svelte"`}
58
+ ${queryMountFn}
59
+ ${initialDeclarations}
60
+
61
+ $: __evidence_page_query_texts = {${queryNames
62
+ .map((queryName) => `${queryName}: __evidence_query_text_${queryName}`)
63
+ .join(', ')}}
64
+
65
+ const updateContext = updateQueryContext()
66
+ $: updateContext(__evidence_page_query_texts)
67
+ ${filename?.endsWith('.md') ? queryDeclarations : ''}
68
+
69
+ /******** User / Component Code ********/
70
+ ${content}
71
+
72
+ /******** Preprocessed Code ********/
73
+ ${queryEffects}
74
+ `;
75
+ return { code: adjusted };
76
+ }
77
+ };
@@ -0,0 +1,56 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ /**
5
+ * @param {string} filename
6
+ * @returns {boolean}
7
+ */
8
+ const hasPrerenderExport = (filename) => {
9
+ const content = fs.readFileSync(filename, 'utf-8');
10
+ return content.includes('export const prerender');
11
+ };
12
+
13
+ /**
14
+ * @type {() => void}
15
+ */
16
+ export const markRootLayoutPrerenderable = () => {
17
+ const src = fs.readdirSync('src');
18
+
19
+ if (src.includes('routes')) {
20
+ // SvelteKit app
21
+ const routes = fs.readdirSync(path.join('src', 'routes'));
22
+ let layoutPath;
23
+ // in order of preference
24
+ const layoutPaths = ['+layout.js', '+layout.server.js', '+layout.ts', '+layout.server.ts'];
25
+ for (const layout of layoutPaths) {
26
+ if (routes.includes(layout)) {
27
+ // Check for a prerender output
28
+ if (hasPrerenderExport(path.join('src', 'routes', layout))) {
29
+ // The user has already added a prerender export; we shouldn't override it.
30
+ return;
31
+ } else {
32
+ layoutPath = layout;
33
+ }
34
+ }
35
+ }
36
+ // If we reach this point; then a prerender statement does not exist
37
+ if (layoutPath) {
38
+ const p = path.join('src', 'routes', layoutPath);
39
+ // The most preferred layout path exists
40
+ const layoutContent = fs.readFileSync(p, 'utf-8');
41
+ fs.writeFileSync(p, `${layoutContent}\nexport const prerender = true`);
42
+ } else {
43
+ // We need to create the layout
44
+ const p = path.join('src', 'routes', layoutPaths[0]);
45
+ fs.writeFileSync(p, `export const prerender = true`);
46
+ }
47
+ }
48
+
49
+ // script({ content, filename, attributes }) {
50
+ // if (filename?.endsWith("/routes/+layout.svelte") && attributes.context === "module") {
51
+ // return {
52
+ // code: `export const prerender = true;\n${content}`
53
+ // }
54
+ // }
55
+ // }
56
+ };
@@ -0,0 +1,17 @@
1
+ import * as jsdom from 'jsdom';
2
+
3
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
4
+ export const obfuscateCode = {
5
+ markup: ({ content }) => {
6
+ const dom = new jsdom.JSDOM(content, { contentType: 'text/html' });
7
+ const codeElements = dom.window.document.querySelectorAll('code');
8
+ let r = content;
9
+ codeElements.forEach((el) => {
10
+ const originalContent = el.innerHTML;
11
+ el.innerHTML = `{@html atob(\`${btoa(el.innerHTML.trim() ?? '')}\`)}`;
12
+ r = r.replace(originalContent, el.innerHTML);
13
+ });
14
+
15
+ return { code: r.trim() };
16
+ }
17
+ };
@@ -0,0 +1,21 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { obfuscateCode } from './obfuscateCode.js';
3
+
4
+ describe('obfusctateCode', () => {
5
+ it('Should b64 encode basic javascript', () => {
6
+ const r = obfuscateCode.markup({
7
+ content: `<code lang="javascript">
8
+ console.log()
9
+ </code>`
10
+ });
11
+ expect(r.code).toEqual(`<code lang="javascript">{@html atob(\`Y29uc29sZS5sb2coKQ==\`)}</code>`);
12
+ });
13
+ it('Should b64 encode basic SQL', () => {
14
+ const r = obfuscateCode.markup({
15
+ content: `<code lang="sql">
16
+ SELECT * FROM 5
17
+ </code>`
18
+ });
19
+ expect(r.code).toEqual(`<code lang="sql">{@html atob(\`U0VMRUNUICogRlJPTSA1\`)}</code>`);
20
+ });
21
+ });
@@ -0,0 +1,58 @@
1
+ import * as jsdom from 'jsdom';
2
+ import { checkImport } from '../../../../lib/check-import.js';
3
+
4
+ /**
5
+ * @type {Record<string, boolean>}
6
+ */
7
+ const fileHasQueries = {};
8
+
9
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
10
+ export const transformQueries = {
11
+ script: ({ filename, content, attributes }) => {
12
+ if (attributes.context) return;
13
+ if (!filename) return;
14
+ if (!fileHasQueries[filename]) return;
15
+ if (checkImport('QuerySSR', '$evidence/QuerySSR.svelte', content)) return { code: content };
16
+ else {
17
+ return { code: `import QuerySSR from "$evidence/QuerySSR.svelte";\n` + content };
18
+ }
19
+ },
20
+ markup: ({ filename, content }) => {
21
+ if (!filename) return;
22
+ const dom = new jsdom.JSDOM(content, { contentType: 'text/html' });
23
+ const codeElements = dom.window.document.querySelectorAll('code[evidence-query-name]');
24
+ if (codeElements.length) {
25
+ fileHasQueries[filename] = true;
26
+ }
27
+ let r = content;
28
+ for (const el of codeElements) {
29
+ let target = el;
30
+ if (el.parentElement?.tagName.toLowerCase() === 'pre') {
31
+ target = el.parentElement;
32
+ }
33
+ const originalHtml = target.outerHTML;
34
+
35
+ target.classList.add('text-xs');
36
+ target.classList.add('leading-tight');
37
+ target.classList.add('bg-base-300');
38
+ target.classList.add('p-2');
39
+ target.classList.add('w-fit');
40
+ target.classList.add('min-w-[60ch]');
41
+ el.textContent = `{__evidence_query_text_${el.getAttribute('evidence-query-name')}}`;
42
+
43
+ // TODO: Figure this out
44
+ const replaceTarget = originalHtml.replaceAll('&lt;', '<').replaceAll('&gt;', '>');
45
+
46
+ r = r.replace(replaceTarget, '');
47
+ r =
48
+ r +
49
+ `\n<svelte:head> <meta evidence-query-presence={btoa(__evidence_query_text_${el.getAttribute(
50
+ 'evidence-query-name'
51
+ )})} evidence-query-content={__evidence_query_text_${el.getAttribute(
52
+ 'evidence-query-name'
53
+ )}}> </svelte:head>`;
54
+ }
55
+
56
+ return { code: r };
57
+ }
58
+ };
@@ -0,0 +1,24 @@
1
+ import { getEvidenceConfig } from '../../configuration/index.js';
2
+
3
+ /**
4
+ * @returns {import('vite').Plugin}
5
+ */
6
+ export const configVirtual = () => {
7
+ /** @type {any} */
8
+ let config;
9
+ return {
10
+ name: 'evidence:virtuals-config',
11
+ resolveId(id) {
12
+ if (id === '$evidence/config') {
13
+ return `\0$evidence/config`;
14
+ }
15
+ return null;
16
+ },
17
+ load: async (id) => {
18
+ if (id === `\0$evidence/config`) {
19
+ if (!config) config = getEvidenceConfig();
20
+ return `export const config = ${JSON.stringify(config)};`;
21
+ }
22
+ }
23
+ };
24
+ };
@@ -0,0 +1,7 @@
1
+ export const VITE_EVENTS = Object.freeze({
2
+ SOURCE_END: 'evidence:source-end',
3
+ SOURCE_START: 'evidence:source-start',
4
+ SOURCE_ERROR: 'evidence:source-error',
5
+
6
+ RESET_QUERIES: 'evidence:reset-queries'
7
+ });
@@ -0,0 +1,117 @@
1
+ import path from 'path';
2
+ import { evalSources } from '../../../plugins/datasources/evalSources.js';
3
+ import { dataDirectory, metaDirectory, sourcesDirectory } from '../../../lib/projectPaths.js';
4
+ import { updateManifest } from '../../../plugins/datasources/updateManifest.js';
5
+ import { ProcessingQueue } from '../../../lib/processing-queue.js';
6
+ import { VITE_EVENTS } from '../constants.js';
7
+ import { debounce } from 'perfect-debounce';
8
+ /**
9
+ * @returns {import("vite").Plugin}
10
+ */
11
+ export const sourceQueryHmr = () => {
12
+ const processingQueue = ProcessingQueue();
13
+
14
+ /** @type {import('vite').ViteDevServer | undefined} */
15
+ let server;
16
+
17
+ /** @type {import('../../../plugins/datasources/types.js').Manifest | undefined} */
18
+ let latestManifest;
19
+
20
+ /**
21
+ * @param {string | null} datasource
22
+ * @param {string | null} table
23
+ */
24
+ const processSource = (datasource, table) => async () => {
25
+ if (!server) {
26
+ console.warn('missing ref to dev server');
27
+ return;
28
+ }
29
+ server.hot.send(VITE_EVENTS.SOURCE_START, {
30
+ id: `${datasource}.${table}`,
31
+ toast: {
32
+ id: `${datasource}.${table}`,
33
+ status: 'info',
34
+ message: `Loading ${datasource}.${table}`
35
+ }
36
+ });
37
+
38
+ try {
39
+ const updatedManifest = await evalSources(
40
+ dataDirectory,
41
+ metaDirectory,
42
+ {
43
+ sources: datasource ? new Set([datasource]) : null,
44
+ queries: table ? new Set([table]) : null,
45
+ only_changed: false
46
+ },
47
+ true
48
+ );
49
+ latestManifest = await updateManifest(updatedManifest, dataDirectory);
50
+
51
+ server?.hot.send(VITE_EVENTS.SOURCE_END, {
52
+ id: `${datasource}.${table}-end`,
53
+ toast: {
54
+ id: `${datasource}.${table}`,
55
+ status: 'success',
56
+ message: `Finished ${datasource}.${table}`
57
+ }
58
+ });
59
+ } catch (e) {
60
+ server?.hot.send(VITE_EVENTS.SOURCE_ERROR, {
61
+ error: e instanceof Error ? e.message : e,
62
+ toast: {
63
+ id: `${datasource}.${table}`,
64
+ status: 'error',
65
+ message: `Failed to process ${datasource}.${table}`
66
+ }
67
+ });
68
+ }
69
+ };
70
+
71
+ const queueOptions = debounce(
72
+ /**
73
+ * Whenever sources are saved, the connection.options.yaml file and the connection.yaml file are written
74
+ * Because we are using a file-watch, this would double up the executions, so we debounce them to prevent that
75
+ * @param {string} sourceName
76
+ */
77
+ (sourceName) => {
78
+ processingQueue.add(processSource(sourceName, null));
79
+ },
80
+ 50
81
+ );
82
+
83
+ /** @type {import("vite").Plugin} */
84
+ return {
85
+ name: 'evidence:source-query-hmr',
86
+ buildStart: function () {
87
+ if (this.meta.watchMode) {
88
+ this.addWatchFile(sourcesDirectory);
89
+ }
90
+ },
91
+ configureServer: (s) => {
92
+ server = s;
93
+ processingQueue.addListener('done', () => {
94
+ s.hot.send(VITE_EVENTS.RESET_QUERIES, { latestManifest });
95
+ });
96
+ },
97
+ /** @type {import("vite").Plugin['watchChange']} */
98
+ watchChange: async function (id) {
99
+ const changed = path.resolve(id);
100
+ if (!changed.startsWith(sourcesDirectory)) return; // don't care
101
+
102
+ const parts = changed.replace(sourcesDirectory, '').split(path.sep);
103
+ const sourceName = parts.at(1);
104
+ const queryName = path.basename(changed).split('.').at(0);
105
+ if (!sourceName || !queryName) {
106
+ console.warn(
107
+ `Failed to HMR source query at ${changed}, could not identify source or query name`
108
+ );
109
+ return;
110
+ }
111
+ // TODO: How can we debounce a little bit to make sure we don't run twice?
112
+ if (queryName === 'connection' || queryName === 'connection.options')
113
+ queueOptions(sourceName);
114
+ else processingQueue.add(processSource(sourceName, queryName));
115
+ }
116
+ };
117
+ };
@@ -0,0 +1,6 @@
1
+ declare module '$evidence/config' {
2
+ type EvidenceConfigSchema =
3
+ typeof import('../../configuration/schemas/config.schema.js').EvidenceConfigSchema;
4
+ export type EvidenceConfig = import('zod').z.infer<EvidenceConfigSchema>;
5
+ export const config: EvidenceConfig;
6
+ }
@@ -0,0 +1,214 @@
1
+ import * as path from 'path';
2
+ import fs from 'fs/promises';
3
+ import chalk from 'chalk';
4
+ import { applySidecarConfig, getSidecarApp } from './sidecar-app.js';
5
+
6
+ import { dirname } from 'path';
7
+ import { fileURLToPath } from 'url';
8
+ import { getManifest } from './virtuals/node/static-assets.js';
9
+ import { copyToLayout } from './layout-plugins/copy-to-layout.js';
10
+ import { dataDirectory, evidenceDirectory } from './../../lib/projectPaths.js';
11
+ /** @type {Record<string, () => string>} */
12
+ import * as dynamicVirtuals from './virtuals-dynamic.js';
13
+
14
+ const __dirname = dirname(fileURLToPath(import.meta.url));
15
+
16
+ const PREFIX = `$evidence/`;
17
+
18
+ /**
19
+ * @type {() => Promise<import("vite").Plugin>}
20
+ */
21
+ export const evidencePlugin = async () => {
22
+ const sidecar = getSidecarApp();
23
+
24
+ // TODO: Find a way to make this get a list of queries that should be available on the page
25
+ return {
26
+ name: 'evidence',
27
+
28
+ buildStart: async function () {
29
+ if (this.meta.watchMode) {
30
+ await copyToLayout();
31
+ } else {
32
+ this.emitFile({
33
+ type: 'asset',
34
+ name: path.join('_evidence', 'manifest.json'),
35
+ source: getManifest(),
36
+ fileName: path.join('_evidence', 'manifest.json'),
37
+ needsCodeReference: false
38
+ });
39
+ const dataDirExists = await fs
40
+ .stat(dataDirectory)
41
+ .then((r) => r.isDirectory())
42
+ .catch(() => false);
43
+ if (!dataDirExists) {
44
+ console.warn(
45
+ chalk.yellow(
46
+ 'Evidence data directory not found, if you use source queries in this project, you probably need to run sources'
47
+ )
48
+ );
49
+ return;
50
+ }
51
+
52
+ const queries = await fs.readdir(dataDirectory, { recursive: true, withFileTypes: true });
53
+
54
+ for (const query of queries) {
55
+ if (query.isFile() && query.name.endsWith('.parquet')) {
56
+ const relPath = path.join(path.relative(dataDirectory, query.path), query.name);
57
+
58
+ this.emitFile({
59
+ type: 'asset',
60
+ name: path.join('_evidence', relPath),
61
+ source: await fs.readFile(path.join(query.path, query.name)),
62
+ fileName: path.join('_evidence', relPath),
63
+ needsCodeReference: false
64
+ });
65
+ }
66
+ }
67
+ }
68
+ },
69
+
70
+ async configureServer(vite) {
71
+ vite.middlewares.use(sidecar);
72
+ },
73
+
74
+ async configurePreviewServer(vite) {
75
+ vite.middlewares.use(sidecar);
76
+ },
77
+
78
+ async config(cfg) {
79
+ if (!cfg.optimizeDeps) cfg.optimizeDeps = {};
80
+ if (!cfg.optimizeDeps.exclude) cfg.optimizeDeps.exclude = [];
81
+ if (!cfg.optimizeDeps.exclude.includes('@evidence-dev/universal-sql/client-duckdb'))
82
+ cfg.optimizeDeps.exclude.push('@evidence-dev/universal-sql/client-duckdb');
83
+
84
+ if (!cfg.optimizeDeps.include) cfg.optimizeDeps.include = [];
85
+ // TODO: This has something to do with the import management crap
86
+ // if (!cfg.ssr) cfg.ssr = {}
87
+ // if (!cfg.ssr.noExternal) cfg.ssr.noExternal = []
88
+ // if (typeof cfg.ssr.noExternal !== "boolean" && !Array.isArray(cfg.ssr.noExternal)) cfg.ssr.noExternal = [cfg.ssr.noExternal]
89
+ // if (typeof cfg.ssr.noExternal !== "boolean") {
90
+ // cfg.ssr.noExternal.push(/@evidence-dev\/.+/)
91
+ // cfg.ssr.noExternal.push(/@steeze-ui\/.+/)
92
+ // }
93
+
94
+ if (!cfg.server) cfg.server = {};
95
+ if (!cfg.server.fs) cfg.server.fs = {};
96
+ if (!cfg.server.fs.allow) cfg.server.fs.allow = [];
97
+
98
+ cfg.server.fs.allow.push(evidenceDirectory);
99
+
100
+ // // Lifted from SvelteKit
101
+ // const allow = new Set([
102
+ // path.resolve('src'), // TODO this isn't correct if user changed all his files to sth else than src (like in test/options)
103
+ // path.resolve('node_modules'),
104
+ // // path.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
105
+ // ]);
106
+
107
+ for (const allowDir of ['.', 'node_modules', '../node_modules', '../../node_modules']) {
108
+ if (!cfg.server.fs.allow.includes(allowDir)) cfg.server.fs.allow.push(allowDir);
109
+ }
110
+
111
+ // This lets us serve from node_modules regardless of where it is linked.
112
+ // Vite considers the "real" path, not the link path when serving.
113
+
114
+ /** @type {string} */
115
+ let resolvingPath = path.resolve('node_modules');
116
+ // eslint-disable-next-line no-constant-condition
117
+ while (true) {
118
+ /** @type {string | false} */
119
+ const nextPath = await fs
120
+ .readlink(resolvingPath)
121
+ .catch(() => fs.readlink(path.resolve(resolvingPath, '..')))
122
+ .catch(() => false);
123
+ if (!nextPath) {
124
+ // /home/brian/code/evidence/sdk/examples/example-layout-plugin/node_modules/@sveltejs/kit/src/runtime/client/entry.js
125
+ // /home/brian/code/evidence/sdk/test-project/examples/example-layout-plugin
126
+ cfg.server.fs.allow.push(resolvingPath);
127
+ break;
128
+ } else {
129
+ if (nextPath.startsWith('.')) resolvingPath = path.relative(resolvingPath, nextPath);
130
+ else resolvingPath = nextPath;
131
+ }
132
+ }
133
+
134
+ cfg = applySidecarConfig(cfg);
135
+
136
+ return cfg;
137
+ },
138
+
139
+ resolveId(id) {
140
+ if (id.startsWith(PREFIX)) {
141
+ return id;
142
+ }
143
+
144
+ return null;
145
+ },
146
+
147
+ async load(id, opts) {
148
+ if (id.startsWith(PREFIX)) {
149
+ const virtuals = await fs.readdir(path.join(__dirname, 'virtuals'), { recursive: true });
150
+ const idNoPrefix = id.slice(PREFIX.length);
151
+ const idNoExtension = idNoPrefix.split('.').at(0);
152
+ if (!idNoExtension) return null;
153
+
154
+ if (idNoExtension in dynamicVirtuals) {
155
+ const dynFunction = /** @type {() => string | Promise<string>} */ (
156
+ // @ts-expect-error
157
+ dynamicVirtuals[idNoExtension]
158
+ );
159
+ return await dynFunction();
160
+ }
161
+
162
+ const matches = virtuals.filter((filepath) => {
163
+ if (filepath.endsWith('.d.ts')) return false;
164
+ if (filepath.split('.').at(-2) === 'spec') return false;
165
+ if (!filepath.split('/').at(-1)?.split('.').includes(idNoExtension)) return false;
166
+ return true;
167
+ });
168
+
169
+ let targetFile;
170
+
171
+ if (matches.length === 1) {
172
+ // Only one possible match
173
+ targetFile = matches[0];
174
+ } else if (matches.length > 1) {
175
+ // There are choices to be made here
176
+ const environmentOptions = matches.filter((m) =>
177
+ m.startsWith(opts?.ssr ? 'node/' : 'browser/')
178
+ );
179
+
180
+ if (environmentOptions.length >= 1) {
181
+ if (environmentOptions.length > 1) {
182
+ console.warn(
183
+ chalk.yellow(
184
+ `[!] Multiple Import Files found for $evidence/${idNoPrefix}, this is ambigious and should be corrected. ${environmentOptions.join(
185
+ ', '
186
+ )}`
187
+ )
188
+ );
189
+ }
190
+ targetFile = environmentOptions[0];
191
+ } else {
192
+ // There are no specifics
193
+ console.warn(
194
+ chalk.yellow(
195
+ `[!] Multiple Import Files found for $evidence/${idNoPrefix}, this is ambigious and should be corrected. ${matches.join(
196
+ ', '
197
+ )}`
198
+ )
199
+ );
200
+ targetFile = matches[0];
201
+ }
202
+ } else {
203
+ console.warn(chalk.yellow(`[!] No Import Files found for $evidence/${idNoPrefix}`));
204
+ return null;
205
+ }
206
+
207
+ this.addWatchFile(path.join(__dirname, 'virtuals', targetFile));
208
+ return await fs.readFile(path.join(__dirname, 'virtuals', targetFile), 'utf-8');
209
+ }
210
+
211
+ return null;
212
+ }
213
+ };
214
+ };
@@ -0,0 +1,71 @@
1
+ import chokidar from 'chokidar';
2
+ import { loadLayoutPlugin } from '../../../plugins/layouts/loadLayoutPlugin.js';
3
+ import path from 'path';
4
+ import { projectRoot } from '../../../lib/projectRoot.js';
5
+ import { copyMethods } from '../../../plugins/layouts/copyMethods/index.js';
6
+ import fs from 'fs/promises';
7
+ import { EvidenceError } from '../../../lib/EvidenceError.js';
8
+ import chalk from 'chalk';
9
+
10
+ export const copyToLayout = async () => {
11
+ const plugin = await loadLayoutPlugin();
12
+ if (!plugin) return false;
13
+
14
+ const routesDestination = plugin.evidence.layout.routes.destination.split(/[/\\]/);
15
+ const templateRoot = path.join(projectRoot, '.evidence', 'template');
16
+ const rootTargetDir = path.join(templateRoot, ...routesDestination);
17
+ const rootSourceDir = path.join(projectRoot, 'pages');
18
+
19
+ const method = copyMethods[plugin.evidence.layout.routes.style];
20
+ const pageWatcher = chokidar.watch(rootSourceDir, { alwaysStat: true });
21
+
22
+ /**
23
+ * @param {string} filepath
24
+ */
25
+ const copyToLayout = async (filepath) => {
26
+ const parsedPath = path.parse(filepath);
27
+ const dirEntries = await fs.readdir(parsedPath.dir, { withFileTypes: true });
28
+ const dirEnt = dirEntries.find((ent) => ent.name === parsedPath.base);
29
+ if (!dirEnt)
30
+ throw new EvidenceError('Error Copying page to layout', [
31
+ 'dirEnt could not be found, this should not happen.'
32
+ ]);
33
+ method.copyFile(rootSourceDir, rootTargetDir, dirEnt);
34
+ };
35
+
36
+ // TODO: Handle moved files, deleted files, etc
37
+ pageWatcher.on('add', copyToLayout);
38
+ pageWatcher.on('change', copyToLayout);
39
+
40
+ const componentsDestination = plugin.evidence.layout.components.destination.split(/[/\\]/);
41
+ const componentsSource = path.join(projectRoot, 'components');
42
+ const componentWatcher = chokidar.watch(componentsSource);
43
+ /**
44
+
45
+ * @param {string} filepath
46
+ */
47
+ const copyComponent = async (filepath) => {
48
+ const relPath = path.relative(componentsSource, filepath);
49
+ const destination = path.join(templateRoot, ...componentsDestination, relPath);
50
+ console.debug(chalk.dim(`Copied ${filepath} -> ${destination}`));
51
+ await fs.cp(filepath, destination);
52
+ };
53
+ componentWatcher.on('add', copyComponent);
54
+ componentWatcher.on('change', copyComponent);
55
+
56
+ const staticDestination = plugin.evidence.layout.static.destination.split(/[/\\]/);
57
+ const staticSource = path.join(projectRoot, 'static');
58
+ const staticWatcher = chokidar.watch(staticSource);
59
+ /**
60
+
61
+ * @param {string} filepath
62
+ */
63
+ const copyStatic = async (filepath) => {
64
+ const relPath = path.relative(staticSource, filepath);
65
+ const destination = path.join(templateRoot, ...staticDestination, relPath);
66
+ console.debug(chalk.dim(`Copied ${filepath} -> ${destination}`));
67
+ await fs.cp(filepath, destination);
68
+ };
69
+ staticWatcher.on('add', copyStatic);
70
+ staticWatcher.on('change', copyStatic);
71
+ };
@@ -0,0 +1,4 @@
1
+ export { sourceQueryHmr } from './datasource-plugins/source-query-hmr.js';
2
+ export { copyToLayout } from './layout-plugins/copy-to-layout.js';
3
+ export { configVirtual } from './config-virtual.js';
4
+ export { queryDirectoryHmr } from './query-directory-hmr.js';