instant-cli 0.22.116-experimental.drewh-tanstack-start.21487748189.1 → 0.22.116-experimental.drewh-clief.21487748784.1

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 (105) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/index.d.ts +4 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +2 -2
  5. package/dist/index.js.map +1 -1
  6. package/dist/new/commands/init.d.ts +9 -0
  7. package/dist/new/commands/init.d.ts.map +1 -0
  8. package/dist/new/commands/init.js +9 -0
  9. package/dist/new/commands/init.js.map +1 -0
  10. package/dist/new/commands/initWithoutFiles.d.ts +10 -0
  11. package/dist/new/commands/initWithoutFiles.d.ts.map +1 -0
  12. package/dist/new/commands/initWithoutFiles.js +36 -0
  13. package/dist/new/commands/initWithoutFiles.js.map +1 -0
  14. package/dist/new/commands/login.d.ts +9 -0
  15. package/dist/new/commands/login.d.ts.map +1 -0
  16. package/dist/new/commands/login.js +51 -0
  17. package/dist/new/commands/login.js.map +1 -0
  18. package/dist/new/commands/logout.d.ts +4 -0
  19. package/dist/new/commands/logout.d.ts.map +1 -0
  20. package/dist/new/commands/logout.js +22 -0
  21. package/dist/new/commands/logout.js.map +1 -0
  22. package/dist/new/context/authToken.d.ts +19 -0
  23. package/dist/new/context/authToken.d.ts.map +1 -0
  24. package/dist/new/context/authToken.js +51 -0
  25. package/dist/new/context/authToken.js.map +1 -0
  26. package/dist/new/context/currentApp.d.ts +36 -0
  27. package/dist/new/context/currentApp.d.ts.map +1 -0
  28. package/dist/new/context/currentApp.js +145 -0
  29. package/dist/new/context/currentApp.js.map +1 -0
  30. package/dist/new/context/globalOpts.d.ts +11 -0
  31. package/dist/new/context/globalOpts.d.ts.map +1 -0
  32. package/dist/new/context/globalOpts.js +10 -0
  33. package/dist/new/context/globalOpts.js.map +1 -0
  34. package/dist/new/context/platformApi.d.ts +19 -0
  35. package/dist/new/context/platformApi.d.ts.map +1 -0
  36. package/dist/new/context/platformApi.js +24 -0
  37. package/dist/new/context/platformApi.js.map +1 -0
  38. package/dist/new/context/projectInfo.d.ts +25 -0
  39. package/dist/new/context/projectInfo.d.ts.map +1 -0
  40. package/dist/new/context/projectInfo.js +120 -0
  41. package/dist/new/context/projectInfo.js.map +1 -0
  42. package/dist/new/errors.d.ts +10 -0
  43. package/dist/new/errors.d.ts.map +1 -0
  44. package/dist/new/errors.js +6 -0
  45. package/dist/new/errors.js.map +1 -0
  46. package/dist/new/index.d.ts +17 -0
  47. package/dist/new/index.d.ts.map +1 -0
  48. package/dist/new/index.js +159 -0
  49. package/dist/new/index.js.map +1 -0
  50. package/dist/new/layer.d.ts +15 -0
  51. package/dist/new/layer.d.ts.map +1 -0
  52. package/dist/new/layer.js +41 -0
  53. package/dist/new/layer.js.map +1 -0
  54. package/dist/new/lib/createApp.d.ts +4 -0
  55. package/dist/new/lib/createApp.d.ts.map +1 -0
  56. package/dist/new/lib/createApp.js +13 -0
  57. package/dist/new/lib/createApp.js.map +1 -0
  58. package/dist/new/lib/handleEnv.d.ts +7 -0
  59. package/dist/new/lib/handleEnv.d.ts.map +1 -0
  60. package/dist/new/lib/handleEnv.js +88 -0
  61. package/dist/new/lib/handleEnv.js.map +1 -0
  62. package/dist/new/lib/http.d.ts +15 -0
  63. package/dist/new/lib/http.d.ts.map +1 -0
  64. package/dist/new/lib/http.js +32 -0
  65. package/dist/new/lib/http.js.map +1 -0
  66. package/dist/new/lib/login.d.ts +13 -0
  67. package/dist/new/lib/login.d.ts.map +1 -0
  68. package/dist/new/lib/login.js +36 -0
  69. package/dist/new/lib/login.js.map +1 -0
  70. package/dist/new/lib/ui.d.ts +16 -0
  71. package/dist/new/lib/ui.d.ts.map +1 -0
  72. package/dist/new/lib/ui.js +30 -0
  73. package/dist/new/lib/ui.js.map +1 -0
  74. package/dist/new/logging.d.ts +3 -0
  75. package/dist/new/logging.d.ts.map +1 -0
  76. package/dist/new/logging.js +8 -0
  77. package/dist/new/logging.js.map +1 -0
  78. package/dist/ui/index.d.ts +1 -1
  79. package/dist/ui/index.d.ts.map +1 -1
  80. package/dist/ui/index.js.map +1 -1
  81. package/dist/util/getAuthPaths.d.ts.map +1 -1
  82. package/dist/util/getAuthPaths.js.map +1 -1
  83. package/package.json +14 -5
  84. package/src/index.js +2 -2
  85. package/src/new/commands/init.ts +12 -0
  86. package/src/new/commands/initWithoutFiles.ts +44 -0
  87. package/src/new/commands/login.ts +73 -0
  88. package/src/new/commands/logout.ts +23 -0
  89. package/src/new/context/authToken.ts +77 -0
  90. package/src/new/context/currentApp.ts +207 -0
  91. package/src/new/context/globalOpts.ts +22 -0
  92. package/src/new/context/platformApi.ts +35 -0
  93. package/src/new/context/projectInfo.ts +172 -0
  94. package/src/new/errors.ts +7 -0
  95. package/src/new/index.ts +245 -0
  96. package/src/new/layer.ts +78 -0
  97. package/src/new/lib/createApp.ts +18 -0
  98. package/src/new/lib/handleEnv.ts +107 -0
  99. package/src/new/lib/http.ts +63 -0
  100. package/src/new/lib/login.ts +50 -0
  101. package/src/new/lib/ui.ts +45 -0
  102. package/src/new/logging.ts +9 -0
  103. package/src/ui/index.ts +1 -1
  104. package/src/util/getAuthPaths.ts +1 -0
  105. package/tsconfig.json +7 -2
