powersync 0.8.0 → 0.9.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.
- package/LICENSE +67 -0
- package/README.md +1376 -173
- package/bin/dev.cmd +3 -3
- package/bin/dev.js +20 -7
- package/bin/run.cmd +3 -3
- package/bin/run.js +9 -5
- package/dist/api/cloud/create-cloud-instance.d.ts +15 -0
- package/dist/api/cloud/create-cloud-instance.js +19 -0
- package/dist/api/cloud/fetch-cloud-config.d.ts +12 -0
- package/dist/api/cloud/fetch-cloud-config.js +18 -0
- package/dist/api/cloud/fetch-cloud-sync-rules-content.d.ts +7 -0
- package/dist/api/cloud/fetch-cloud-sync-rules-content.js +26 -0
- package/dist/api/cloud/test-connection.d.ts +16 -0
- package/dist/api/cloud/test-connection.js +36 -0
- package/dist/api/cloud/validate-cloud-link-config.d.ts +17 -0
- package/dist/api/cloud/validate-cloud-link-config.js +47 -0
- package/dist/api/cloud/wait-for-operation.d.ts +16 -0
- package/dist/api/cloud/wait-for-operation.js +33 -0
- package/dist/api/cloud/write-cloud-link.d.ts +10 -0
- package/dist/api/cloud/write-cloud-link.js +21 -0
- package/dist/api/display-status.d.ts +4 -0
- package/dist/api/display-status.js +80 -0
- package/dist/api/ide/configure-vscode-ide.d.ts +14 -0
- package/dist/api/ide/configure-vscode-ide.js +62 -0
- package/dist/api/login-server.d.ts +26 -0
- package/dist/api/login-server.js +137 -0
- package/dist/api/run-validation-tests.d.ts +63 -0
- package/dist/api/run-validation-tests.js +202 -0
- package/dist/api/self-hosted/fetch-self-hosted-sync-rules-content.d.ts +7 -0
- package/dist/api/self-hosted/fetch-self-hosted-sync-rules-content.js +26 -0
- package/dist/commands/configure/ide.d.ts +8 -0
- package/dist/commands/configure/ide.js +58 -0
- package/dist/commands/configure/index.d.ts +8 -0
- package/dist/commands/configure/index.js +11 -0
- package/dist/commands/deploy/index.d.ts +48 -0
- package/dist/commands/deploy/index.js +333 -0
- package/dist/commands/deploy/service-config.d.ts +14 -0
- package/dist/commands/deploy/service-config.js +30 -0
- package/dist/commands/deploy/sync-config.d.ts +24 -0
- package/dist/commands/deploy/sync-config.js +103 -0
- package/dist/commands/destroy.d.ts +14 -0
- package/dist/commands/destroy.js +62 -0
- package/dist/commands/fetch/config.d.ts +14 -0
- package/dist/commands/fetch/config.js +33 -0
- package/dist/commands/fetch/index.d.ts +13 -0
- package/dist/commands/fetch/index.js +16 -0
- package/dist/commands/fetch/instances.d.ts +47 -0
- package/dist/commands/fetch/instances.js +177 -0
- package/dist/commands/fetch/status.d.ts +18 -0
- package/dist/commands/fetch/status.js +60 -0
- package/dist/commands/{instance/schema.d.ts → generate/index.d.ts} +3 -1
- package/dist/commands/generate/index.js +11 -0
- package/dist/commands/generate/schema.d.ts +19 -0
- package/dist/commands/generate/schema.js +72 -0
- package/dist/commands/generate/token.d.ts +25 -0
- package/dist/commands/generate/token.js +124 -0
- package/dist/commands/init/cloud.d.ts +11 -0
- package/dist/commands/init/cloud.js +64 -0
- package/dist/commands/{init.d.ts → init/index.d.ts} +2 -1
- package/dist/commands/init/index.js +11 -0
- package/dist/commands/init/self-hosted.d.ts +11 -0
- package/dist/commands/init/self-hosted.js +60 -0
- package/dist/commands/link/cloud.d.ts +15 -0
- package/dist/commands/link/cloud.js +111 -0
- package/dist/commands/{instance/config.d.ts → link/index.d.ts} +3 -1
- package/dist/commands/link/index.js +11 -0
- package/dist/commands/link/self-hosted.d.ts +12 -0
- package/dist/commands/link/self-hosted.js +54 -0
- package/dist/commands/login.d.ts +8 -0
- package/dist/commands/login.js +107 -0
- package/dist/commands/logout.d.ts +8 -0
- package/dist/commands/logout.js +19 -0
- package/dist/commands/migrate/index.d.ts +7 -0
- package/dist/commands/migrate/index.js +10 -0
- package/dist/commands/migrate/sync-rules.d.ts +15 -0
- package/dist/commands/migrate/sync-rules.js +51 -0
- package/dist/commands/{instances.d.ts → pull/index.d.ts} +3 -1
- package/dist/commands/pull/index.js +11 -0
- package/dist/commands/pull/instance.d.ts +14 -0
- package/dist/commands/pull/instance.js +108 -0
- package/dist/commands/status.d.ts +1 -0
- package/dist/commands/status.js +2 -0
- package/dist/commands/stop.d.ts +14 -0
- package/dist/commands/stop.js +62 -0
- package/dist/commands/validate.d.ts +16 -0
- package/dist/commands/validate.js +165 -0
- package/dist/help.d.ts +31 -0
- package/dist/help.js +123 -0
- package/oclif.manifest.json +1259 -330
- package/package.json +104 -75
- package/templates/cloud/powersync/cli.yaml +3 -0
- package/templates/cloud/powersync/service.yaml +174 -0
- package/templates/cloud/powersync/sync-config.yaml +22 -0
- package/templates/self-hosted/base/powersync/cli.yaml +3 -0
- package/templates/self-hosted/base/powersync/service.yaml +243 -0
- package/templates/self-hosted/base/powersync/sync-config.yaml +22 -0
- package/templates/vscode/settings.json +3 -0
- package/dist/commands/init.js +0 -66
- package/dist/commands/instance/config.js +0 -23
- package/dist/commands/instance/create.d.ts +0 -36
- package/dist/commands/instance/create.js +0 -137
- package/dist/commands/instance/deploy.d.ts +0 -21
- package/dist/commands/instance/deploy.js +0 -91
- package/dist/commands/instance/destroy.d.ts +0 -9
- package/dist/commands/instance/destroy.js +0 -46
- package/dist/commands/instance/generate-dev-token.d.ts +0 -10
- package/dist/commands/instance/generate-dev-token.js +0 -54
- package/dist/commands/instance/schema.js +0 -20
- package/dist/commands/instance/set.d.ts +0 -9
- package/dist/commands/instance/set.js +0 -31
- package/dist/commands/instance/status.d.ts +0 -12
- package/dist/commands/instance/status.js +0 -50
- package/dist/commands/instance/stop.d.ts +0 -9
- package/dist/commands/instance/stop.js +0 -42
- package/dist/commands/instance/sync-rules/deploy.d.ts +0 -10
- package/dist/commands/instance/sync-rules/deploy.js +0 -49
- package/dist/commands/instance/sync-rules/generate-schema.d.ts +0 -10
- package/dist/commands/instance/sync-rules/generate-schema.js +0 -56
- package/dist/commands/instance/sync-rules/validate.d.ts +0 -9
- package/dist/commands/instance/sync-rules/validate.js +0 -29
- package/dist/commands/instances.js +0 -29
- package/dist/commands.utils.d.ts +0 -2
- package/dist/commands.utils.js +0 -131
- package/dist/connection.utils.d.ts +0 -56
- package/dist/connection.utils.js +0 -76
- package/dist/constants.d.ts +0 -8
- package/dist/constants.js +0 -10
- package/dist/instance-command-flags.d.ts +0 -13
- package/dist/instance-command-flags.js +0 -22
- package/dist/service/accounts.client.js +0 -11307
- package/dist/service/powersync.client.js +0 -75510
- package/dist/service/types.js +0 -1
- package/dist/service/utils.js +0 -153
- package/dist/utils.d.ts +0 -14
- package/dist/utils.js +0 -68
package/bin/dev.cmd
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
@echo off
|
|
2
|
-
|
|
3
|
-
node --loader ts-node/esm --no-warnings=ExperimentalWarning "%~dp0\dev" %*
|
|
1
|
+
@echo off
|
|
2
|
+
|
|
3
|
+
node --loader ts-node/esm --no-warnings=ExperimentalWarning "%~dp0\dev" %*
|
package/bin/dev.js
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
-
#!/usr/bin/env -S node --
|
|
1
|
+
#!/usr/bin/env -S node --import tsx
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
3
|
+
import { execute } from '@oclif/core';
|
|
4
|
+
import { setCliClientHeaders } from '@powersync/cli-core';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
import packageJSON from '../package.json' with { type: 'json' };
|
|
9
|
+
|
|
10
|
+
setCliClientHeaders({
|
|
11
|
+
'user-agent': `POWERSYNC_CLI/${packageJSON.version}`
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
15
|
+
|
|
16
|
+
await execute({
|
|
17
|
+
development: true,
|
|
18
|
+
dir: import.meta.url,
|
|
19
|
+
loadOptions: {
|
|
20
|
+
root: path.resolve(__dirname, '..')
|
|
21
|
+
}
|
|
22
|
+
});
|
package/bin/run.cmd
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
@echo off
|
|
2
|
-
|
|
3
|
-
node "%~dp0\run" %*
|
|
1
|
+
@echo off
|
|
2
|
+
|
|
3
|
+
node "%~dp0\run" %*
|
package/bin/run.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
await execute({ dir: import.meta.url });
|
|
6
|
-
}
|
|
3
|
+
import { execute } from '@oclif/core';
|
|
4
|
+
import { setCliClientHeaders } from '@powersync/cli-core';
|
|
7
5
|
|
|
8
|
-
|
|
6
|
+
import packageJSON from '../package.json' with { type: 'json' };
|
|
7
|
+
|
|
8
|
+
setCliClientHeaders({
|
|
9
|
+
'user-agent': `POWERSYNC_CLI/${packageJSON.version}`
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
await execute({ dir: import.meta.url });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PowerSyncManagementClient } from '@powersync/management-client';
|
|
2
|
+
export type CreateCloudInstanceOptions = {
|
|
3
|
+
name: string;
|
|
4
|
+
orgId: string;
|
|
5
|
+
projectId: string;
|
|
6
|
+
region: string;
|
|
7
|
+
};
|
|
8
|
+
export type CreateCloudInstanceResult = {
|
|
9
|
+
instanceId: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new PowerSync Cloud instance in the given org and project.
|
|
13
|
+
* Uses the management API (e.g. createInstance). Caller should handle errors.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createCloudInstance(client: PowerSyncManagementClient, options: CreateCloudInstanceOptions): Promise<CreateCloudInstanceResult>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new PowerSync Cloud instance in the given org and project.
|
|
3
|
+
* Uses the management API (e.g. createInstance). Caller should handle errors.
|
|
4
|
+
*/
|
|
5
|
+
export async function createCloudInstance(client, options) {
|
|
6
|
+
const { name, orgId, projectId, region } = options;
|
|
7
|
+
// validate the region against the list of regions returned by the client.listRegions() method
|
|
8
|
+
const regions = await client.listRegions();
|
|
9
|
+
if (!regions.regions.some((r) => r.name === region)) {
|
|
10
|
+
throw new Error(`Region ${region} is not supported. Please choose a region from the list of supported regions: ${regions.regions.map((r) => r.name).join(', ')}.`);
|
|
11
|
+
}
|
|
12
|
+
const result = await client.createInstance({
|
|
13
|
+
app_id: projectId,
|
|
14
|
+
name,
|
|
15
|
+
org_id: orgId,
|
|
16
|
+
region
|
|
17
|
+
});
|
|
18
|
+
return { instanceId: result.id };
|
|
19
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ResolvedCloudCLIConfig, ServiceCloudConfigDecoded } from '@powersync/cli-schemas';
|
|
2
|
+
import { PowerSyncManagementClient } from '@powersync/management-client';
|
|
3
|
+
import { routes } from '@powersync/management-types';
|
|
4
|
+
export type FetchedCloudConfig = {
|
|
5
|
+
config: ServiceCloudConfigDecoded;
|
|
6
|
+
syncRules?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function decodeFetchedCloudConfig(instanceConfig: routes.InstanceConfigResponse): FetchedCloudConfig;
|
|
9
|
+
/**
|
|
10
|
+
* Fetches instance config from PowerSync Cloud and decodes it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function fetchCloudConfig(client: PowerSyncManagementClient, linked: ResolvedCloudCLIConfig): Promise<FetchedCloudConfig>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ServiceCloudConfig } from '@powersync/cli-schemas';
|
|
2
|
+
export function decodeFetchedCloudConfig(instanceConfig) {
|
|
3
|
+
const configFromCloud = { _type: 'cloud', name: instanceConfig.name, ...instanceConfig.config };
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
const config = ServiceCloudConfig.decode(configFromCloud);
|
|
6
|
+
return { config, syncRules: instanceConfig.sync_rules };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Fetches instance config from PowerSync Cloud and decodes it.
|
|
10
|
+
*/
|
|
11
|
+
export async function fetchCloudConfig(client, linked) {
|
|
12
|
+
const instanceConfig = await client.getInstanceConfig({
|
|
13
|
+
app_id: linked.project_id,
|
|
14
|
+
id: linked.instance_id,
|
|
15
|
+
org_id: linked.org_id
|
|
16
|
+
});
|
|
17
|
+
return decodeFetchedCloudConfig(instanceConfig);
|
|
18
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { CloudProject } from '@powersync/cli-core';
|
|
2
|
+
/**
|
|
3
|
+
* Fetches the sync config content for a cloud project.
|
|
4
|
+
* @param project - The project to fetch the sync config content for.
|
|
5
|
+
* @returns The sync config content.
|
|
6
|
+
*/
|
|
7
|
+
export declare function fetchCloudSyncRulesContent(project: CloudProject): Promise<string>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createCloudClient, SYNC_FILENAME } from '@powersync/cli-core';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
/**
|
|
5
|
+
* Fetches the sync config content for a cloud project.
|
|
6
|
+
* @param project - The project to fetch the sync config content for.
|
|
7
|
+
* @returns The sync config content.
|
|
8
|
+
*/
|
|
9
|
+
export async function fetchCloudSyncRulesContent(project) {
|
|
10
|
+
const { linked } = project;
|
|
11
|
+
const client = await createCloudClient();
|
|
12
|
+
// First try and use the local file
|
|
13
|
+
if (existsSync(join(project.projectDirectory, SYNC_FILENAME))) {
|
|
14
|
+
return readFileSync(join(project.projectDirectory, SYNC_FILENAME), 'utf8');
|
|
15
|
+
}
|
|
16
|
+
// Try and fetch from the cloud config
|
|
17
|
+
const instanceConfig = await client.getInstanceConfig({
|
|
18
|
+
app_id: linked.project_id,
|
|
19
|
+
id: linked.instance_id,
|
|
20
|
+
org_id: linked.org_id
|
|
21
|
+
});
|
|
22
|
+
if (!instanceConfig.sync_rules) {
|
|
23
|
+
throw new Error('No sync config found');
|
|
24
|
+
}
|
|
25
|
+
return instanceConfig.sync_rules;
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ResolvedCloudCLIConfig } from '@powersync/cli-schemas';
|
|
2
|
+
import type { PowerSyncManagementClient } from '@powersync/management-client';
|
|
3
|
+
import { routes } from '@powersync/management-types';
|
|
4
|
+
export type TestConnectionResult = {
|
|
5
|
+
connectionName: string;
|
|
6
|
+
response: routes.TestConnectionResponse;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Tests each database connection from the config against PowerSync Cloud.
|
|
10
|
+
* Uses the instance ID so secret_refs can be resolved.
|
|
11
|
+
*/
|
|
12
|
+
export declare function testCloudConnections(client: PowerSyncManagementClient, linked: ResolvedCloudCLIConfig, connections: Array<Record<string, unknown> & {
|
|
13
|
+
name?: string;
|
|
14
|
+
}>): Promise<TestConnectionResult[]>;
|
|
15
|
+
/** Pretty-print test connection response for error output. */
|
|
16
|
+
export declare function formatTestConnectionFailure(response: routes.TestConnectionResponse, connectionName: string): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { routes } from '@powersync/management-types';
|
|
2
|
+
/**
|
|
3
|
+
* Tests each database connection from the config against PowerSync Cloud.
|
|
4
|
+
* Uses the instance ID so secret_refs can be resolved.
|
|
5
|
+
*/
|
|
6
|
+
export async function testCloudConnections(client, linked, connections) {
|
|
7
|
+
const results = [];
|
|
8
|
+
for (const connection of connections) {
|
|
9
|
+
const response = await client.testConnection(routes.TestConnectionRequest.encode({
|
|
10
|
+
app_id: linked.project_id,
|
|
11
|
+
connection: connection,
|
|
12
|
+
id: linked.instance_id,
|
|
13
|
+
org_id: linked.org_id
|
|
14
|
+
}));
|
|
15
|
+
results.push({
|
|
16
|
+
connectionName: connection.name ?? 'unnamed',
|
|
17
|
+
response
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return results;
|
|
21
|
+
}
|
|
22
|
+
/** Pretty-print test connection response for error output. */
|
|
23
|
+
export function formatTestConnectionFailure(response, connectionName) {
|
|
24
|
+
const lines = [
|
|
25
|
+
`Failed to test connection for connection "${connectionName}":`,
|
|
26
|
+
'',
|
|
27
|
+
' Overall success: ' + String(response.success),
|
|
28
|
+
' Error: ' + (response.error ?? '(none)'),
|
|
29
|
+
'',
|
|
30
|
+
' Checks:',
|
|
31
|
+
' • connection.success: ' + String(response.connection?.success ?? '—'),
|
|
32
|
+
' • connection.reachable: ' + String(response.connection?.reachable ?? '—'),
|
|
33
|
+
' • configuration.success: ' + String(response.configuration?.success ?? '—')
|
|
34
|
+
];
|
|
35
|
+
return lines.join('\n');
|
|
36
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PowerSyncManagementClient } from '@powersync/management-client';
|
|
2
|
+
type InstanceConfigResponse = Awaited<ReturnType<PowerSyncManagementClient['getInstanceConfig']>>;
|
|
3
|
+
export type CloudLinkValidationInput = {
|
|
4
|
+
instanceId?: string;
|
|
5
|
+
orgId: string;
|
|
6
|
+
projectId: string;
|
|
7
|
+
};
|
|
8
|
+
export type ValidateCloudLinkConfigOptions = {
|
|
9
|
+
cloudClient: PowerSyncManagementClient;
|
|
10
|
+
input: CloudLinkValidationInput;
|
|
11
|
+
validateInstance?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type ValidateCloudLinkConfigResult = {
|
|
14
|
+
instanceConfig?: InstanceConfigResponse;
|
|
15
|
+
};
|
|
16
|
+
export declare function validateCloudLinkConfig(options: ValidateCloudLinkConfigOptions): Promise<ValidateCloudLinkConfigResult>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createAccountsHubClient, OBJECT_ID_REGEX } from '@powersync/cli-core';
|
|
2
|
+
function ensureObjectId(value, flagName) {
|
|
3
|
+
if (!OBJECT_ID_REGEX.test(value)) {
|
|
4
|
+
throw new Error(`Invalid ${flagName} "${value}". Expected a BSON ObjectID (24 hex characters).`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export async function validateCloudLinkConfig(options) {
|
|
8
|
+
const { cloudClient, input, validateInstance = false } = options;
|
|
9
|
+
const { instanceId, orgId, projectId } = input;
|
|
10
|
+
ensureObjectId(orgId, '--org-id');
|
|
11
|
+
ensureObjectId(projectId, '--project-id');
|
|
12
|
+
const accountsClient = createAccountsHubClient();
|
|
13
|
+
try {
|
|
14
|
+
await accountsClient.getOrganization({ id: orgId });
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
throw new Error(`Organization ${orgId} was not found or is not accessible with the current token.`);
|
|
18
|
+
}
|
|
19
|
+
let projects;
|
|
20
|
+
try {
|
|
21
|
+
projects = await accountsClient.listProjects({ id: projectId, org_id: orgId });
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
throw new Error(`Project ${projectId} was not found in organization ${orgId}, or is not accessible with the current token.`);
|
|
25
|
+
}
|
|
26
|
+
if ((projects.total ?? projects.objects?.length ?? 0) < 1) {
|
|
27
|
+
throw new Error(`Project ${projectId} was not found in organization ${orgId}, or is not accessible with the current token.`);
|
|
28
|
+
}
|
|
29
|
+
if (!validateInstance) {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
if (!instanceId) {
|
|
33
|
+
throw new Error('Instance validation requested but no instance ID was provided.');
|
|
34
|
+
}
|
|
35
|
+
ensureObjectId(instanceId, '--instance-id');
|
|
36
|
+
try {
|
|
37
|
+
const instanceConfig = await cloudClient.getInstanceConfig({
|
|
38
|
+
app_id: projectId,
|
|
39
|
+
id: instanceId,
|
|
40
|
+
org_id: orgId
|
|
41
|
+
});
|
|
42
|
+
return { instanceConfig };
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
throw new Error(`Instance ${instanceId} was not found in project ${projectId} in organization ${orgId}, or is not accessible with the current token.`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ResolvedCloudCLIConfig } from '@powersync/cli-schemas';
|
|
2
|
+
import { PowerSyncManagementClient } from '@powersync/management-client';
|
|
3
|
+
export declare const DEFAULT_DEPLOY_TIMEOUT_MS: number;
|
|
4
|
+
type DeployStatus = 'completed' | 'failed' | 'pending' | 'running';
|
|
5
|
+
export type WaitForOperationParams = {
|
|
6
|
+
client: PowerSyncManagementClient;
|
|
7
|
+
instanceId: string;
|
|
8
|
+
linked: ResolvedCloudCLIConfig;
|
|
9
|
+
operationId: string;
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Waits for the deployment operation to complete by polling the instance status until it detects a 'completed' or 'failed' status, or until a timeout is reached. Returns the final status of the deployment operation.
|
|
14
|
+
*/
|
|
15
|
+
export declare function waitForOperationStatusChange(params: WaitForOperationParams): Promise<DeployStatus>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const STATUS_POLL_INTERVAL_MS = 5000;
|
|
2
|
+
export const DEFAULT_DEPLOY_TIMEOUT_MS = 5 * 60 * 1000;
|
|
3
|
+
/**
|
|
4
|
+
* Waits for the deployment operation to complete by polling the instance status until it detects a 'completed' or 'failed' status, or until a timeout is reached. Returns the final status of the deployment operation.
|
|
5
|
+
*/
|
|
6
|
+
export async function waitForOperationStatusChange(params) {
|
|
7
|
+
const { client, instanceId, linked, operationId, timeoutMs = DEFAULT_DEPLOY_TIMEOUT_MS } = params;
|
|
8
|
+
const startedAt = Date.now();
|
|
9
|
+
for (;;) {
|
|
10
|
+
const result = await client.getInstanceStatus({
|
|
11
|
+
app_id: linked.project_id,
|
|
12
|
+
id: instanceId,
|
|
13
|
+
org_id: linked.org_id
|
|
14
|
+
});
|
|
15
|
+
const operation = result.operations?.find((op) => op.id === operationId);
|
|
16
|
+
if (!operation) {
|
|
17
|
+
throw new Error(`Operation with ID ${operationId} not found for instance ${instanceId}.`);
|
|
18
|
+
}
|
|
19
|
+
const status = operation.status;
|
|
20
|
+
if (status === 'failed' || status === 'completed')
|
|
21
|
+
return status;
|
|
22
|
+
if (status === undefined) {
|
|
23
|
+
// No operation or unknown status; treat as failed to avoid infinite loop
|
|
24
|
+
return 'failed';
|
|
25
|
+
}
|
|
26
|
+
if (Date.now() - startedAt >= timeoutMs) {
|
|
27
|
+
throw new Error(`Deployment did not complete within ${Math.round(timeoutMs / 1000)} seconds. Check instance status and try again.`);
|
|
28
|
+
}
|
|
29
|
+
await new Promise((resolve) => {
|
|
30
|
+
setTimeout(resolve, STATUS_POLL_INTERVAL_MS);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type WriteCloudLinkOptions = {
|
|
2
|
+
instanceId: string;
|
|
3
|
+
orgId: string;
|
|
4
|
+
projectId: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Writes or updates cli.yaml with Cloud instance link (type: cloud, instance_id, org_id, project_id).
|
|
8
|
+
* Creates a new file if it does not exist.
|
|
9
|
+
*/
|
|
10
|
+
export declare function writeCloudLink(projectDir: string, options: WriteCloudLinkOptions): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CLI_FILENAME, parseYamlFile } from '@powersync/cli-core';
|
|
2
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { Document } from 'yaml';
|
|
5
|
+
/**
|
|
6
|
+
* Writes or updates cli.yaml with Cloud instance link (type: cloud, instance_id, org_id, project_id).
|
|
7
|
+
* Creates a new file if it does not exist.
|
|
8
|
+
*/
|
|
9
|
+
export function writeCloudLink(projectDir, options) {
|
|
10
|
+
const { instanceId, orgId, projectId } = options;
|
|
11
|
+
const linkPath = join(projectDir, CLI_FILENAME);
|
|
12
|
+
if (!existsSync(projectDir)) {
|
|
13
|
+
mkdirSync(projectDir, { recursive: true });
|
|
14
|
+
}
|
|
15
|
+
const doc = existsSync(linkPath) ? parseYamlFile(linkPath) : new Document();
|
|
16
|
+
doc.set('type', 'cloud');
|
|
17
|
+
doc.set('instance_id', instanceId);
|
|
18
|
+
doc.set('org_id', orgId);
|
|
19
|
+
doc.set('project_id', projectId);
|
|
20
|
+
writeFileSync(linkPath, doc.toString(), 'utf8');
|
|
21
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { routes } from '@powersync/management-types';
|
|
2
|
+
export type DiagnosticsResponse = routes.InstanceDiagnosticsResponse;
|
|
3
|
+
export type SyncRulesSection = NonNullable<DiagnosticsResponse['active_sync_rules']>;
|
|
4
|
+
export declare function formatDiagnosticsHuman(diagnostics: DiagnosticsResponse): string;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const INDENT = ' ';
|
|
2
|
+
const BULLET = '•';
|
|
3
|
+
function pad(level) {
|
|
4
|
+
return INDENT.repeat(level);
|
|
5
|
+
}
|
|
6
|
+
/** Format a date value as "ISO (local)" for human output. Returns raw value if not parseable. */
|
|
7
|
+
function formatDate(value) {
|
|
8
|
+
if (value === undefined || value === null)
|
|
9
|
+
return '—';
|
|
10
|
+
const date = new Date(value);
|
|
11
|
+
if (Number.isNaN(date.getTime()))
|
|
12
|
+
return String(value);
|
|
13
|
+
return `${date.toISOString()} (${date.toLocaleString()})`;
|
|
14
|
+
}
|
|
15
|
+
function formatErrors(errors, indentLevel) {
|
|
16
|
+
if (!errors?.length)
|
|
17
|
+
return '';
|
|
18
|
+
const p = pad(indentLevel);
|
|
19
|
+
return errors.map((e) => `${p}${BULLET} [${e.level}] ${e.message}${e.ts ? ` (${e.ts})` : ''}`).join('\n');
|
|
20
|
+
}
|
|
21
|
+
function formatConnectionsSection(connections, indentLevel) {
|
|
22
|
+
if (!connections?.length)
|
|
23
|
+
return `${pad(indentLevel)}(no connections)\n`;
|
|
24
|
+
const p = pad(indentLevel);
|
|
25
|
+
const lines = [];
|
|
26
|
+
for (const conn of connections) {
|
|
27
|
+
const status = conn.connected ? 'connected' : 'disconnected';
|
|
28
|
+
lines.push(`${p}${BULLET} ${conn.id}`, `${p} Postgres URI: ${conn.postgres_uri ?? '—'}`, `${p} Status: ${status}`);
|
|
29
|
+
if (conn.errors?.length) {
|
|
30
|
+
lines.push(`${p} Errors:`, formatErrors(conn.errors, indentLevel + 2));
|
|
31
|
+
}
|
|
32
|
+
lines.push('');
|
|
33
|
+
}
|
|
34
|
+
return lines.join('\n').trimEnd() + '\n';
|
|
35
|
+
}
|
|
36
|
+
function formatSyncRulesSection(section, indentLevel) {
|
|
37
|
+
const p = pad(indentLevel);
|
|
38
|
+
const lines = [];
|
|
39
|
+
if (section.errors?.length) {
|
|
40
|
+
lines.push(`${p}Errors:`, formatErrors(section.errors, indentLevel + 1), '');
|
|
41
|
+
}
|
|
42
|
+
if (section.connections?.length) {
|
|
43
|
+
lines.push(`${p}Connections:`);
|
|
44
|
+
for (const conn of section.connections) {
|
|
45
|
+
lines.push(`${p} ${BULLET} ${conn.tag ?? conn.id} (slot: ${conn.slot_name ?? '—'})`, `${p} Initial replication done: ${conn.initial_replication_done}`);
|
|
46
|
+
if (conn.last_lsn != null)
|
|
47
|
+
lines.push(`${p} Last LSN: ${conn.last_lsn}`);
|
|
48
|
+
if (conn.last_keepalive_ts != null)
|
|
49
|
+
lines.push(`${p} Last keepalive: ${formatDate(conn.last_keepalive_ts)}`);
|
|
50
|
+
if (conn.last_checkpoint_ts != null)
|
|
51
|
+
lines.push(`${p} Last checkpoint: ${formatDate(conn.last_checkpoint_ts)}`);
|
|
52
|
+
if (conn.replication_lag_bytes != null)
|
|
53
|
+
lines.push(`${p} Replication lag: ${conn.replication_lag_bytes} bytes`);
|
|
54
|
+
if (conn.tables?.length) {
|
|
55
|
+
lines.push(`${p} Tables:`);
|
|
56
|
+
for (const table of conn.tables) {
|
|
57
|
+
const name = `${table.schema}.${table.name}`;
|
|
58
|
+
const repl = table.replication_id?.length ? table.replication_id.join(', ') : '—';
|
|
59
|
+
lines.push(`${p} - ${name} (replication_id: ${repl})`, `${p} used_for_replication: ${table.data_queries || table.parameter_queries ? 'yes' : 'no'}`, formatErrors(table.errors, indentLevel + 3));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
lines.push('');
|
|
64
|
+
}
|
|
65
|
+
if (section.content != null && section.content !== '') {
|
|
66
|
+
lines.push(`${p}Content:`, `${p} ${section.content.split('\n').join(`\n${p} `)}`);
|
|
67
|
+
}
|
|
68
|
+
return lines.join('\n').trimEnd() || `${p}(no data)\n`;
|
|
69
|
+
}
|
|
70
|
+
export function formatDiagnosticsHuman(diagnostics) {
|
|
71
|
+
const sections = [];
|
|
72
|
+
sections.push('═══ Connections ═══', formatConnectionsSection(diagnostics.connections ?? [], 0));
|
|
73
|
+
if (diagnostics.active_sync_rules != null) {
|
|
74
|
+
sections.push('═══ Active Sync Config ═══', formatSyncRulesSection(diagnostics.active_sync_rules, 0));
|
|
75
|
+
}
|
|
76
|
+
if (diagnostics.deploying_sync_rules != null) {
|
|
77
|
+
sections.push('═══ Deploying Sync Config ═══', formatSyncRulesSection(diagnostics.deploying_sync_rules, 0));
|
|
78
|
+
}
|
|
79
|
+
return sections.join('\n').trimEnd();
|
|
80
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A pluggable function signature for configuring a specific IDE.
|
|
3
|
+
* Receives the workspace root (for IDE settings), the list of discovered project
|
|
4
|
+
* directories to scan, and a log callback for producing output.
|
|
5
|
+
*/
|
|
6
|
+
export type IdeConfigurator = (workspaceRoot: string, projectDirs: string[], log: (message: string) => void) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Configures the VSCode workspace for PowerSync YAML editing and prints guidance:
|
|
9
|
+
* - Writes/merges .vscode/settings.json with yaml.customTags so the !env tag is recognised.
|
|
10
|
+
* - Scans each projectDir for known config files and prepends a yaml-language-server schema
|
|
11
|
+
* comment to any file that does not already have one.
|
|
12
|
+
* - Prints a summary of changes, extension recommendation, and schema comment reference.
|
|
13
|
+
*/
|
|
14
|
+
export declare function configureVscodeIde(workspaceRoot: string, projectDirs: string[], log: (message: string) => void): void;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ux } from '@oclif/core';
|
|
2
|
+
import { CLI_FILENAME, SERVICE_FILENAME, SYNC_FILENAME, YAML_CLI_SCHEMA, YAML_SERVICE_SCHEMA, YAML_SYNC_RULES_SCHEMA } from '@powersync/cli-core';
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
const VSCODE_YAML_TAGS = ['!env scalar'];
|
|
6
|
+
/** Maps each known config filename to its yaml-language-server schema comment. */
|
|
7
|
+
const YAML_SCHEMA_COMMENTS = {
|
|
8
|
+
[CLI_FILENAME]: YAML_CLI_SCHEMA,
|
|
9
|
+
[SERVICE_FILENAME]: YAML_SERVICE_SCHEMA,
|
|
10
|
+
[SYNC_FILENAME]: YAML_SYNC_RULES_SCHEMA
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Configures the VSCode workspace for PowerSync YAML editing and prints guidance:
|
|
14
|
+
* - Writes/merges .vscode/settings.json with yaml.customTags so the !env tag is recognised.
|
|
15
|
+
* - Scans each projectDir for known config files and prepends a yaml-language-server schema
|
|
16
|
+
* comment to any file that does not already have one.
|
|
17
|
+
* - Prints a summary of changes, extension recommendation, and schema comment reference.
|
|
18
|
+
*/
|
|
19
|
+
export function configureVscodeIde(workspaceRoot, projectDirs, log) {
|
|
20
|
+
const vscodeDir = join(workspaceRoot, '.vscode');
|
|
21
|
+
const settingsPath = join(vscodeDir, 'settings.json');
|
|
22
|
+
let settings = {};
|
|
23
|
+
if (existsSync(settingsPath)) {
|
|
24
|
+
try {
|
|
25
|
+
const raw = readFileSync(settingsPath, 'utf8');
|
|
26
|
+
settings = JSON.parse(raw);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// If invalid JSON, overwrite with our settings.
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const currentTags = (settings['yaml.customTags'] ?? []);
|
|
33
|
+
settings['yaml.customTags'] = [...new Set([...currentTags, ...VSCODE_YAML_TAGS])];
|
|
34
|
+
mkdirSync(vscodeDir, { recursive: true });
|
|
35
|
+
writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
|
|
36
|
+
const filesUpdated = [];
|
|
37
|
+
for (const projectDir of projectDirs) {
|
|
38
|
+
for (const [filename, schemaComment] of Object.entries(YAML_SCHEMA_COMMENTS)) {
|
|
39
|
+
const filePath = join(projectDir, filename);
|
|
40
|
+
if (!existsSync(filePath))
|
|
41
|
+
continue;
|
|
42
|
+
const content = readFileSync(filePath, 'utf8');
|
|
43
|
+
if (!content.includes('yaml-language-server:')) {
|
|
44
|
+
writeFileSync(filePath, `${schemaComment}\n\n${content}`);
|
|
45
|
+
filesUpdated.push(filePath);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const lines = [
|
|
50
|
+
ux.colorize('green', 'VSCode configured for PowerSync YAML editing!'),
|
|
51
|
+
'',
|
|
52
|
+
`✔ Updated .vscode/settings.json with yaml.customTags: ${JSON.stringify(VSCODE_YAML_TAGS)}`
|
|
53
|
+
];
|
|
54
|
+
if (filesUpdated.length > 0) {
|
|
55
|
+
lines.push('', 'Added yaml-language-server schema comments to:');
|
|
56
|
+
for (const f of filesUpdated) {
|
|
57
|
+
lines.push(` ✔ ${f}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
lines.push('', ux.colorize('cyan', 'Recommended: Install the YAML extension'), 'Install the Red Hat YAML extension for VSCode to get schema validation and autocompletion:', ux.colorize('blue', ' https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml'), '', ux.colorize('cyan', 'Language server schema comments'), 'The following comments at the top of each YAML config file activate schema support.', 'They are added automatically when you run powersync init, but you can also add them manually:', '', ux.colorize('dim', '# service.yaml'), ux.colorize('gray', YAML_SERVICE_SCHEMA), '', ux.colorize('dim', '# sync-config.yaml'), ux.colorize('gray', YAML_SYNC_RULES_SCHEMA), '', ux.colorize('dim', '# cli.yaml'), ux.colorize('gray', YAML_CLI_SCHEMA));
|
|
61
|
+
log(lines.join('\n'));
|
|
62
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server login handshake (asymmetric encryption)
|
|
3
|
+
*
|
|
4
|
+
* Flow:
|
|
5
|
+
* 1. CLI starts a local HTTP server and generates an ephemeral RSA keypair.
|
|
6
|
+
* 2. CLI opens the dashboard with a base64-encoded `request` param containing
|
|
7
|
+
* { redirect_url, public_key }. The dashboard learns where to send the token
|
|
8
|
+
* and how to encrypt it.
|
|
9
|
+
* 3. User creates a token in the dashboard. The dashboard encrypts the token
|
|
10
|
+
* with the CLI's public key and redirects the browser to redirect_url with
|
|
11
|
+
* the encrypted token in the query string (GET), so the secret never appears in history.
|
|
12
|
+
* 4. CLI receives GET /response?token=<base64> (see decryptTokenFromDashboard), decrypts it,
|
|
13
|
+
* and stores the plaintext token.
|
|
14
|
+
*
|
|
15
|
+
* Why encryption:
|
|
16
|
+
* In some cases the dashboard needs to hand the token back via a URL (e.g.
|
|
17
|
+
* redirect or opening the CLI callback URL in the browser). If we sent the
|
|
18
|
+
* token in the query string or fragment, it would appear in browser history,
|
|
19
|
+
* logs, and referrers. Encrypting with the CLI's public key ensures only this
|
|
20
|
+
* CLI instance can decrypt the token; the value that might appear in a URL or
|
|
21
|
+
* in transit is ciphertext, not the secret.
|
|
22
|
+
*/
|
|
23
|
+
export declare function startPATLoginServer(signal: AbortSignal): Promise<{
|
|
24
|
+
address: string;
|
|
25
|
+
tokenPromise: Promise<string>;
|
|
26
|
+
}>;
|