create-feltdb 0.4.4 → 0.4.9

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.
package/dist/index.js CHANGED
@@ -2,3 +2,4 @@
2
2
  * create-feltdb - Main exports
3
3
  */
4
4
  export * from './create.js';
5
+ export * from './managed-account.js';
@@ -0,0 +1,61 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ async function responseJson(response, fallback) {
4
+ const body = await response.json().catch(() => ({}));
5
+ if (!response.ok)
6
+ throw new Error(String(body.error || body.message || fallback));
7
+ return body;
8
+ }
9
+ export async function configureManagedAccount(options) {
10
+ const accountUrl = (options.accountUrl || process.env.FELTDB_MANAGED_ACCOUNT_URL || 'https://feltdb.com').replace(/\/$/, '');
11
+ const apiUrl = (options.apiUrl || process.env.FELTDB_MANAGED_URL || 'https://api.feltdb.com').replace(/\/$/, '');
12
+ const request = options.fetchImpl || fetch;
13
+ const authenticated = await request(`${accountUrl}/api/auth`, {
14
+ method: 'POST', headers: { 'Content-Type': 'application/json' },
15
+ body: JSON.stringify({ email: options.email, password: options.password }),
16
+ });
17
+ await responseJson(authenticated, 'Could not create or sign in to the managed account');
18
+ const cookie = authenticated.headers.get('set-cookie')?.split(';', 1)[0];
19
+ if (!cookie)
20
+ throw new Error('Managed account service did not return a secure session');
21
+ const headers = { Cookie: cookie, 'Content-Type': 'application/json' };
22
+ const tenants = await responseJson(await request(`${accountUrl}/api/tenants`, { headers }), 'Could not load managed workspaces');
23
+ let tenant = Array.isArray(tenants) ? tenants[0] : undefined;
24
+ if (!tenant) {
25
+ tenant = await responseJson(await request(`${accountUrl}/api/tenants`, {
26
+ method: 'POST', headers, body: JSON.stringify({ name: `${options.applicationName} Workspace` }),
27
+ }), 'Could not create the managed workspace');
28
+ }
29
+ const applicationsUrl = `${accountUrl}/api/tenants/${encodeURIComponent(tenant.id)}/applications`;
30
+ const applications = await responseJson(await request(applicationsUrl, { headers }), 'Could not load managed applications');
31
+ let application = Array.isArray(applications)
32
+ ? applications.find(value => value.name === options.applicationName)
33
+ : undefined;
34
+ if (!application) {
35
+ application = await responseJson(await request(applicationsUrl, {
36
+ method: 'POST', headers, body: JSON.stringify({ name: options.applicationName }),
37
+ }), 'Could not create the managed application');
38
+ }
39
+ const key = await responseJson(await request(`${apiUrl}/api/keys`, {
40
+ method: 'POST', headers,
41
+ body: JSON.stringify({
42
+ id: `${options.applicationName}-cli`,
43
+ namespace: options.namespace,
44
+ scopes: ['state:read', 'state:write', 'events:read'],
45
+ }),
46
+ }), 'Could not create the managed application key');
47
+ if (!key.secret)
48
+ throw new Error('Managed key service did not return an application secret');
49
+ const environment = [
50
+ '# Generated by create-feltdb managed setup. Do not commit this file.',
51
+ `VITE_FELTDB_MANAGED_URL=${apiUrl}`,
52
+ `VITE_FELTDB_MANAGED_API_KEY=${key.secret}`,
53
+ `VITE_FELTDB_MANAGED_TENANT_ID=${tenant.id}`,
54
+ `VITE_FELTDB_MANAGED_APPLICATION_ID=${application.id}`,
55
+ `VITE_FELTDB_MANAGED_NAMESPACE=${application.namespace || options.namespace}`,
56
+ 'VITE_FELTDB_MANAGED_ENVIRONMENT=production',
57
+ '',
58
+ ].join('\n');
59
+ fs.writeFileSync(path.join(options.projectDir, '.env.local'), environment, { mode: 0o600 });
60
+ return { tenant, application, apiUrl };
61
+ }
@@ -1,4 +1,4 @@
1
1
  // One release train keeps generated applications installable. The repository
2
2
  // validation script checks these values against every workspace manifest.
3
- export const FELTDB_PACKAGE_VERSION = '0.4.4';
3
+ export const FELTDB_PACKAGE_VERSION = '0.4.9';
4
4
  export const feltdbPackageRange = `^${FELTDB_PACKAGE_VERSION}`;
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "create-feltdb",
3
- "version": "0.4.4",
3
+ "private": false,
4
4
  "description": "Create a new FeltDB application with one command",
5
+ "version": "0.4.9",
5
6
  "license": "MIT",
6
7
  "bin": {
7
8
  "create-feltdb": "bin/create-feltdb.js"