@yolean/evidence 0.0.0-stage → 40.3.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/cli.js +436 -0
  3. package/package.json +66 -3
  4. package/template/README.md +11 -0
  5. package/template/postcss.config.cjs +15 -0
  6. package/template/src/app.css +327 -0
  7. package/template/src/app.html +96 -0
  8. package/template/src/fix-tprotocol-service-worker.js +36 -0
  9. package/template/src/global.d.ts +1 -0
  10. package/template/src/hooks.client.js +19 -0
  11. package/template/src/hooks.server.js +41 -0
  12. package/template/src/pages/+error.svelte +57 -0
  13. package/template/src/pages/+layout.js +220 -0
  14. package/template/src/pages/+layout.svelte +10 -0
  15. package/template/src/pages/+page.md +16 -0
  16. package/template/src/pages/api/[...route]/evidencemeta.json/+server.js +48 -0
  17. package/template/src/pages/api/[route_hash]/[additional_hash]/all-queries.json/+server.js +10 -0
  18. package/template/src/pages/api/customFormattingSettings.json/+server.js +82 -0
  19. package/template/src/pages/api/customFormattingSettings.json/GET.json/+server.js +6 -0
  20. package/template/src/pages/api/pagesManifest.json/+server.js +92 -0
  21. package/template/src/pages/api/pagesManifest.json/buildPageManifest.spec.js +274 -0
  22. package/template/src/pages/api/prerendered_queries/[query_hash].arrow/+server.js +10 -0
  23. package/template/src/pages/api/settings.json/+server.js +107 -0
  24. package/template/src/pages/explore/+layout.js +5 -0
  25. package/template/src/pages/explore/console/+page.svelte +5 -0
  26. package/template/src/pages/explore/schema/+page.svelte +13 -0
  27. package/template/src/pages/fix-tprotocol-service-worker.js/+server.js +20 -0
  28. package/template/src/pages/manifest.webmanifest/+server.js +21 -0
  29. package/template/src/pages/settings/+layout.server.js +1 -0
  30. package/template/src/pages/settings/+page.js +25 -0
  31. package/template/src/pages/settings/+page.server.js +110 -0
  32. package/template/src/pages/settings/+page.svelte +111 -0
  33. package/template/static/apple-touch-icon.png +0 -0
  34. package/template/static/favicon.ico +0 -0
  35. package/template/static/icon-192.png +0 -0
  36. package/template/static/icon-512.png +0 -0
  37. package/template/static/icon.svg +6 -0
  38. package/template/svelte.config.js +106 -0
  39. package/template/tailwind.config.cjs +48 -0
  40. package/template/vite.config.js +99 -0
  41. package/README.md +0 -3
