@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,199 @@
1
+ // TODO: Kit
2
+
3
+ import { findFile } from 'pkg-types';
4
+ import { parse, prettyPrint, visit } from 'recast';
5
+ import fs from 'fs/promises';
6
+ import { EvidenceError } from '../../../lib/EvidenceError.js';
7
+
8
+ /**
9
+ *
10
+ * @param {import("estree").Property} node
11
+ * @param {string} importName
12
+ */
13
+ const insertPreprocessor = (node, importName) => {
14
+ /** @type {import("estree").ArrayExpression} */
15
+ let arrayValue;
16
+
17
+ if (node.value.type !== 'ArrayExpression') {
18
+ arrayValue = {
19
+ type: 'ArrayExpression',
20
+ elements: []
21
+ };
22
+
23
+ if (node.value.type === 'CallExpression') {
24
+ arrayValue.elements.push(/** @type {import("estree").Expression} */ (node.value));
25
+ }
26
+
27
+ if (node.value.type === 'Identifier') arrayValue.elements.push(node.value);
28
+ } else {
29
+ arrayValue = /** @type {import("estree").ArrayExpression} */ (node.value);
30
+ }
31
+
32
+ if (
33
+ !arrayValue.elements.some(
34
+ (el) =>
35
+ el && // not null
36
+ el.type === 'CallExpression' && // is function call
37
+ el.callee.type == 'Identifier' && // has name
38
+ el.callee.name === importName // name is correct import
39
+ )
40
+ ) {
41
+ // Add the call
42
+ arrayValue.elements.push({
43
+ type: 'CallExpression',
44
+ callee: {
45
+ type: 'Identifier',
46
+ name: importName
47
+ },
48
+ arguments: [],
49
+ optional: false
50
+ });
51
+ }
52
+
53
+ node.value = arrayValue;
54
+ };
55
+
56
+ export const updateSvelteConfig = async () => {
57
+ const svelteConfig = await findFile('svelte.config.ts')
58
+ .catch(() => findFile('svelte.config.js'))
59
+ .catch(() => {
60
+ throw new Error(
61
+ 'Could not find a valid svelte configuration file (svelte.config.js, svelte.config.ts)'
62
+ );
63
+ });
64
+ const svelteConfigContent = await fs.readFile(svelteConfig, 'utf-8');
65
+ /** @type {{program: import("estree").Program}} */
66
+ const { program: svelteConfigAst } = await parse(svelteConfigContent);
67
+
68
+ let foundImport = false;
69
+ let importName = 'evidenceSvelte';
70
+
71
+ let foundPreprocess = false;
72
+
73
+ visit(svelteConfigAst, {
74
+ visitImportDeclaration({ node }) {
75
+ if (node.source.value !== '@evidence-dev/sdk')
76
+ // TODO: Update this package title
77
+ return false;
78
+
79
+ const importValue = node.specifiers?.find(
80
+ (spec) => spec.type === 'ImportSpecifier' && spec.imported.name === 'evidenceSvelte'
81
+ );
82
+ if (importValue && importValue.type === 'ImportSpecifier') {
83
+ foundImport = true;
84
+
85
+ if (importValue.local) {
86
+ let v = importValue.local.name;
87
+ while (typeof v !== 'string') v = v.name;
88
+ importName = v;
89
+ } else {
90
+ let v = importValue.imported.name;
91
+ while (typeof v !== 'string') v = v.name;
92
+ importName = v;
93
+ }
94
+
95
+ return false;
96
+ } else if (importValue) {
97
+ throw new EvidenceError('Failed to add needed imports to svelte configuration');
98
+ }
99
+ return false;
100
+ },
101
+ visitProperty({ node }) {
102
+ if (
103
+ node.type === 'Property' &&
104
+ node.key.type === 'Identifier' &&
105
+ node.key.name === 'preprocess'
106
+ ) {
107
+ foundPreprocess = true;
108
+ insertPreprocessor(/** @type {import("estree").Property} */ (node), importName);
109
+ }
110
+ return false;
111
+ }
112
+ });
113
+
114
+ if (!foundImport) {
115
+ svelteConfigAst.body = [
116
+ {
117
+ type: 'ImportDeclaration',
118
+ specifiers: [
119
+ {
120
+ type: 'ImportSpecifier',
121
+ imported: { type: 'Identifier', name: importName },
122
+ local: { type: 'Identifier', name: importName }
123
+ }
124
+ ],
125
+ source: {
126
+ type: 'Literal',
127
+ value: '@evidence-dev/sdk'
128
+ }
129
+ },
130
+ ...svelteConfigAst.body
131
+ ];
132
+ }
133
+ if (!foundPreprocess) {
134
+ let updatedPreprocess = false;
135
+ visit(svelteConfigAst, {
136
+ visitVariableDeclaration({ node }) {
137
+ const config =
138
+ /** @type {import("estree").VariableDeclarator | undefined} */
139
+ (
140
+ node.declarations.find(
141
+ (dec) =>
142
+ dec.type === 'VariableDeclarator' && // declaring a variable
143
+ dec.id.type === 'Identifier' && // id is an id I guess
144
+ dec.id.name === 'config' // variable is named config
145
+ )
146
+ );
147
+ if (!config) return false;
148
+ if (!config.init) return false;
149
+ if (config.init.type !== 'ObjectExpression') return false; // TODO: How to handle this case?
150
+
151
+ const cfgObj = config.init;
152
+ if (
153
+ cfgObj.properties.some(
154
+ (prop) =>
155
+ prop.type === 'Property' &&
156
+ prop.key.type === 'Identifier' &&
157
+ prop.key.name === 'preprocess'
158
+ )
159
+ ) {
160
+ // Something weird is happening in this case, preprocess shouldn't exist right now
161
+ return false;
162
+ }
163
+ cfgObj.properties.push({
164
+ kind: 'init',
165
+ method: false,
166
+ shorthand: false,
167
+ computed: false,
168
+
169
+ type: 'Property',
170
+ key: {
171
+ type: 'Identifier',
172
+ name: 'preprocess'
173
+ },
174
+ value: {
175
+ type: 'ArrayExpression',
176
+ elements: [
177
+ {
178
+ type: 'CallExpression',
179
+ callee: {
180
+ type: 'Identifier',
181
+ name: importName
182
+ },
183
+ optional: false,
184
+ arguments: []
185
+ }
186
+ ]
187
+ }
188
+ });
189
+ updatedPreprocess = true;
190
+
191
+ return false;
192
+ }
193
+ });
194
+ if (!updatedPreprocess)
195
+ throw new EvidenceError('Failed to insert preprocessor to svelte configuration');
196
+ }
197
+
198
+ await fs.writeFile(svelteConfig, prettyPrint(svelteConfigAst).code);
199
+ };
@@ -0,0 +1,152 @@
1
+ import { findFile } from 'pkg-types';
2
+ import { parse, prettyPrint, visit } from 'recast';
3
+ import fs from 'fs/promises';
4
+ import { EvidenceError } from '../../../lib/EvidenceError.js';
5
+
6
+ export const updateViteConfig = async () => {
7
+ const viteConfig = await findFile('vite.config.ts')
8
+ .catch(() => findFile('vite.config.js'))
9
+ .catch(() => {
10
+ throw new EvidenceError(
11
+ 'Could not find a valid vite configuration file (vite.config.js, vite.config.ts)'
12
+ );
13
+ });
14
+ const viteConfigContent = await fs.readFile(viteConfig, 'utf-8');
15
+ /** @type {{program: import("estree").Program}} */
16
+ const { program: viteConfigAst } = await parse(viteConfigContent);
17
+
18
+ let foundImport = false;
19
+ let importName = 'evidenceVite';
20
+
21
+ visit(viteConfigAst, {
22
+ visitImportDeclaration({ node }) {
23
+ if (node.source.value !== '@evidence-dev/sdk')
24
+ // TODO: Update this package title
25
+ return false;
26
+
27
+ const importValue = node.specifiers?.find(
28
+ (spec) => spec.type === 'ImportSpecifier' && spec.imported.name === 'evidenceVite'
29
+ );
30
+ if (importValue && importValue.type === 'ImportSpecifier') {
31
+ foundImport = true;
32
+
33
+ if (importValue.local) {
34
+ let v = importValue.local.name;
35
+ while (typeof v !== 'string') v = v.name;
36
+ importName = v;
37
+ } else {
38
+ let v = importValue.imported.name;
39
+ while (typeof v !== 'string') v = v.name;
40
+ importName = v;
41
+ }
42
+
43
+ return false;
44
+ } else if (importValue) {
45
+ throw new EvidenceError(
46
+ 'Failed to add needed imports to vite configuration',
47
+ 'You may need to install Evidence manually'
48
+ ); // TODO: Docs page
49
+ }
50
+ return false;
51
+ },
52
+ visitCallExpression({ node }) {
53
+ // TODO: Is there any sense in tracking down a plugin that is declared outside the defineConfig?
54
+ // I think not, if they are at that point they can set it up themselves
55
+ if (node.callee.type !== 'Identifier') return false;
56
+ if (node.callee.name !== 'defineConfig') return false;
57
+
58
+ /** @type {import("estree").ObjectExpression} */
59
+ let opts;
60
+ if (node.arguments.length === 0)
61
+ node.arguments.push({
62
+ type: 'ObjectExpression',
63
+ properties: []
64
+ });
65
+ if (node.arguments[0].type !== 'ObjectExpression') {
66
+ throw new EvidenceError(
67
+ 'Cannot update vite.config.js, unexpected argument found in defineConfig',
68
+ 'You may need to install Evidence manually'
69
+ ); // TODO: Docs page
70
+ }
71
+ opts = /** @type {import("estree").ObjectExpression} */ (node.arguments[0]);
72
+ if (
73
+ !opts.properties.some(
74
+ (prop) =>
75
+ prop.type === 'Property' && prop.key.type === 'Identifier' && prop.key.name == 'plugins'
76
+ )
77
+ ) {
78
+ // Add plugins key
79
+ opts.properties.push({
80
+ type: 'Property',
81
+ kind: 'init',
82
+ method: false,
83
+ shorthand: false,
84
+ computed: false,
85
+ key: {
86
+ type: 'Identifier',
87
+ name: 'plugins'
88
+ },
89
+ value: {
90
+ type: 'ArrayExpression',
91
+ elements: []
92
+ }
93
+ });
94
+ }
95
+
96
+ const pluginArray = /** @type {import("estree").ArrayExpression} */ (
97
+ /** @type {import("estree").Property} */ (
98
+ opts.properties.find(
99
+ (prop) =>
100
+ prop.type === 'Property' &&
101
+ prop.key.type === 'Identifier' &&
102
+ prop.key.name === 'plugins' &&
103
+ prop.value.type === 'ArrayExpression'
104
+ )
105
+ )?.value
106
+ );
107
+
108
+ const hasCall = pluginArray.elements.some(
109
+ (el) =>
110
+ el &&
111
+ el.type === 'CallExpression' &&
112
+ el.callee.type === 'Identifier' &&
113
+ el.callee.name === importName
114
+ );
115
+ if (!hasCall) {
116
+ pluginArray.elements.push({
117
+ type: 'CallExpression',
118
+ callee: {
119
+ type: 'Identifier',
120
+ name: importName
121
+ },
122
+ arguments: [],
123
+ optional: false
124
+ });
125
+ }
126
+
127
+ return false;
128
+ }
129
+ });
130
+
131
+ if (!foundImport) {
132
+ viteConfigAst.body = [
133
+ {
134
+ type: 'ImportDeclaration',
135
+ specifiers: [
136
+ {
137
+ type: 'ImportSpecifier',
138
+ imported: { type: 'Identifier', name: importName },
139
+ local: { type: 'Identifier', name: importName }
140
+ }
141
+ ],
142
+ source: {
143
+ type: 'Literal',
144
+ value: '@evidence-dev/sdk'
145
+ }
146
+ },
147
+ ...viteConfigAst.body
148
+ ];
149
+ }
150
+
151
+ await fs.writeFile(viteConfig, prettyPrint(viteConfigAst).code);
152
+ };
@@ -0,0 +1,95 @@
1
+ import * as prompt from '@clack/prompts';
2
+ import { updateSvelteConfig } from './add/updateSvelteConfig.js';
3
+ import { updatePackageJson } from './add/updatePackageJson.js';
4
+ import { updateViteConfig } from './add/updateViteConfig.js';
5
+ import { createEvidenceConfig } from '../../configuration/createEvidenceConfig.js';
6
+ import { updateGitIgnore } from './add/updateGitIgnore.js';
7
+ import { addTypeRef } from './add/addTypeRef.js';
8
+
9
+ import fs from 'fs/promises';
10
+ import path from 'path';
11
+ import { installPluginPrompt } from '../../plugins/cli/install.prompt.js';
12
+
13
+ import chalk from 'chalk';
14
+ import { projectRoot } from '../../lib/projectRoot.js';
15
+ import { addServerHook } from './add/addServerHook.js';
16
+ import { EvidenceError } from '../../lib/EvidenceError.js';
17
+
18
+ /** @type {import("@brianmd/citty").CommandDef} */
19
+ export const add = {
20
+ meta: {
21
+ name: 'add',
22
+ description: 'Add evidence to your Svelte project'
23
+ },
24
+ async run() {
25
+ prompt.intro('Installing Evidence SDK in your project');
26
+ await updateGitIgnore();
27
+ prompt.log.step('Updated .gitignore');
28
+ await updatePackageJson();
29
+ prompt.log.step('Updated package.json');
30
+ await updateSvelteConfig();
31
+ prompt.log.step('Updated svelte configuration');
32
+ await updateViteConfig();
33
+ prompt.log.step('Updated vite configuration');
34
+ await addTypeRef();
35
+ prompt.log.step('Added type reference');
36
+
37
+ // TODO: Detect if we are in a sveltekit project
38
+ await addServerHook();
39
+ prompt.log.step('Server hook configured');
40
+ await createEvidenceConfig()
41
+ .then(() => {
42
+ prompt.log.step('Created evidence.config.yaml');
43
+ })
44
+ .catch((e) => {
45
+ if (e instanceof EvidenceError && e.message.includes('Configuration file already exists')) {
46
+ console.debug(chalk.dim('Did not create evidence.config.yaml, already exists'));
47
+ } else {
48
+ throw e;
49
+ }
50
+ });
51
+
52
+ await fs.mkdir(path.resolve(projectRoot, 'sources'), { recursive: true });
53
+
54
+ prompt.log.step('Created sources directory');
55
+
56
+ const installSources = await prompt.confirm({
57
+ message: 'Would you like to install a datasource?'
58
+ });
59
+ if (installSources === true) {
60
+ const result = await installPluginPrompt(true, 'datasource', true);
61
+ if (result) {
62
+ prompt.log.info(
63
+ `Plugin installed! Run ${chalk.bold('npm i')} to install it and ${chalk.bold(
64
+ 'npx evidence-sdk connections edit'
65
+ )} to set up a connection.`
66
+ );
67
+ } else {
68
+ prompt.log.warn(
69
+ `You did not install any plugins; install one manually, or use ${chalk.bold(
70
+ 'npx evidence-sdk plugins install'
71
+ )} for an interactive helper.`
72
+ );
73
+ }
74
+ } else {
75
+ prompt.log.warn(
76
+ `You did not install any plugins; install one manually, or use ${chalk.bold(
77
+ 'npx evidence-sdk plugins install'
78
+ )} for an interactive helper.`
79
+ );
80
+ }
81
+
82
+ prompt.log.info(
83
+ [
84
+ chalk.bold('Thanks for trying out the Evidence SDK Preview!'),
85
+ 'Please note that the interfaces in the preview are unstable, and bugs are to be expected',
86
+ 'We do not consider this ready for production yet.',
87
+ 'If you encounter issues, you can report them on GitHub:' +
88
+ chalk.bold('https://github.com/evidence-dev/sdk/issues/new'),
89
+ chalk.green("Happy SQL'ing! 🎉")
90
+ ].join('\n')
91
+ );
92
+
93
+ prompt.outro('Evidence SDK Installed!');
94
+ }
95
+ };
@@ -0,0 +1,30 @@
1
+ import { loadEnv } from 'vite';
2
+ import { evidenceProjectOnlyNotice } from '../../lib/cli/notices.js';
3
+ import path from 'path';
4
+ import { projectRoot } from '../../lib/projectRoot.js';
5
+ import { copyToLayout } from '../../plugins/layouts/copyToLayout.js';
6
+ import { build as viteBuild } from 'vite';
7
+
8
+ /** @type {import("@brianmd/citty").CommandDef} */
9
+ export const build = evidenceProjectOnlyNotice('build', {
10
+ meta: {
11
+ name: 'build',
12
+ description: 'Build your evidence project'
13
+ },
14
+ async run() {
15
+ const loadedEnv = loadEnv('', '.', ['VITE_', 'EVIDENCE_']);
16
+ Object.assign(process.env, loadedEnv);
17
+
18
+ await copyToLayout();
19
+ let templateRoot = path.join(projectRoot, '.evidence', 'template');
20
+
21
+ process.chdir(templateRoot);
22
+
23
+ await viteBuild({
24
+ build: {
25
+ emptyOutDir: true,
26
+ outDir: '../../build'
27
+ }
28
+ });
29
+ }
30
+ });
@@ -0,0 +1,49 @@
1
+ import { evidenceProjectOnlyNotice } from '../../lib/cli/notices.js';
2
+ import { createServer, loadEnv } from 'vite';
3
+ import chalk from 'chalk';
4
+ import { copyToLayout } from '../../plugins/layouts/copyToLayout.js';
5
+ import path from 'path';
6
+ import { projectRoot } from '../../lib/projectRoot.js';
7
+
8
+ /** @type {import("@brianmd/citty").CommandDef} */
9
+ export const dev = evidenceProjectOnlyNotice('dev', {
10
+ meta: {
11
+ name: 'dev',
12
+ description: 'Run the evidence dev server'
13
+ },
14
+ async run({ rawArgs }) {
15
+ // TODO: Should we use vite's built in loadEnv function?
16
+
17
+ const loadedEnv = loadEnv('', '.', ['VITE_', 'EVIDENCE_']);
18
+ Object.assign(process.env, loadedEnv);
19
+
20
+ console.clear();
21
+ await copyToLayout();
22
+
23
+ let templateRoot = path.join(projectRoot, '.evidence', 'template');
24
+
25
+ process.chdir(templateRoot);
26
+
27
+ /** @type {import("vite").InlineConfig} */
28
+ const viteConfig = {
29
+ server: {
30
+ host: rawArgs.includes('--host')
31
+ }
32
+ };
33
+
34
+ const server = await createServer(viteConfig);
35
+
36
+ await server.listen(3000);
37
+
38
+ console.log(chalk.dim(`Running in ${process.cwd()}`));
39
+
40
+ console.log(chalk.bold(' Your Evidence project has started!'));
41
+ console.log('');
42
+ console.log(' You can access it at the following URLs:');
43
+
44
+ server.printUrls();
45
+
46
+ // TODO: We could add a keyboard shortcut here for running sources; might be cute
47
+ server.bindCLIShortcuts({ print: true });
48
+ }
49
+ });
@@ -0,0 +1,14 @@
1
+ import { add } from './add.js';
2
+ import { build } from './build.js';
3
+ import { dev } from './dev.js';
4
+ import { upgrade } from './upgrade.js';
5
+
6
+ /**
7
+ * @type {import("@brianmd/citty").SubCommandsDef}
8
+ */
9
+ export const buildDevCli = {
10
+ add,
11
+ build,
12
+ dev,
13
+ upgrade
14
+ };
@@ -0,0 +1,54 @@
1
+ import { evidenceProjectOnlyNotice } from '../../lib/cli/notices.js';
2
+ import chalk from 'chalk';
3
+ import { spawn } from 'child_process';
4
+ import { intro, outro, spinner } from '@clack/prompts';
5
+
6
+ function upgradePackages() {
7
+ return /** @type {Promise<void>} */ (
8
+ new Promise((resolve, reject) => {
9
+ const child = spawn(
10
+ 'npm install @evidence-dev/evidence@latest @evidence-dev/core-components@latest @evidence-dev/component-utilities@latest @evidence-dev/sdk@latest @evidence-dev/tailwind@latest',
11
+ {
12
+ shell: true,
13
+ detached: false,
14
+ stdio: 'ignore'
15
+ }
16
+ );
17
+
18
+ child.on('exit', function (code) {
19
+ if (code === 0) {
20
+ resolve();
21
+ } else {
22
+ reject(new Error(`Process exited with code ${code}`));
23
+ }
24
+ });
25
+
26
+ child.on('error', function (err) {
27
+ reject(err);
28
+ });
29
+ })
30
+ );
31
+ }
32
+
33
+ /** @type {import("@brianmd/citty").CommandDef} */
34
+ export const upgrade = evidenceProjectOnlyNotice('upgrade', {
35
+ meta: {
36
+ name: 'upgrade',
37
+ description: 'Upgrade your evidence project'
38
+ },
39
+ async run() {
40
+ intro(chalk.inverse(' Upgrading Evidence '));
41
+
42
+ const s = spinner();
43
+ s.start('Installing via npm');
44
+
45
+ upgradePackages()
46
+ .then(() => {
47
+ s.stop('Installed via npm');
48
+ outro(chalk.greenBright(`Done`));
49
+ })
50
+ .catch((err) => {
51
+ s.stop(chalk.redBright(`Error: ${err.message}`), 1);
52
+ });
53
+ }
54
+ });
@@ -0,0 +1,54 @@
1
+ import seqPreprocessor from 'svelte-sequential-preprocessor';
2
+
3
+ import { addScriptTags } from './processors/addScriptTags.js';
4
+ import { convertToHtml } from './processors/convertToHtml/convertToHtml.js';
5
+ import { extractQueries } from './processors/extractQueries/extractQueries.js';
6
+ import { initUniversalSql } from './processors/initializeUniversalSql.js';
7
+ import { injectHighlightStyles } from './processors/injectHighlightStyles.js';
8
+ import { injectQueries } from './processors/injectQueries/injectQueries.js';
9
+ import { obfuscateCode } from './processors/obfuscateCode/obfuscateCode.js';
10
+ import { transformQueries } from './processors/transformQueries/transformQueries.js';
11
+ import { highlight } from './processors/highlight/highlight.js';
12
+ import { injectComponents } from './processors/injectComponents/injectComponents.js';
13
+ import { addBasePathToHrefAndSrc } from './processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.js';
14
+ /**
15
+ * @typedef {Object} EvidencePreprocessOptions
16
+ * @property {boolean} [escapeCode = true] If false, disables the b64 escaping of codeblock content
17
+ * @property {boolean} [highlight = true] If false, disables the auto highlight of code blocks
18
+ */
19
+
20
+ /**
21
+ * @param {EvidencePreprocessOptions} opts
22
+ * @returns {import("svelte/types/compiler/preprocess").PreprocessorGroup}
23
+ */
24
+ export const svelte = (opts = {}) => {
25
+ /** @type {Array<import('svelte/types/compiler/preprocess').PreprocessorGroup>} */
26
+ const processors = [];
27
+ processors.push(convertToHtml);
28
+ processors.push(addScriptTags);
29
+ processors.push(extractQueries);
30
+ if (opts.highlight !== false) processors.push(highlight);
31
+ if (opts.escapeCode !== false) processors.push(obfuscateCode);
32
+ processors.push(transformQueries);
33
+ processors.push(injectHighlightStyles);
34
+ processors.push(injectQueries);
35
+ processors.push(initUniversalSql);
36
+ processors.push(injectComponents());
37
+ processors.push(addBasePathToHrefAndSrc);
38
+
39
+ // @ts-expect-error Typescript is upset about this for some reason
40
+ return seqPreprocessor(processors);
41
+ };
42
+
43
+ export {
44
+ addScriptTags,
45
+ convertToHtml,
46
+ extractQueries,
47
+ highlight,
48
+ injectHighlightStyles,
49
+ injectQueries,
50
+ initUniversalSql,
51
+ injectComponents,
52
+ transformQueries,
53
+ addBasePathToHrefAndSrc as addBasePathToHrefAndSrc
54
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ *
3
+ * @param {string | undefined} filename
4
+ * @returns
5
+ */
6
+ export const isTargetFile = (filename) => filename?.split('/').at(-1)?.startsWith('+');
@@ -0,0 +1,39 @@
1
+ /** @typedef {import("../../types/index.js").FileMetadata} FileMetadata */
2
+
3
+ /** @type {Map<string, FileMetadata>} */
4
+ const metadatas = new Map();
5
+
6
+ /**
7
+ * @param {string} filename
8
+ * @returns {FileMetadata | undefined}
9
+ */
10
+ export const getFileMetadata = (filename) => metadatas.get(filename);
11
+
12
+ /**
13
+ * @param {string} filename
14
+ * @param {FileMetadata} metadata
15
+ * @returns {void}
16
+ */
17
+ export const addFileMetadata = (filename, metadata) => {
18
+ metadatas.set(filename, Object.assign(metadatas.get(filename) ?? {}, metadata));
19
+ };
20
+
21
+ /**
22
+ * @param {string} filename
23
+ * @param {FileMetadata} metadata
24
+ * @returns {void}
25
+ */
26
+ export const setFileMetadata = (filename, metadata) => {
27
+ metadatas.set(filename, metadata);
28
+ };
29
+
30
+ /**
31
+ * @returns {void}
32
+ */
33
+ export const clearFileMetadatas = () => {
34
+ if (process.env.NODE_ENV !== 'test')
35
+ console.warn('clearFileMetadatas is only intended to be used during testing');
36
+ metadatas.clear();
37
+ };
38
+
39
+ export const getAllFileMetadatas = () => Object.fromEntries(metadatas.entries());