@@ -0,0 +1,245 @@
1
+ import { Command, Option, program } from '@commander-js/extra-typings';
2
+ import chalk from 'chalk';
3
+ import { Effect } from 'effect';
4
+ import version from '../version.js';
5
+ import { initCommand } from './commands/init.js';
6
+ import { initWithoutFilesCommand } from './commands/initWithoutFiles.js';
7
+ import { loginCommand } from './commands/login.js';
8
+ import { logoutCommand } from './commands/logout.js';
9
+ import { loadEnv } from '../util/loadEnv.js';
10
+ import {
11
+ AuthLayerLive,
12
+ BaseLayerLive,
13
+ printRedErrors,
14
+ WithAppLayer,
15
+ } from './layer.js';
16
+
17
+ loadEnv();
18
+
19
+ export type ArgsFromCommand<C> =
20
+ C extends Command<any, infer R, any> ? R : never;
21
+
22
+ program
23
+ .name('instant-cli')
24
+ .addOption(globalOption('-t --token <token>', 'Auth token override'))
25
+ .addOption(globalOption('-y --yes', "Answer 'yes' to all prompts"))
26
+ .addOption(globalOption('--env <file>', 'Use a specific .env file'))
27
+ .addOption(
28
+ globalOption('-v --version', 'Print the version number', () => {
29
+ console.log(version);
30
+ process.exit(0);
31
+ }),
32
+ )
33
+ .addHelpOption(globalOption('-h --help', 'Print the help text for a command'))
34
+ .usage(`<command> ${chalk.dim('[options] [args]')}`);
35
+
36
+ // Command List
37
+ export const initDef = program
38
+ .command('init')
39
+ .description('Set up a new project.')
40
+ .option(
41
+ '-a --app <app-id>',
42
+ 'If you have an existing app ID, we can pull schema and perms from there.',
43
+ )
44
+ .option(
45
+ '-p --package <react|react-native|core|admin>',
46
+ 'Which package to automatically install if there is not one installed already.',
47
+ )
48
+ .option('--title <title>', 'Title for the created app')
49
+ .action((options) => {
50
+ return Effect.runPromise(
51
+ initCommand(options).pipe(
52
+ Effect.provide(
53
+ WithAppLayer({
54
+ coerce: true,
55
+ title: options.title,
56
+ appId: options.app,
57
+ packageName: options.package as any,
58
+ applyEnv: true,
59
+ }),
60
+ ),
61
+ printRedErrors,
62
+ ),
63
+ );
64
+ });
65
+
66
+ export const initWithoutFilesDef = program
67
+ .command('init-without-files')
68
+ .description('Generate a new app id and admin token pair without any files.')
69
+ .option('--title <title>', 'Title for the created app.')
70
+ .option(
71
+ '--org-id <org-id>',
72
+ 'Organization id for app. Cannot be used with --temp flag.',
73
+ )
74
+ .option(
75
+ '--temp',
76
+ 'Create a temporary app which will automatically delete itself after >24 hours.',
77
+ )
78
+ .action((opts) => {
79
+ return Effect.runPromise(
80
+ initWithoutFilesCommand(opts).pipe(
81
+ Effect.provide(AuthLayerLive),
82
+ printRedErrors,
83
+ ),
84
+ );
85
+ });
86
+
87
+ export const loginDef = program
88
+ .command('login')
89
+ .description('Log into your account')
90
+ .option('-p --print', 'Prints the auth token into the console.')
91
+ .option(
92
+ '--headless',
93
+ 'Print the login URL instead of trying to open the browser',
94
+ )
95
+ .action(async (opts) => {
96
+ Effect.runPromise(
97
+ loginCommand(opts).pipe(Effect.provide(BaseLayerLive), printRedErrors),
98
+ );
99
+ });
100
+
101
+ const _logoutDef = program
102
+ .command('logout')
103
+ .description('Log out of your Instant account')
104
+ .action(async () => {
105
+ Effect.runPromise(
106
+ logoutCommand().pipe(Effect.provide(BaseLayerLive), printRedErrors),
107
+ );
108
+ });
109
+
110
+ //// Program setup /////
111
+
112
+ function globalOption(
113
+ flags: string,
114
+ description?: string,
115
+ argParser?: (value: string, prev?: unknown) => unknown,
116
+ ) {
117
+ const opt = new Option(flags, description);
118
+ if (argParser) {
119
+ opt.argParser(argParser);
120
+ }
121
+ // @ts-ignore
122
+ // __global does not exist on `Option`,
123
+ // but we use it in `getLocalAndGlobalOptions`, to produce
124
+ // our own custom list of local and global options.
125
+ // For more info, see the original PR:
126
+ // https://github.com/instantdb/instant/pull/505
127
+ opt.__global = true;
128
+ return opt;
129
+ }
130
+
131
+ function getLocalAndGlobalOptions(cmd, helper) {
132
+ const mixOfLocalAndGlobal = helper.visibleOptions(cmd);
133
+ const localOptionsFromMix = mixOfLocalAndGlobal.filter(
134
+ (option) => !option.__global,
135
+ );
136
+ const globalOptionsFromMix = mixOfLocalAndGlobal.filter(
137
+ (option) => option.__global,
138
+ );
139
+ const globalOptions = helper.visibleGlobalOptions(cmd);
140
+
141
+ return [localOptionsFromMix, globalOptionsFromMix.concat(globalOptions)];
142
+ }
143
+
144
+ function formatHelp(cmd, helper) {
145
+ const termWidth = helper.padWidth(cmd, helper);
146
+ const helpWidth = helper.helpWidth || 80;
147
+ const itemIndentWidth = 2;
148
+ const itemSeparatorWidth = 2; // between term and description
149
+ function formatItem(term, description) {
150
+ if (description) {
151
+ const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
152
+ return helper.wrap(
153
+ fullText,
154
+ helpWidth - itemIndentWidth,
155
+ termWidth + itemSeparatorWidth,
156
+ );
157
+ }
158
+ return term;
159
+ }
160
+ function formatList(textArray) {
161
+ return textArray.join('\n').replace(/^/gm, ' '.repeat(itemIndentWidth));
162
+ }
163
+
164
+ // Usage
165
+ let output = [`${helper.commandUsage(cmd)}`, ''];
166
+
167
+ // Description
168
+ const commandDescription = helper.commandDescription(cmd);
169
+ if (commandDescription.length > 0) {
170
+ output = output.concat([helper.wrap(commandDescription, helpWidth, 0), '']);
171
+ }
172
+
173
+ // Arguments
174
+ const argumentList = helper.visibleArguments(cmd).map((argument) => {
175
+ return formatItem(
176
+ helper.argumentTerm(argument),
177
+ helper.argumentDescription(argument),
178
+ );
179
+ });
180
+ if (argumentList.length > 0) {
181
+ output = output.concat([
182
+ chalk.dim.bold('Arguments'),
183
+ formatList(argumentList),
184
+ '',
185
+ ]);
186
+ }
187
+ const [visibleOptions, visibleGlobalOptions] = getLocalAndGlobalOptions(
188
+ cmd,
189
+ helper,
190
+ );
191
+
192
+ // Options
193
+ const optionList = visibleOptions.map((option) => {
194
+ return formatItem(
195
+ helper.optionTerm(option),
196
+ helper.optionDescription(option),
197
+ );
198
+ });
199
+ if (optionList.length > 0) {
200
+ output = output.concat([
201
+ chalk.dim.bold('Options'),
202
+ formatList(optionList),
203
+ '',
204
+ ]);
205
+ }
206
+ // Commands
207
+ const commandList = helper.visibleCommands(cmd).map((cmd) => {
208
+ return formatItem(
209
+ helper.subcommandTerm(cmd),
210
+ helper.subcommandDescription(cmd),
211
+ );
212
+ });
213
+ if (commandList.length > 0) {
214
+ output = output.concat([
215
+ chalk.dim.bold('Commands'),
216
+ formatList(commandList),
217
+ '',
218
+ ]);
219
+ }
220
+
221
+ if (this.showGlobalOptions) {
222
+ const globalOptionList = visibleGlobalOptions.map((option) => {
223
+ return formatItem(
224
+ helper.optionTerm(option),
225
+ helper.optionDescription(option),
226
+ );
227
+ });
228
+ if (globalOptionList.length > 0) {
229
+ output = output.concat([
230
+ chalk.dim.bold('Global Options'),
231
+ formatList(globalOptionList),
232
+ '',
233
+ ]);
234
+ }
235
+ }
236
+
237
+ return output.join('\n');
238
+ }
239
+
240
+ program.configureHelp({
241
+ showGlobalOptions: true,
242
+ formatHelp,
243
+ });
244
+
245
+ program.parse(process.argv);
@@ -0,0 +1,78 @@
1
+ import { NodeContext, NodeHttpClient } from '@effect/platform-node';
2
+ import chalk from 'chalk';
3
+ import { Cause, Console, Effect, Layer } from 'effect';
4
+ import { InstantHttpAuthedLive, InstantHttpLive } from './lib/http.js';
5
+ import { AuthTokenLive } from './context/authToken.js';
6
+ import { PlatformApi } from './context/platformApi.js';
7
+ import { GlobalOptsLive } from './context/globalOpts.js';
8
+ import {
9
+ PACKAGE_ALIAS_AND_FULL_NAMES,
10
+ ProjectInfoLive,
11
+ } from './context/projectInfo.js';
12
+ import { CurrentAppLive } from './context/currentApp.js';
13
+
14
+ export const printRedErrors = Effect.catchAllCause((cause) => {
15
+ const failure = Cause.failureOption(cause);
16
+
17
+ // Print just the message if the error has a message attribute and no cause
18
+ if (
19
+ failure._tag === 'Some' &&
20
+ typeof failure.value === 'object' &&
21
+ failure.value !== null &&
22
+ 'message' in failure.value &&
23
+ !('cause' in failure.value)
24
+ ) {
25
+ return Console.error(
26
+ chalk.red((failure.value as { message: string }).message),
27
+ );
28
+ }
29
+ return Console.error(
30
+ chalk.red(Cause.pretty(cause, { renderErrorCause: true })),
31
+ );
32
+ });
33
+
34
+ /**
35
+ * Note:
36
+ Avoid Duplicate Layer Creation
37
+
38
+ Layers are memoized using reference equality. Therefore, if you have a layer that is created by calling a function like f(), you should only call that f once and re-use the resulting layer so that you are always using the same instance.
39
+ */
40
+
41
+ // Base layers
42
+ const AuthTokenLayer = Layer.provide(AuthTokenLive, NodeContext.layer);
43
+ const InstantHttpLayer = Layer.provide(InstantHttpLive, NodeHttpClient.layer);
44
+
45
+ // Unauthenticated layer with InstantHttp + PlatformApi + GlobalOpts + NodeContext
46
+ export const BaseLayerLive = Layer.provideMerge(
47
+ Layer.mergeAll(InstantHttpLayer, PlatformApi.Default, GlobalOptsLive),
48
+ NodeContext.layer,
49
+ );
50
+
51
+ // Authenticated layer extends BaseLayerLive with InstantHttpAuthed
52
+ export const AuthLayerLive = Layer.provideMerge(
53
+ Layer.provideMerge(
54
+ InstantHttpAuthedLive,
55
+ Layer.merge(AuthTokenLayer, InstantHttpLayer),
56
+ ),
57
+ BaseLayerLive,
58
+ );
59
+
60
+ export const WithAppLayer = (args: {
61
+ appId?: string;
62
+ title?: string;
63
+ coerce: boolean;
64
+ packageName: keyof typeof PACKAGE_ALIAS_AND_FULL_NAMES;
65
+ applyEnv?: boolean;
66
+ }) =>
67
+ Layer.mergeAll(
68
+ CurrentAppLive({
69
+ coerce: args.coerce,
70
+ appId: args.appId,
71
+ title: args.title,
72
+ applyEnv: args.applyEnv,
73
+ }),
74
+ ).pipe(
75
+ Layer.provideMerge(GlobalOptsLive),
76
+ Layer.provideMerge(AuthLayerLive),
77
+ Layer.provideMerge(ProjectInfoLive(args.coerce, args.packageName)),
78
+ );
@@ -0,0 +1,18 @@
1
+ import { randomUUID } from 'crypto';
2
+ import { Effect } from 'effect';
3
+ import { InstantHttpAuthed } from './http.js';
4
+ import { HttpClientRequest } from '@effect/platform';
5
+
6
+ export const createApp = Effect.fn(function* (title: string, orgId?: string) {
7
+ const http = yield* InstantHttpAuthed;
8
+ const id = randomUUID();
9
+ const token = randomUUID();
10
+ const app = { id, title, admin_token: token, org_id: orgId };
11
+
12
+ const res = yield* HttpClientRequest.post('/dash/apps').pipe(
13
+ HttpClientRequest.bodyJson(app),
14
+ Effect.flatMap(http.execute),
15
+ Effect.flatMap((res) => res.json),
16
+ );
17
+ return res;
18
+ });
@@ -0,0 +1,107 @@
1
+ import { Effect } from 'effect';
2
+ import { CurrentAppInfo, potentialEnvs } from '../context/currentApp.js';
3
+ import { ProjectInfo, ProjectInfoError } from '../context/projectInfo.js';
4
+ import { readPackage } from 'pkg-types';
5
+ import { GlobalOpts } from '../context/globalOpts.js';
6
+ import { FileSystem, Path } from '@effect/platform';
7
+ import chalk from 'chalk';
8
+ import terminalLink from 'terminal-link';
9
+ import { getDashUrl } from './http.js';
10
+ import { promptOk } from './ui.js';
11
+
12
+ export const handleEnv = Effect.fn(function* (app: CurrentAppInfo) {
13
+ const opts = yield* GlobalOpts;
14
+ const { pkgDir } = yield* ProjectInfo;
15
+ const envType = yield* detectEnvType;
16
+ const envName = potentialEnvs[envType];
17
+ const envFile = opts.env ?? '.env';
18
+ const fs = yield* FileSystem.FileSystem;
19
+ const path = yield* Path.Path;
20
+ const hasEnvFile = yield* fs.exists(path.join(pkgDir, envFile));
21
+ const dashOrigin = yield* getDashUrl;
22
+ if (hasEnvFile) {
23
+ return printDotEnvInfo(envType, app.appId, dashOrigin);
24
+ }
25
+ console.log(
26
+ `\nLooks like you don't have a ${chalk.green(`\`${envFile}\``)} file yet.`,
27
+ );
28
+ console.log(
29
+ `If we set ${chalk.green(envName)} & ${chalk.green('INSTANT_APP_ADMIN_TOKEN')}, we can remember the app that you chose for all future commands.`,
30
+ );
31
+ const saveExtraInfo =
32
+ envFile !== '.env' ? chalk.green(' (will create `' + envFile + '`)') : '';
33
+
34
+ const ok = yield* promptOk(
35
+ {
36
+ inline: true,
37
+ promptText: 'Want us to create this env file for you?' + saveExtraInfo,
38
+ modifyOutput: (a) => a,
39
+ },
40
+ true,
41
+ );
42
+ if (!ok) {
43
+ console.log(
44
+ `No .env file created. You can always set ${chalk.green('`' + envName + '`')} later. \n`,
45
+ );
46
+ return;
47
+ }
48
+ const content =
49
+ [
50
+ [envName, app.appId],
51
+ ['INSTANT_APP_ADMIN_TOKEN', app.adminToken],
52
+ ]
53
+ .map(([k, v]) => `${k}=${v}`)
54
+ .join('\n') + '\n';
55
+
56
+ yield* fs.writeFileString(path.join(pkgDir, envFile), content);
57
+
58
+ if (envFile !== '.env') {
59
+ console.log(`Created ${chalk.green(envFile)}!`);
60
+ } else {
61
+ console.log(`Created ${chalk.green('.env')} file!`);
62
+ }
63
+ });
64
+
65
+ const detectEnvType = Effect.gen(function* () {
66
+ const pkgJson = yield* Effect.tryPromise({
67
+ try: () => readPackage(),
68
+ catch: () =>
69
+ new ProjectInfoError({ message: "Couldn't read package.json" }),
70
+ });
71
+ if (pkgJson.dependencies?.next) {
72
+ return 'next';
73
+ }
74
+ if (pkgJson.devDependencies?.svelte) {
75
+ return 'svelte';
76
+ }
77
+ if (pkgJson.devDependencies?.vite) {
78
+ return 'vite';
79
+ }
80
+ if (pkgJson.dependencies?.expo) {
81
+ return 'expo';
82
+ }
83
+ if (pkgJson.dependencies?.nuxt) {
84
+ return 'nuxt';
85
+ }
86
+ return 'catchall';
87
+ }).pipe(Effect.catchTag('ProjectInfoError', () => Effect.succeed('catchall')));
88
+
89
+ function printDotEnvInfo(envType, appId, dashOrigin: string) {
90
+ console.log(`\nPicked app ${chalk.green(appId)}!\n`);
91
+ console.log(
92
+ `To use this app automatically from now on, update your ${chalk.green('`.env`')} file:`,
93
+ );
94
+ const picked = potentialEnvs[envType];
95
+ const rest = { ...potentialEnvs };
96
+ delete rest[envType];
97
+ console.log(` ${chalk.green(picked)}=${appId}`);
98
+ const otherEnvs = Object.values(rest);
99
+ otherEnvs.sort();
100
+ const otherEnvStr = otherEnvs.map((x) => ' ' + chalk.green(x)).join('\n');
101
+ console.log(`Alternative names: \n${otherEnvStr} \n`);
102
+ console.log(terminalLink('Dashboard:', appDashUrl(appId, dashOrigin)) + '\n');
103
+ }
104
+
105
+ function appDashUrl(id, instantOrigin: string) {
106
+ return `${instantOrigin}/dash?s=main&t=home&app=${id}`;
107
+ }
@@ -0,0 +1,63 @@
1
+ import { HttpClient, HttpClientRequest } from '@effect/platform';
2
+ import { Config, Context, Effect, Layer, Option } from 'effect';
3
+ import { AuthToken } from '../context/authToken.js';
4
+
5
+ export class InstantHttp extends Context.Tag(
6
+ 'instant-cli/new/lib/http/InstantHttp',
7
+ )<InstantHttp, HttpClient.HttpClient>() {}
8
+
9
+ export class InstantHttpAuthed extends Context.Tag(
10
+ 'instant-cli/new/lib/http/InstantHttpAuthed',
11
+ )<InstantHttpAuthed, HttpClient.HttpClient>() {}
12
+
13
+ export const InstantHttpLive = Layer.effect(
14
+ InstantHttp,
15
+ Effect.gen(function* () {
16
+ const client = yield* HttpClient.HttpClient;
17
+ const baseUrl = yield* getBaseUrl;
18
+ return client.pipe(
19
+ HttpClient.mapRequest((r) =>
20
+ r.pipe(HttpClientRequest.prependUrl(baseUrl)),
21
+ ),
22
+ HttpClient.filterStatusOk, // makes non 2xx http codes error
23
+ );
24
+ }),
25
+ );
26
+
27
+ export const InstantHttpAuthedLive = Layer.effect(
28
+ InstantHttpAuthed,
29
+ Effect.gen(function* () {
30
+ const http = yield* InstantHttp;
31
+ const { authToken } = yield* AuthToken;
32
+ return http.pipe(
33
+ HttpClient.mapRequest((r) =>
34
+ r.pipe(
35
+ HttpClientRequest.setHeader('Authorization', `Bearer ${authToken}`),
36
+ ),
37
+ ),
38
+ );
39
+ }),
40
+ );
41
+
42
+ export const getBaseUrl = Effect.gen(function* () {
43
+ const setEnv = yield* Config.string('INSTANT_CLI_API_URI').pipe(
44
+ Config.option,
45
+ );
46
+ const dev = yield* Config.boolean('INSTANT_CLI_DEV').pipe(
47
+ Config.withDefault(false),
48
+ );
49
+
50
+ return Option.match(setEnv, {
51
+ onSome: (url) => url,
52
+ onNone: () => {
53
+ return dev ? 'http://localhost:8888' : 'https://api.instantdb.com';
54
+ },
55
+ });
56
+ });
57
+
58
+ export const getDashUrl = Effect.gen(function* () {
59
+ const dev = Option.getOrNull(
60
+ yield* Config.boolean('INSTANT_CLI_DEV').pipe(Config.option),
61
+ );
62
+ return dev ? 'http://localhost:3000' : 'https://instantdb.com';
63
+ });
@@ -0,0 +1,50 @@
1
+ import { Effect, Schedule, Schema } from 'effect';
2
+ import { InstantHttp } from './http.js';
3
+ import {
4
+ HttpClientRequest,
5
+ HttpClientResponse,
6
+ FileSystem,
7
+ } from '@effect/platform';
8
+ import { getAuthPaths } from '../../util/getAuthPaths.js';
9
+
10
+ const LoginInfo = Schema.Struct({
11
+ secret: Schema.String,
12
+ ticket: Schema.String,
13
+ });
14
+
15
+ const TokenResult = Schema.Struct({
16
+ token: Schema.String,
17
+ email: Schema.String,
18
+ });
19
+
20
+ export const getLoginTicketAndSecret = Effect.gen(function* () {
21
+ const http = yield* InstantHttp;
22
+ const res = yield* http
23
+ .post('/dash/cli/auth/register')
24
+ .pipe(Effect.flatMap(HttpClientResponse.schemaBodyJson(LoginInfo)));
25
+ return res;
26
+ });
27
+
28
+ export const waitForAuthToken = Effect.fn(function* (secret: string) {
29
+ const http = yield* InstantHttp;
30
+ const res = yield* HttpClientRequest.post('/dash/cli/auth/check').pipe(
31
+ HttpClientRequest.bodyJson({
32
+ secret,
33
+ }),
34
+ Effect.flatMap(http.execute),
35
+ Effect.flatMap(HttpClientResponse.schemaBodyJson(TokenResult)),
36
+ Effect.retry({
37
+ schedule: Schedule.fixed('1 seconds'),
38
+ times: 12,
39
+ }),
40
+ );
41
+ return res;
42
+ });
43
+
44
+ export const saveConfigAuthToken = Effect.fn(function* (token: string) {
45
+ const authPaths = getAuthPaths();
46
+
47
+ const fs = yield* FileSystem.FileSystem;
48
+ yield* fs.makeDirectory(authPaths.appConfigDirPath, { recursive: true });
49
+ yield* fs.writeFileString(authPaths.authConfigFilePath, token);
50
+ });
@@ -0,0 +1,45 @@
1
+ import { Data, Effect } from 'effect';
2
+ import { GlobalOpts } from '../context/globalOpts.js';
3
+ import { Prompt, renderUnwrap } from '../../ui/lib.js';
4
+ import boxen from 'boxen';
5
+ import { UI } from '../../ui/index.js';
6
+
7
+ export class UIError extends Data.TaggedError('UIError')<{
8
+ message: string;
9
+ cause?: unknown;
10
+ }> {}
11
+
12
+ export const promptOk = Effect.fn('promptOk')(function* (
13
+ props: UI.ConfirmationProps,
14
+ defaultValue: boolean = true,
15
+ ) {
16
+ const opts = yield* GlobalOpts;
17
+ if (opts.yes) {
18
+ return defaultValue;
19
+ }
20
+
21
+ const ok = yield* Effect.tryPromise(() =>
22
+ renderUnwrap(
23
+ new UI.Confirmation({
24
+ ...props,
25
+ modifyOutput: (out) =>
26
+ boxen(out, {
27
+ dimBorder: true,
28
+ padding: {
29
+ left: 1,
30
+ right: 1,
31
+ },
32
+ }),
33
+ defaultValue,
34
+ }),
35
+ ),
36
+ ).pipe(Effect.orElseSucceed(() => defaultValue));
37
+
38
+ return ok;
39
+ });
40
+
41
+ export const runUIEffect = <P>(prompt: Prompt<P>) =>
42
+ Effect.tryPromise({
43
+ try: () => renderUnwrap(prompt),
44
+ catch: (error) => new UIError({ message: 'UI Error', cause: error }),
45
+ });
@@ -0,0 +1,9 @@
1
+ import chalk from 'chalk';
2
+
3
+ export function warn(firstArg, ...rest) {
4
+ console.warn(chalk.yellow('[warning]') + ' ' + firstArg, ...rest);
5
+ }
6
+
7
+ export function error(firstArg, ...rest) {
8
+ console.error(chalk.red('[error]') + ' ' + firstArg, ...rest);
9
+ }
package/src/ui/index.ts CHANGED
@@ -842,7 +842,7 @@ ${inputDisplay}`;
842
842
  }