@@ -0,0 +1,220 @@
1
+ import { browser, building, dev } from '$app/environment';
2
+ import {
3
+ tableFromIPC,
4
+ initDB,
5
+ setParquetURLs,
6
+ query as usqlQuery,
7
+ updateSearchPath,
8
+ arrowTableToJSON
9
+ } from '@evidence-dev/universal-sql/client-duckdb';
10
+ import { profile } from '@evidence-dev/component-utilities/profile';
11
+ import { toasts } from '@evidence-dev/component-utilities/stores';
12
+ import { setTrackProxy } from '@evidence-dev/sdk/usql';
13
+ import { addBasePath, addScopedBasePath, getDataScope } from '@evidence-dev/sdk/utils/svelte';
14
+ import md5 from 'blueimp-md5';
15
+
16
+ export const ssr = !dev;
17
+ export const prerender = import.meta.env.VITE_EVIDENCE_SPA !== 'true';
18
+ export const trailingSlash = 'always';
19
+
20
+ /**
21
+ * @param {string | null} scope deployment.dataScope value; the browser loads only that scope's data
22
+ */
23
+ const loadDB = async (scope) => {
24
+ let renderedFiles = {};
25
+ /** @param {string} path */
26
+ const addDataPath = (path) => (browser ? addScopedBasePath(path, scope) : addBasePath(path));
27
+
28
+ if (!browser) {
29
+ const { readFile } = await import('fs/promises');
30
+ ({ renderedFiles } = JSON.parse(
31
+ await readFile('./static/data/manifest.json', 'utf-8').catch(() => '{}')
32
+ ));
33
+ } else {
34
+ const res = await fetch(addDataPath('/data/manifest.json'));
35
+ if (res.ok) ({ renderedFiles } = await res.json());
36
+ }
37
+ await profile(initDB);
38
+
39
+ if (Object.keys(renderedFiles ?? {}).length === 0) {
40
+ console.warn(`No sources found, execute "npm run sources" to generate`.trim());
41
+ if (dev) {
42
+ toasts.add(
43
+ {
44
+ id: 'MissingManifest',
45
+ status: 'warning',
46
+ title: 'No Sources Found',
47
+ message: 'Configure and run sources to include data in your project.'
48
+ },
49
+ 10000
50
+ );
51
+ }
52
+ } else {
53
+ await profile(setParquetURLs, renderedFiles, { addBasePath: addDataPath });
54
+ await profile(updateSearchPath, Object.keys(renderedFiles));
55
+ }
56
+ };
57
+
58
+ /** @type {Promise<void> | undefined} */
59
+ let database_initialization;
60
+ /** @type {string | null | undefined} */
61
+ let database_scope;
62
+ /**
63
+ * The server prerenders every scope from the full data set; the browser
64
+ * reloads when navigation crosses into another scope.
65
+ * @param {string | null} scope
66
+ */
67
+ const initializeDatabase = (scope) => {
68
+ if (!database_initialization || (browser && scope !== database_scope)) {
69
+ database_scope = scope;
70
+ database_initialization = profile(loadDB, scope);
71
+ }
72
+ return database_initialization;
73
+ };
74
+
75
+ /**
76
+ *
77
+ * @param {string} routeHash
78
+ * @param {string} paramsHash
79
+ * @param {typeof fetch} fetch
80
+ * @param {string | null} scope
81
+ * @returns {Promise<Record<string, unknown[]>>}
82
+ */
83
+ async function getPrerenderedQueries(routeHash, paramsHash, fetch, scope) {
84
+ // get every query that's run in the component
85
+ const res = await fetch(
86
+ addScopedBasePath(`/api/${routeHash}/${paramsHash}/all-queries.json`, scope)
87
+ );
88
+ if (!res.ok) return {};
89
+
90
+ const sql_cache_with_hashed_query_strings = await res.json();
91
+
92
+ const resolved_entries = await Promise.all(
93
+ Object.entries(sql_cache_with_hashed_query_strings).map(async ([query_name, query_hash]) => {
94
+ const res = await fetch(
95
+ addScopedBasePath(`/api/prerendered_queries/${query_hash}.arrow`, scope)
96
+ );
97
+ if (!res.ok) return null;
98
+
99
+ const table = await tableFromIPC(res);
100
+ return [query_name, arrowTableToJSON(table)];
101
+ })
102
+ );
103
+
104
+ return Object.fromEntries(resolved_entries.filter(Boolean));
105
+ }
106
+
107
+ const system_routes = ['/settings', '/explore'];
108
+
109
+ /** @type {Map<string, { inputs: Record<string, string> }>} */
110
+ const dummy_pages = new Map();
111
+
112
+ /** @satisfies {import("./$types").LayoutLoad} */
113
+ export const load = async ({ fetch, route, params, url }) => {
114
+ const scope = getDataScope(params);
115
+ // prerendering fetches the shared files; the build copies them into each scope
116
+ /** @param {string} path */
117
+ const addApiPath = (path) => (browser ? addScopedBasePath(path, scope) : addBasePath(path));
118
+ const [{ customFormattingSettings }, pagesManifest, evidencemeta] = await Promise.all([
119
+ fetch(addApiPath('/api/customFormattingSettings.json/GET.json')).then((x) => x.json()),
120
+ fetch(addApiPath('/api/pagesManifest.json')).then((x) => x.json()),
121
+ fetch(addApiPath(`/api/${route.id}/evidencemeta.json`))
122
+ .then((x) => x.json())
123
+ .catch(() => ({ queries: [] }))
124
+ ]);
125
+
126
+ const routeHash = md5(route.id);
127
+ const paramsHash = md5(
128
+ Object.entries(params)
129
+ .sort()
130
+ .map(([key, value]) => `${key}\x1F${value}`)
131
+ .join('\x1E')
132
+ );
133
+ const isUserPage =
134
+ route.id && system_routes.every((system_route) => !route.id.startsWith(system_route));
135
+
136
+ /** @type {App.PageData["data"]} */
137
+ let data = {};
138
+
139
+ const {
140
+ inputs = setTrackProxy({
141
+ label: '',
142
+ value: '(SELECT NULL WHERE 0 /* An Input has not been set */)'
143
+ }) /* Create a proxy by default */
144
+ } = dummy_pages.get(url.pathname) ?? {};
145
+
146
+ const is_dummy_page = dummy_pages.has(url.pathname);
147
+ if ((dev || building) && !browser && !is_dummy_page) {
148
+ dummy_pages.set(url.pathname, { inputs });
149
+ await fetch(url);
150
+ dummy_pages.delete(url.pathname);
151
+ }
152
+
153
+ const database_ready = initializeDatabase(scope);
154
+ if (!browser) await database_ready;
155
+ // account for potential changes in manifest (source query hmr)
156
+ if (!browser && dev) await initDB();
157
+
158
+ // let SSR saturate the cache first
159
+ if (browser && isUserPage && prerender) {
160
+ data = await getPrerenderedQueries(routeHash, paramsHash, fetch, scope);
161
+ }
162
+
163
+ /** @type {App.PageData["__db"]["query"]} */
164
+ function query(sql, { query_name, callback = (x) => x } = {}) {
165
+ if (browser) {
166
+ return (async () => {
167
+ await database_ready;
168
+ const result = await usqlQuery(sql);
169
+ return callback(result);
170
+ })();
171
+ }
172
+
173
+ return callback(
174
+ usqlQuery(sql, {
175
+ route_hash: routeHash,
176
+ additional_hash: paramsHash,
177
+ query_name,
178
+ prerendering: building,
179
+ scope
180
+ })
181
+ );
182
+ }
183
+
184
+ let tree = pagesManifest;
185
+ for (const part of (route.id ?? '').split('/').slice(1)) {
186
+ tree = tree.children[part];
187
+ if (!tree) break;
188
+ if (tree.frontMatter?.title) {
189
+ tree.title = tree.frontMatter.title;
190
+ } else if (tree.frontMatter?.breadcrumb) {
191
+ let { breadcrumb } = tree.frontMatter;
192
+ for (const [param, value] of Object.entries(params)) {
193
+ breadcrumb = breadcrumb.replaceAll(`\${params.${param}}`, value);
194
+ }
195
+ tree.title = (await query(breadcrumb))[0]?.breadcrumb;
196
+ }
197
+ }
198
+
199
+ return /** @type {App.PageData} */ ({
200
+ __db: {
201
+ query,
202
+ async load() {
203
+ return database_ready;
204
+ },
205
+ async updateParquetURLs(manifest) {
206
+ // todo: maybe diff with old?
207
+ const { renderedFiles } = JSON.parse(manifest);
208
+ await profile(setParquetURLs, renderedFiles, {
209
+ addBasePath: (path) => addScopedBasePath(path, scope)
210
+ });
211
+ }
212
+ },
213
+ inputs,
214
+ data,
215
+ customFormattingSettings,
216
+ isUserPage,
217
+ evidencemeta,
218
+ pagesManifest
219
+ });
220
+ };
@@ -0,0 +1,10 @@
1
+ <script>
2
+ import '@evidence-dev/tailwind/fonts.css';
3
+ import '../app.css';
4
+ import { EvidenceDefaultLayout } from '@evidence-dev/core-components';
5
+ export let data;
6
+ </script>
7
+
8
+ <EvidenceDefaultLayout {data}>
9
+ <slot slot="content" />
10
+ </EvidenceDefaultLayout>
@@ -0,0 +1,16 @@
1
+ # Welcome to the Evidence Development Workspace!
2
+
3
+ <Alert status=warning>
4
+ If you see this page in your Evidence App, you have deleted the `index.md` file from your project. Add a `pages/index.md` to your project to override this page.
5
+ </Alert>
6
+
7
+ This workspace contains examples of all Evidence features.
8
+
9
+ If you are developing a new feature, add a page to this project. The page will be used as a reference for the feature, as a sandbox for anyone making changes to the feature in the future, and for general product testing.
10
+
11
+ If you notice a feature is missing from this project, please:
12
+
13
+ - add a page or
14
+ - create an issue on Github.
15
+
16
+ Thanks!
@@ -0,0 +1,48 @@
1
+ import fs from 'fs/promises';
2
+ import path from 'path';
3
+ import preprocessor from '@evidence-dev/preprocess';
4
+ import { json } from '@sveltejs/kit';
5
+ import { paths, isExampleProject } from '@evidence-dev/sdk/meta';
6
+
7
+ export const prerender = true;
8
+
9
+ /** @type {import('./$types').EntryGenerator} */
10
+ export const entries = async () => {
11
+ const pagesDir = paths.pagesDirectory;
12
+
13
+ const allPages = (await fs.readdir(pagesDir, { recursive: true })).filter((f) =>
14
+ f.endsWith('.md')
15
+ ); // Discard everything that isn't a page
16
+
17
+ const output = allPages.map((filepath) => {
18
+ // Example Project is special
19
+ const removal = isExampleProject ? '/+page.md' : '.md';
20
+ let result = filepath.slice(0, -removal.length);
21
+ if (filepath.endsWith('index.md')) result = result.replaceAll(/\/?index/g, '');
22
+ return { route: result };
23
+ });
24
+
25
+ return output;
26
+ };
27
+
28
+ /** @type {import("./$types").RequestHandler} */
29
+ export async function GET({ params: { route } }) {
30
+ if (route === '/settings') {
31
+ const queries = [];
32
+ return json({ queries });
33
+ }
34
+ let routesDir;
35
+ if ((await fs.readdir(process.cwd())).includes('src')) {
36
+ routesDir = path.join('src', 'pages'); // example project wackiness
37
+ } else {
38
+ routesDir = path.join('.evidence', 'template', 'src', 'pages');
39
+ }
40
+ const routePath = path.join(process.cwd(), routesDir, route, '+page.md');
41
+
42
+ const content = await fs.readFile(routePath, 'utf8');
43
+
44
+ const partialInjectedContent = preprocessor.injectPartials(content);
45
+ const queries = preprocessor.extractQueries(partialInjectedContent);
46
+
47
+ return json({ queries });
48
+ }
@@ -0,0 +1,10 @@
1
+ import { get_all_page_queries } from '@evidence-dev/universal-sql';
2
+ import { error } from '@sveltejs/kit';
3
+
4
+ export const GET = ({ params }) => {
5
+ try {
6
+ return new Response(get_all_page_queries(params.route_hash, params.additional_hash));
7
+ } catch (e) {
8
+ throw error(404, 'not found');
9
+ }
10
+ };
@@ -0,0 +1,82 @@
1
+ export const prerender = false;
2
+
3
+ import { json } from '@sveltejs/kit';
4
+ import fs from 'fs';
5
+ import path from 'path';
6
+
7
+ const CUSTOMIZATION_DIRECTORY = '../customization';
8
+ const CUSTOM_FORMATTING_SETTINGS_PATH = `${CUSTOMIZATION_DIRECTORY}/custom-formatting.json`;
9
+ const DEFAULT_CUSTOM_FORMATTING_SETTINGS = {
10
+ version: '1.0',
11
+ customFormats: []
12
+ };
13
+
14
+ export async function GET() {
15
+ let customFormattingSettings = {};
16
+ try {
17
+ customFormattingSettings = getCustomFormattingSettings() || customFormattingSettings;
18
+ } catch {
19
+ // custom settings will be empty for now.
20
+ }
21
+ let result = { customFormattingSettings };
22
+ return json(result);
23
+ }
24
+
25
+ export async function POST({ request }) {
26
+ const { newCustomFormat } = await request.json();
27
+
28
+ let customFormattingSettings = getCustomFormattingSettings() || {};
29
+
30
+ if (newCustomFormat) {
31
+ if (!customFormattingSettings.customFormats) {
32
+ customFormattingSettings.customFormats = [];
33
+ }
34
+ if (newCustomFormat.formatTag && newCustomFormat.formatCode) {
35
+ customFormattingSettings.customFormats.push(newCustomFormat);
36
+ }
37
+ saveCustomFormattingSettings(customFormattingSettings);
38
+ }
39
+ return json(customFormattingSettings);
40
+ }
41
+
42
+ export async function DELETE({ request }) {
43
+ const { formatTag } = await request.json();
44
+ let customFormattingSettings = getCustomFormattingSettings() || {};
45
+ if (formatTag) {
46
+ if (!customFormattingSettings.customFormats) {
47
+ customFormattingSettings.customFormats = [];
48
+ }
49
+ let index = customFormattingSettings.customFormats.findIndex(
50
+ (format) => format.formatTag === formatTag
51
+ );
52
+ if (index >= 0) {
53
+ customFormattingSettings.customFormats.splice(index, 1);
54
+ }
55
+ saveCustomFormattingSettings(customFormattingSettings);
56
+ }
57
+ return json(customFormattingSettings);
58
+ }
59
+
60
+ function getCustomFormattingSettings() {
61
+ let customFormattingSettingsPath = getCustomFormattingSettingsFilePath();
62
+ return JSON.parse(fs.readFileSync(customFormattingSettingsPath, 'utf8'));
63
+ }
64
+
65
+ function saveCustomFormattingSettings(customFormattingSettings) {
66
+ let customFormattingSettingsPath = getCustomFormattingSettingsFilePath();
67
+ fs.writeFileSync(customFormattingSettingsPath, JSON.stringify(customFormattingSettings, null, 2));
68
+ }
69
+
70
+ function getCustomFormattingSettingsFilePath() {
71
+ let customFormattingSettingsPath = path.join(path.resolve('./'), CUSTOM_FORMATTING_SETTINGS_PATH);
72
+ if (!fs.existsSync(customFormattingSettingsPath)) {
73
+ if (!fs.existsSync(CUSTOMIZATION_DIRECTORY)) {
74
+ fs.mkdirSync(CUSTOMIZATION_DIRECTORY);
75
+ }
76
+ fs.writeFileSync(
77
+ customFormattingSettingsPath,
78
+ JSON.stringify(DEFAULT_CUSTOM_FORMATTING_SETTINGS, null, 2)
79
+ );
80
+ }
81
+ return customFormattingSettingsPath;
82
+ }
@@ -0,0 +1,6 @@
1
+ export const prerender = true;
2
+
3
+ /**
4
+ * Re-exported beacuse ../+server.js has routes that can't be prerendered
5
+ */
6
+ export { GET } from '../+server.js';
@@ -0,0 +1,92 @@
1
+ import preprocess from '@evidence-dev/preprocess';
2
+ import { error } from '@sveltejs/kit';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+
6
+ /**
7
+ * @typedef {Object} PageManifestNode
8
+ * @property {string | undefined} label
9
+ * @property {string | undefined} href
10
+ * @property {Record<string, PageManifestNode>} children
11
+ * @property {Record<string, any>} frontMatter
12
+ * @property {boolean} isTemplated
13
+ * @property {boolean} isPage
14
+ */
15
+
16
+ /**
17
+ * @param {Record<string, string>} pages
18
+ * @returns {PageManifestNode}
19
+ */
20
+ export function _buildPageManifest(pages) {
21
+ const fileTree = {
22
+ label: 'Home',
23
+ href: undefined,
24
+ children: {},
25
+ frontMatter: {},
26
+ isTemplated: false,
27
+ isPage: false
28
+ };
29
+ for (const [pagePath, pageContent] of Object.entries(pages)) {
30
+ const path = pagePath.replace('/src/pages/', '');
31
+ let node = fileTree;
32
+ for (const part of path.split('/')) {
33
+ if (part === '+page.md') {
34
+ // cover home page
35
+ if (path === '+page.md') {
36
+ node.href = '/';
37
+ } else if (!path.includes('[')) {
38
+ node.href = encodeURI('/' + path.replace('/+page.md', ''));
39
+ }
40
+ node.frontMatter = preprocess.parseFrontmatter(pageContent);
41
+ node.isPage = true;
42
+ } else {
43
+ const label = part.includes('[') ? undefined : part.replace(/_/g, ' ').replace(/-/g, ' ');
44
+ node = node.children[part] = node.children[part] ?? {
45
+ label,
46
+ href: undefined,
47
+ children: {},
48
+ frontMatter: {},
49
+ isTemplated: part.includes('['),
50
+ isPage: false
51
+ };
52
+ }
53
+ }
54
+ }
55
+ return fileTree;
56
+ }
57
+
58
+ export const prerender = true;
59
+
60
+ /**
61
+ * @type {import("@sveltejs/kit").RequestHandler}
62
+ */
63
+ export async function GET() {
64
+ const pages = {};
65
+
66
+ /** @param {string} dir */
67
+ async function recursiveReadDir(dir) {
68
+ const dirents = await fs.readdir(dir, { withFileTypes: true });
69
+ for (const dirent of dirents) {
70
+ if (dirent.isFile() && dirent.name.endsWith('.md')) {
71
+ const relative_path = path.join(dirent.parentPath ?? dirent.path ?? dir, dirent.name);
72
+ const content = await fs.readFile(relative_path, 'utf-8');
73
+ // regularize for windows
74
+ const normalized_path = '/' + path.normalize(relative_path).split(path.sep).join('/');
75
+ pages[normalized_path] = content;
76
+ } else if (dirent.isDirectory()) {
77
+ await recursiveReadDir(path.join(dir, dirent.name));
78
+ }
79
+ }
80
+ }
81
+
82
+ try {
83
+ await recursiveReadDir(path.join('src', 'pages'));
84
+
85
+ const fileTree = _buildPageManifest(pages);
86
+
87
+ return new Response(JSON.stringify(fileTree));
88
+ } catch (e) {
89
+ console.log('Failed to build pages manifest with error: ', e);
90
+ throw error(500, 'Failed to build pages manifest.');
91
+ }
92
+ }