@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,496 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ Options,
4
+ OptionSpecMode,
5
+ getSecretOptions,
6
+ getSafeOptions,
7
+ OptionParentMode,
8
+ getSpecAtPath
9
+ } from './Options.js';
10
+
11
+ /** @type {import("../../Datasources.js").Datasource["options"]} */
12
+ const secSpec = {
13
+ opt: { secret: false, title: 'opt' },
14
+ secret: { secret: true }
15
+ };
16
+ /** @type {import("../../Datasources.js").Datasource["options"]} */
17
+ const secSpecAlt = {
18
+ _opt: { secret: false, title: '_opt' },
19
+ _secret: { secret: true }
20
+ };
21
+ /** @type {import("../../Datasources.js").Datasource["options"]} */
22
+ const secSpecAlt2 = {
23
+ __opt: { secret: false },
24
+ __secret: { secret: true }
25
+ };
26
+
27
+ /** @type {import("../../Datasources.js").Datasource["options"]} */
28
+ const testSpec = {
29
+ ...secSpec,
30
+ nested: {
31
+ nest: true,
32
+ children: {
33
+ a: secSpec,
34
+ b: secSpecAlt,
35
+ double: {
36
+ v: {
37
+ nest: true,
38
+ children: {
39
+ a: secSpec,
40
+ b: secSpecAlt
41
+ }
42
+ }
43
+ },
44
+ unnested: {
45
+ nest: false,
46
+ children: {
47
+ a: secSpecAlt,
48
+ b: secSpecAlt2
49
+ }
50
+ }
51
+ }
52
+ },
53
+ unnested: {
54
+ nest: false,
55
+ children: {
56
+ a: secSpecAlt,
57
+ b: secSpecAlt2
58
+ }
59
+ }
60
+ };
61
+
62
+ const complexSpec = {
63
+ project_id: {
64
+ title: 'Project ID',
65
+ type: 'string',
66
+ secret: true,
67
+ virtual: false,
68
+ references: '$.keyfile.project_id',
69
+ forceReference: false,
70
+ required: true
71
+ },
72
+ location: {
73
+ title: 'Location (Region)',
74
+ type: 'string',
75
+ secret: false,
76
+ virtual: false,
77
+ forceReference: false,
78
+ required: false,
79
+ default: 'US'
80
+ },
81
+ authenticator: {
82
+ title: 'Authentication Method',
83
+ type: 'select',
84
+ secret: false,
85
+ virtual: false,
86
+ forceReference: false,
87
+ children: {
88
+ 'service-account': {
89
+ keyfile: {
90
+ title: 'Credentials File',
91
+ type: 'file',
92
+ secret: false,
93
+ virtual: true,
94
+ forceReference: false,
95
+ fileFormat: 'json',
96
+ required: false
97
+ },
98
+ client_email: {
99
+ title: 'Client Email',
100
+ type: 'string',
101
+ secret: true,
102
+ virtual: false,
103
+ references: '$.keyfile.client_email',
104
+ forceReference: true,
105
+ required: true
106
+ },
107
+ private_key: {
108
+ title: 'Private Key',
109
+ type: 'string',
110
+ secret: true,
111
+ virtual: false,
112
+ references: '$.keyfile.private_key',
113
+ forceReference: true,
114
+ required: true
115
+ }
116
+ },
117
+ 'gcloud-cli': {},
118
+ oauth: {
119
+ token: {
120
+ title: 'Token',
121
+ type: 'string',
122
+ secret: true,
123
+ virtual: false,
124
+ forceReference: false,
125
+ required: true
126
+ }
127
+ }
128
+ },
129
+ required: true,
130
+ options: [
131
+ { value: 'service-account', label: 'Service Account' },
132
+ { value: 'gcloud-cli', label: 'GCloud CLI' },
133
+ { value: 'oauth', label: 'OAuth Access Token' }
134
+ ],
135
+ nest: false,
136
+ default: 'service-account'
137
+ }
138
+ };
139
+ const complexOpts = {
140
+ location: 'US',
141
+ authenticator: 'service-account',
142
+ project_id: 'deadbeef-project',
143
+ keyfile: {
144
+ type: 'service_account',
145
+ project_id: 'deadbeef-project',
146
+ private_key_id: 'deadbeef',
147
+ private_key: 'my private key',
148
+ client_email: 'client@email.com',
149
+ client_id: '42',
150
+ auth_uri: 'https://evidence.dev',
151
+ token_uri: 'https://evidence.dev',
152
+ auth_provider_x509_cert_url: 'https://evidence.dev',
153
+ client_x509_cert_url: 'https://evidence.dev'
154
+ },
155
+ client_email: 'https://evidence.dev',
156
+ private_key: 'my private key'
157
+ };
158
+
159
+ describe('Options', () => {
160
+ describe('get', () => {
161
+ describe('root-level', () => {
162
+ it('should get a value', () => {
163
+ const opts = { opt: 'hi', secret: false };
164
+
165
+ const options = Options(testSpec, opts);
166
+ expect(options.opt).toBe('hi');
167
+ });
168
+ it.skip('should throw for a non-defined option', () => {
169
+ const opts = { opt: 'hi', secret: false };
170
+
171
+ const options = Options(testSpec, opts);
172
+ expect(() => options.notReal).toThrowError();
173
+ });
174
+ });
175
+ describe('children, nested', () => {
176
+ it('should get a value', () => {
177
+ const opts = { _nested: 'a', nested: { opt: 'hi' } };
178
+ const options = Options(testSpec, opts);
179
+
180
+ expect(options.nested.opt).toBe('hi');
181
+ });
182
+ it('should allow you to get the value of the field', () => {
183
+ const opts = { _nested: 'a', nested: { opt: 'hi' } };
184
+ const options = Options(testSpec, opts);
185
+
186
+ expect(options[OptionParentMode].nested).toBe('a');
187
+ });
188
+
189
+ it.skip('should throw for a non-defined option', () => {
190
+ const opts = { _nested: 'a', nested: { opt: 'hi' } };
191
+ const options = Options(testSpec, opts);
192
+
193
+ expect(() => options.nested.notReal).toThrowError();
194
+ });
195
+ });
196
+ describe('children, unnested', () => {
197
+ it('should get a child value', () => {
198
+ const opts = { unnested: 'a', _opt: 'hi' };
199
+ const options = Options(testSpec, opts);
200
+
201
+ expect(options.unnested._opt).toBe('hi');
202
+ });
203
+ it('should allow you to get the value of the field', () => {
204
+ const opts = { unnested: 'a' };
205
+ const options = Options(testSpec, opts);
206
+
207
+ expect(options[OptionParentMode].unnested).toBe('a');
208
+ });
209
+
210
+ it.skip('should throw for a non-defined option', () => {
211
+ const opts = { _nested: 'a', nested: { opt: 'hi' } };
212
+ const options = Options(testSpec, opts);
213
+
214
+ expect(() => options.nested.notReal).toThrowError();
215
+ });
216
+ });
217
+ });
218
+
219
+ describe('set', () => {
220
+ describe('root-level', () => {
221
+ // Set Value
222
+ it('should properly set a base value', () => {
223
+ const opts = { opt: 'hi', secret: false };
224
+
225
+ const options = Options(testSpec, opts);
226
+ options.opt = 'bye';
227
+ expect(options.opt).toBe('bye');
228
+ });
229
+ // Set Child Value (nested)
230
+ // Set Child Value (un-nested)
231
+ });
232
+ describe('children, nested', () => {
233
+ it('should properly set a nested child value', () => {
234
+ const opts = { _nested: 'a', nested: { opt: 'hi' } };
235
+ const options = Options(testSpec, opts);
236
+ options.nested.opt = 'bye';
237
+
238
+ expect(options.nested.opt).toBe('bye');
239
+ });
240
+ it('should properly set a nested child value when the nested key does not exist', () => {
241
+ const opts = { _nested: 'a' };
242
+ const options = Options(testSpec, opts);
243
+ options.nested.opt = 'bye';
244
+
245
+ expect(options.nested.opt).toBe('bye');
246
+ });
247
+ it('should properly set a nested value, when the spec calls for it', () => {
248
+ const opts = { _nested: 'a' };
249
+ const options = Options(testSpec, opts);
250
+
251
+ options.nested = 'b';
252
+ options.nested = { _opt: 'bye' };
253
+
254
+ expect(options.nested._opt).toBe('bye');
255
+ });
256
+ it('should throw when setting a nested value to an object with invalid keys', () => {
257
+ const opts = { _nested: 'a' };
258
+ const options = Options(testSpec, opts);
259
+
260
+ // _opt is in the "b" spec, not the "a" spec
261
+ expect(() => (options.nested = { _opt: 'bye' })).toThrowError();
262
+ });
263
+ });
264
+ describe('children, unnested', () => {
265
+ it('should get a non-nested child value', () => {
266
+ const opts = { unnested: 'a', _opt: 'hi' };
267
+ const options = Options(testSpec, opts);
268
+
269
+ options.unnested._opt = 'bye';
270
+ expect(options.unnested._opt).toBe('bye');
271
+ });
272
+ });
273
+ describe('meta functions', () => {
274
+ it('should JSON stringify properly', () => {
275
+ const opts = {};
276
+ const options = Options(testSpec, opts);
277
+
278
+ options.nested = 'a';
279
+ options.nested.opt = 'nested.opt';
280
+ options.nested.secret = 'nested.secret';
281
+ options.unnested = 'a';
282
+ options.unnested._opt = 'unnested.opt';
283
+ options.unnested._secret = 'unnested.secret';
284
+
285
+ expect(JSON.parse(JSON.stringify(options))).toEqual(JSON.parse(JSON.stringify(opts)));
286
+ });
287
+ it('should return keys from the spec, not the object', () => {
288
+ const opts = { x: 1, y: 2, unnested: 'a', _nested: 'a', nested: { opt: 'hi' } };
289
+ const options = Options(testSpec, opts);
290
+
291
+ expect(Object.keys(options)).toEqual(['nested', 'unnested']);
292
+ });
293
+ });
294
+ });
295
+
296
+ describe('spec mode', () => {
297
+ it('should return the field spec from a field if in spec mode', () => {
298
+ const opts = {};
299
+ const options = Options(testSpec, opts, { specMode: true });
300
+
301
+ expect(options.opt.secret).toBe(false);
302
+ expect(options.secret.secret).toBe(true);
303
+ });
304
+ it('should return the field spec from a field if toggled into spec mode', () => {
305
+ const opts = {};
306
+ const options = Options(testSpec, opts);
307
+
308
+ expect(options[OptionSpecMode].opt.secret).toBe(false);
309
+ expect(options[OptionSpecMode].secret.secret).toBe(true);
310
+ });
311
+ it('should return the field spec from a field if in spec mode', () => {
312
+ const opts = { _nested: 'a' };
313
+ const options = Options(testSpec, opts);
314
+
315
+ expect(options[OptionSpecMode].nested.opt.secret).toBe(false);
316
+ expect(options[OptionSpecMode].nested.secret.secret).toBe(true);
317
+ });
318
+ it('should return the field spec from a field if toggled into spec mode at the root level', () => {
319
+ const opts = { _nested: 'a' };
320
+ const options = Options(testSpec, opts);
321
+
322
+ expect(options[OptionSpecMode].nested.opt.secret).toBe(false);
323
+ expect(options[OptionSpecMode].nested.secret.secret).toBe(true);
324
+ });
325
+ it('should return the field spec for a child field', () => {
326
+ const opts = { _nested: 'a' };
327
+ const options = Options(testSpec, opts);
328
+
329
+ expect(options[OptionSpecMode].nested.opt.secret).toBe(false);
330
+ });
331
+ it('should return the field spec from a field if toggled into spec mode at a nested level', () => {
332
+ const opts = { _nested: 'a' };
333
+ const options = Options(testSpec, opts);
334
+
335
+ expect(options[OptionSpecMode].nested.opt.secret).toBe(false);
336
+ expect(options[OptionSpecMode].nested.secret.secret).toBe(true);
337
+ });
338
+ it('should return the field spec from a field if toggled into spec mode at an unested level', () => {
339
+ const opts = { unnested: 'a' };
340
+ const options = Options(testSpec, opts);
341
+
342
+ expect(options[OptionSpecMode].unnested._opt.secret).toBe(false);
343
+ expect(options[OptionSpecMode].unnested._secret.secret).toBe(true);
344
+ });
345
+ });
346
+ });
347
+
348
+ describe('getSecretOptions', () => {
349
+ it('should return secret values', () => {
350
+ const opts = {};
351
+ const options = Options(testSpec, opts);
352
+ options.secret = 'Hi!';
353
+
354
+ const secrets = getSecretOptions(options);
355
+ expect(secrets.secret).toEqual('Hi!');
356
+ });
357
+ it('should not return non-secret values', () => {
358
+ const opts = {};
359
+ const options = Options(testSpec, opts);
360
+ options.opt = 'Hi!';
361
+ const secrets = getSecretOptions(options);
362
+ expect(secrets.opt).toBeUndefined();
363
+ });
364
+
365
+ it('should return nested secret values', () => {
366
+ const opts = {};
367
+ const options = Options(testSpec, opts);
368
+ options.nested = 'a';
369
+ options.nested.secret = 'Hi!';
370
+ options.nested.opt = 'Bye!';
371
+
372
+ const secrets = getSecretOptions(options);
373
+
374
+ expect(secrets.nested.secret).toEqual('Hi!');
375
+ expect(secrets.nested.opt).toBeUndefined();
376
+ expect(secrets._nested).toBeUndefined(); // _nested itself is not a secret value; and does not belong here
377
+ expect(secrets).toEqual({ nested: { secret: 'Hi!' } });
378
+ });
379
+
380
+ it('should return non-nested secret values', () => {
381
+ const opts = {};
382
+ const options = Options(testSpec, opts);
383
+ options.unnested = 'a';
384
+ options.unnested._secret = 'Hi!';
385
+ options.unnested._opt = 'Bye!';
386
+
387
+ const secrets = getSecretOptions(options);
388
+
389
+ expect(secrets._secret).toEqual('Hi!');
390
+ expect(secrets._opt).toBeUndefined();
391
+ expect(secrets.unnested).toBeUndefined();
392
+ expect(secrets).toEqual({ _secret: 'Hi!' });
393
+ });
394
+
395
+ it('should ignore virtual fields', () => {
396
+ const options = Options(complexSpec, complexOpts);
397
+
398
+ const secrets = getSecretOptions(options);
399
+
400
+ expect(secrets).toEqual({
401
+ client_email: 'https://evidence.dev',
402
+ private_key: 'my private key',
403
+ project_id: 'deadbeef-project'
404
+ });
405
+ });
406
+ });
407
+ describe('getSafeOptions', () => {
408
+ it('should return safe values', () => {
409
+ const opts = {};
410
+ const options = Options(testSpec, opts);
411
+ options.secret = 'Hi!';
412
+
413
+ const safes = getSafeOptions(options);
414
+ expect(safes.secret).toBeUndefined();
415
+ });
416
+ it('should not return secret values', () => {
417
+ const opts = {};
418
+ const options = Options(testSpec, opts);
419
+ options.opt = 'Hi!';
420
+ const safes = getSafeOptions(options);
421
+ expect(safes.opt).toEqual('Hi!');
422
+ });
423
+
424
+ it('should return nested safe values', () => {
425
+ const opts = {};
426
+ const options = Options(testSpec, opts);
427
+ options.nested = 'a';
428
+ options.nested.secret = 'Hi!';
429
+ options.nested.opt = 'Bye!';
430
+
431
+ const safes = getSafeOptions(options);
432
+
433
+ expect(safes.nested.secret).toBeUndefined();
434
+ expect(safes.nested.opt).toEqual('Bye!');
435
+ expect(safes._nested).toEqual('a');
436
+ expect(safes).toEqual({ nested: { opt: 'Bye!' }, _nested: 'a' });
437
+ });
438
+
439
+ it('should return non-nested safe values', () => {
440
+ const opts = {};
441
+ const options = Options(testSpec, opts);
442
+ options.unnested = 'a';
443
+ options.unnested._secret = 'Hi!';
444
+ options.unnested._opt = 'Bye!';
445
+
446
+ const safes = getSafeOptions(options);
447
+
448
+ expect(safes._secret).toBeUndefined();
449
+ expect(safes._opt).toEqual('Bye!');
450
+ expect(safes.unnested).toEqual('a');
451
+ expect(safes).toEqual({ _opt: 'Bye!', unnested: 'a' });
452
+ });
453
+
454
+ it('should ignore virtual fields', () => {
455
+ const options = Options(complexSpec, complexOpts);
456
+
457
+ const safes = getSafeOptions(options);
458
+
459
+ expect(safes).toEqual({ authenticator: 'service-account', location: 'US' });
460
+ });
461
+ });
462
+
463
+ describe('getSpecAtPath', () => {
464
+ it('should get a root spec', () => {
465
+ const opts = {};
466
+ const options = Options(testSpec, opts);
467
+
468
+ expect(getSpecAtPath(options, ['opt'])).toEqual(secSpec['opt']);
469
+ });
470
+ it('should get a nested spec', () => {
471
+ const opts = { _nested: 'a' };
472
+ const options = Options(testSpec, opts);
473
+
474
+ expect(getSpecAtPath(options, ['nested'])).toEqual(testSpec.nested);
475
+ });
476
+ it('should get a nested spec', () => {
477
+ const opts = { _nested: 'a' };
478
+ const options = Options(testSpec, opts);
479
+
480
+ expect(getSpecAtPath(options, ['nested', 'opt'])).toEqual(secSpec['opt']);
481
+ });
482
+ it('should get a double nested spec', () => {
483
+ const opts = { _nested: 'double' };
484
+ const options = Options(testSpec, opts);
485
+
486
+ options.nested = 'double';
487
+
488
+ expect(getSpecAtPath(options, ['nested', 'v'])).toEqual(testSpec.nested.children.double.v);
489
+ });
490
+ it('should get an unnested spec', () => {
491
+ const opts = { unnested: 'a' };
492
+ const options = Options(testSpec, opts);
493
+
494
+ expect(getSpecAtPath(options, ['unnested', '_opt'])).toEqual(secSpecAlt['_opt']);
495
+ });
496
+ });
@@ -0,0 +1,201 @@
1
+ import chalk from 'chalk';
2
+ import * as prompt from '@clack/prompts';
3
+
4
+ import { EvidenceError } from '../../../../lib/EvidenceError.js';
5
+ import {
6
+ OptionGetSpec,
7
+ OptionParentMode,
8
+ OptionSpecMode,
9
+ Options,
10
+ getSpecAtPath
11
+ } from './Options.js';
12
+ import { dataDirectory, metaDirectory } from '../../../../lib/projectPaths.js';
13
+ import { evalSources } from '../../evalSources.js';
14
+ import { updateManifest } from '../../updateManifest.js';
15
+ import { writeSourceConfig } from '../../writeSourceConfig.js';
16
+
17
+ /**
18
+ * @returns {Promise<boolean>}
19
+ */
20
+ const validate = async () => {
21
+ // TODO: Validate spec
22
+ return true;
23
+ };
24
+
25
+ /**
26
+ * @param {*} options
27
+ * @param {string[]} optionPath
28
+ */
29
+ const getOptionValue = (options, optionPath) => {
30
+ if (!optionPath.length)
31
+ throw new EvidenceError('Internal Error', [
32
+ 'Attempted to lookup option that did not have a path'
33
+ ]);
34
+ let v = options;
35
+ for (const p of optionPath.slice(0, -1)) {
36
+ v = v[p];
37
+ }
38
+ const lastItem = optionPath.at(-1);
39
+ if (!lastItem)
40
+ throw new EvidenceError('Internal Error', [
41
+ 'optionPath was already validated to have length, but is now missing a final element. Wat?'
42
+ ]);
43
+ return v[lastItem];
44
+ };
45
+
46
+ /**
47
+ * @param {*} options
48
+ * @param {string[]} optionPath
49
+ * @param {string | number | boolean} value
50
+ */
51
+ const setOptionValue = (options, optionPath, value) => {
52
+ if (!optionPath.length)
53
+ throw new EvidenceError('Internal Error', [
54
+ 'Attempted to lookup option that did not have a path'
55
+ ]);
56
+ let v = options;
57
+ for (const p of optionPath.slice(0, -1)) {
58
+ v = v[p];
59
+ }
60
+ const lastItem = optionPath.at(-1);
61
+ if (!lastItem)
62
+ throw new EvidenceError('Internal Error', [
63
+ 'optionPath was already validated to have length, but is now missing a final element. Wat?'
64
+ ]);
65
+ v[lastItem] = value;
66
+ };
67
+
68
+ /**
69
+ * @param {*} options
70
+ * @param {string[]} optionPath
71
+ */
72
+ const editOption = async (options, optionPath) => {
73
+ const spec = getSpecAtPath(options, optionPath);
74
+
75
+ if (!spec)
76
+ throw new EvidenceError('Internal Error', ['Attempted to edit option that does not exist!']);
77
+ let v;
78
+ const message = `${chalk.bold(spec.title)} ${chalk.dim(`(${spec.description ?? ''})`)}`;
79
+
80
+ const initialValue = getOptionValue(options, optionPath) ?? spec.default;
81
+ switch (spec.type) {
82
+ case 'string':
83
+ v = await prompt
84
+ .text({
85
+ initialValue: initialValue,
86
+ message: message
87
+ })
88
+ .then((r) => {
89
+ if (!prompt.isCancel(r)) return r.toString();
90
+ return r;
91
+ });
92
+ break;
93
+ case 'number':
94
+ v = await prompt
95
+ .text({
96
+ initialValue: initialValue.toString(),
97
+ message: message,
98
+ validate: (s) => {
99
+ if (!/^[\d]+$/.test(s)) return `${spec.title} must be a number`;
100
+ }
101
+ })
102
+ .then((r) => parseInt(r.toString()));
103
+ break;
104
+ case 'boolean':
105
+ v = await prompt.confirm({
106
+ initialValue: initialValue,
107
+ message: message
108
+ });
109
+ break;
110
+ case 'select':
111
+ if (!spec.options)
112
+ throw new EvidenceError('Internal Error', [
113
+ 'Attempted to edit a select option that has no choices!'
114
+ ]);
115
+ v = await prompt.select({
116
+ initialValue: initialValue,
117
+ message: message,
118
+ options: spec.options?.map((o) => (typeof o === 'string' ? { label: o, value: o } : o))
119
+ });
120
+ break;
121
+ case 'file':
122
+ break;
123
+ }
124
+
125
+ if (!prompt.isCancel(v)) setOptionValue(options, optionPath, v);
126
+ };
127
+
128
+ /**
129
+ * @param {*} options
130
+ * @param {string} [prefix]
131
+ * @returns {{label: string, value: string[]}[]}
132
+ */
133
+ const transformOptionsToSelectList = (options, prefix) => {
134
+ const output = [];
135
+ for (const [k, v] of Object.entries(options[OptionGetSpec])) {
136
+ const keyWithPrefix = prefix ? `${prefix}.${k}` : k;
137
+ const title = chalk.bold(keyWithPrefix);
138
+ const description = chalk.dim(`(${v.description ?? ''})`);
139
+ const shown = (v.secret && v.shown) || !v.secret;
140
+ const unsetMarker = v.required ? chalk.bold.red('unset') : chalk.yellow('unset');
141
+ const hasValue = options[k] || (v.default !== undefined && v.default !== null);
142
+
143
+ const currentValue =
144
+ (v.children ? options[OptionParentMode][k] : options[k]) ??
145
+ chalk.yellow(v.default) ??
146
+ unsetMarker;
147
+ const shownValue = shown ? `[${currentValue}]` : `[hidden, ${hasValue ? 'set' : unsetMarker}]`;
148
+ output.push({
149
+ label: `${title} ${shownValue} ${description}`,
150
+ value: keyWithPrefix.split('.')
151
+ });
152
+
153
+ if (v.children && typeof options[OptionSpecMode][k] === 'object') {
154
+ output.push(...transformOptionsToSelectList(options[k], keyWithPrefix));
155
+ }
156
+ }
157
+ return output;
158
+ };
159
+
160
+ /**
161
+ * @param {import("../../schemas/datasource.schema.js").DatasourceSpec & { dir: string }} source
162
+ * @param {import("../../Datasources.js").Datasource} plugin
163
+ */
164
+ export const editSourceConfig = async (source, plugin) => {
165
+ const options = Options(plugin.options, source.options);
166
+
167
+ // eslint-disable-next-line no-constant-condition
168
+ while (true) {
169
+ const selectItems = transformOptionsToSelectList(options);
170
+ const selection = await prompt.select({
171
+ message: 'Pick an option to configure',
172
+ options: [...selectItems, { label: 'Done', value: null }]
173
+ });
174
+
175
+ if (selection === null) {
176
+ const valid = await validate();
177
+ if (valid) break;
178
+ } else {
179
+ await editOption(options, selection);
180
+ }
181
+ }
182
+ await writeSourceConfig(options, source, prompt.spinner());
183
+
184
+ const runNow = await prompt.confirm({
185
+ message: `Would you like to run the source queries for ${source.name} now?`
186
+ });
187
+ if (runNow) {
188
+ const filter = {
189
+ sources: new Set([source.name]),
190
+ queries: new Set(),
191
+ only_changed: false
192
+ };
193
+ // Create some space to reduce the wackiness
194
+ console.log('\n\n');
195
+ // TODO: this is duplicated with the sources cli command, should be a shared func
196
+ const evaluatedManifest = await evalSources(dataDirectory, metaDirectory, filter);
197
+ await updateManifest(evaluatedManifest, dataDirectory);
198
+ }
199
+
200
+ return;
201
+ };
@@ -0,0 +1,20 @@
1
+ import * as prompt from '@clack/prompts';
2
+ import { editConnectionsPrompt } from './edit.prompt.js';
3
+
4
+ /** @type { import("@brianmd/citty").CommandDef } */
5
+ export const edit = {
6
+ meta: {
7
+ name: 'edit',
8
+ description: 'Add, remove, and update connections'
9
+ },
10
+
11
+ async run() {
12
+ prompt.intro('Update Datasource Connections');
13
+
14
+ await editConnectionsPrompt();
15
+
16
+ prompt.outro('Done!');
17
+
18
+ return true;
19
+ }
20
+ };