843
843
  }
844
844
 
845
- interface AppSelectorApi {
845
+ export interface AppSelectorApi {
846
846
  getDash: () => { apps: App[]; orgs: Org[] };
847
847
  createEphemeralApp: (title: string) => Promise<{
848
848
  appId: string;
@@ -2,6 +2,7 @@ import envPaths from 'env-paths';
2
2
  import { join } from 'path';
3
3
 
4
4
  const dev = Boolean(process.env.INSTANT_CLI_DEV);
5
+
5
6
  export function getAuthPaths() {
6
7
  const key = `instantdb-${dev ? 'dev' : 'prod'}`;
7
8
  const { config: appConfigDirPath } = envPaths(key);
package/tsconfig.json CHANGED
@@ -5,9 +5,14 @@
5
5
  "rootDir": "src",
6
6
  "sourceMap": true,
7
7
  "skipLibCheck": true, // `unconfig` currently imports a broken type from `@antfu/utils`,
8
- "strictNullChecks": true
8
+ "plugins": [
9
+ {
10
+ "name": "@effect/language-service",
11
+ },
12
+ ],
13
+ "strictNullChecks": true,
9
14
  // remove once fixed
10
15
  },
11
16
  "include": ["src"],
12
- "exclude": ["node_modules", "dist"]
17
+ "exclude": ["node_modules", "dist"],
13
18
  }