powersync 0.1.2 → 0.2.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.
@@ -4,10 +4,10 @@ import ora from 'ora';
4
4
  import * as PowerSyncClient from '../../service/powersync.client.js';
5
5
  import { bold, checkForValidEnvFile, red, yellow } from '../../utils.js';
6
6
  export default class InstanceDeactivate extends Command {
7
- static description = 'stop the current instance';
8
- static examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --instanceId=1234'];
7
+ static description = 'Stop the current instance.';
8
+ static examples = ['<%= config.bin %> <%= command.id %>'];
9
9
  static flags = {
10
- skipConfirmation: Flags.boolean({ char: 's', description: 'Ignore confirmation question' })
10
+ skipConfirmation: Flags.boolean({ char: 's', description: 'Ignore confirmation question.' })
11
11
  };
12
12
  async run() {
13
13
  checkForValidEnvFile(this.config.bin, true);
@@ -5,15 +5,15 @@ import ora from 'ora';
5
5
  import * as PowerSyncClient from '../../../service/powersync.client.js';
6
6
  import { bold, checkForValidEnvFile, green, red } from '../../../utils.js';
7
7
  export default class SyncRulesDeploy extends Command {
8
- static description = 'deploy sync rules from a local file to the current instance';
8
+ static description = 'Deploy sync rules from a local file to the current instance.';
9
9
  static examples = [
10
10
  '<%= config.bin %> <%= command.id %>',
11
11
  '<%= config.bin %> <%= command.id %> --filePath path/to/file',
12
12
  '<%= config.bin %> <%= command.id %> -f path/to/file'
13
13
  ];
14
14
  static flags = {
15
- filePath: Flags.string({ char: 'f', description: 'Path to the file', required: true }),
16
- skipConfirmation: Flags.boolean({ char: 's', description: 'Ignore confirmation question' })
15
+ filePath: Flags.string({ char: 'f', description: 'Path to the file.', required: true }),
16
+ skipConfirmation: Flags.boolean({ char: 's', description: 'Ignore confirmation question.' })
17
17
  };
18
18
  async run() {
19
19
  checkForValidEnvFile(this.config.bin, true);
@@ -5,17 +5,17 @@ import { LANGUAGES } from '../../../constants.js';
5
5
  import * as PowerSyncClient from '../../../service/powersync.client.js';
6
6
  import { checkForValidEnvFile, green, red } from '../../../utils.js';
7
7
  export default class SyncRulesGenerateSchema extends Command {
8
- static description = 'generate schema in your language from sync rules';
8
+ static description = 'Generate schema in your language from sync rules.';
9
9
  static examples = [
10
10
  '<%= config.bin %> <%= command.id %>',
11
11
  '<%= config.bin %> <%= command.id %> --filePath path/to/file',
12
12
  '<%= config.bin %> <%= command.id %> -f path/to/file'
13
13
  ];
14
14
  static flags = {
15
- filePath: Flags.string({ char: 'f', description: 'Path to the file', required: true }),
15
+ filePath: Flags.string({ char: 'f', description: 'Path to the file.', required: true }),
16
16
  language: Flags.string({
17
17
  char: 'l',
18
- description: 'Choose language for output',
18
+ description: 'Choose language for output.',
19
19
  options: LANGUAGES
20
20
  })
21
21
  };
@@ -41,7 +41,7 @@ export default class SyncRulesGenerateSchema extends Command {
41
41
  });
42
42
  const { errors } = syncRules;
43
43
  if (errors.length > 0) {
44
- ux.log(red(`❌ sync rule errors found:`));
44
+ ux.log(red(`❌ Sync rule errors found:`));
45
45
  errors.map((error) => {
46
46
  return ux.log(red(error.message));
47
47
  });
@@ -3,14 +3,14 @@ import fs from 'node:fs';
3
3
  import * as PowerSyncClient from '../../../service/powersync.client.js';
4
4
  import { checkForValidEnvFile, green, red } from '../../../utils.js';
5
5
  export default class SyncRulesValidate extends Command {
6
- static description = 'validate sync rules from a local file';
6
+ static description = 'Validate sync rules from a local file.';
7
7
  static examples = [
8
8
  '<%= config.bin %> <%= command.id %>',
9
9
  '<%= config.bin %> <%= command.id %> --filePath path/to/file',
10
10
  '<%= config.bin %> <%= command.id %> -f path/to/file'
11
11
  ];
12
12
  static flags = {
13
- filePath: Flags.string({ char: 'f', description: 'Path to the file', required: true })
13
+ filePath: Flags.string({ char: 'f', description: 'Path to the file.', required: true })
14
14
  };
15
15
  async run() {
16
16
  checkForValidEnvFile(this.config.bin, true);
@@ -1,20 +1,29 @@
1
1
  import { Command, ux } from '@oclif/core';
2
+ import * as AccountsClient from '../service/accounts.client.js';
2
3
  import * as PowerSyncClient from '../service/powersync.client.js';
3
- import { checkForValidEnvFile, green, red } from '../utils.js';
4
+ import { bold, checkForValidEnvFile, green, red } from '../utils.js';
4
5
  export default class InstancesList extends Command {
5
- static description = 'view list of instances';
6
+ static description = 'View list of instances.';
6
7
  static examples = ['<%= config.bin %> <%= command.id %>'];
7
8
  async run() {
8
9
  checkForValidEnvFile(this.config.bin, false);
10
+ const app = await AccountsClient.getCurrentPowerSyncApp();
9
11
  const list = await PowerSyncClient.getInstancesList();
10
12
  const { instances } = list;
11
13
  if (instances.length === 0) {
12
14
  return ux.log(`It seems like you have ${red('no')} instances. Run ${green('"powersync instance create"')} to create your first instance.`);
13
15
  }
16
+ ux.log(`Your ${bold('app')} is currently set to ${green(app.name)}.`);
17
+ if (process.env.INSTANCE_ID) {
18
+ const currentInstance = instances.find((instance) => instance.id === process.env.INSTANCE_ID);
19
+ if (currentInstance) {
20
+ ux.log(`Your ${bold('instance')} is currently set to ${green(currentInstance.name)}.\n`);
21
+ }
22
+ }
14
23
  ux.table(instances, {
15
24
  id: { header: 'ID' },
16
25
  name: { minWidth: 7 }
17
26
  }, { printLine: this.log.bind(this) });
18
- ux.log(`\nrun ${green(`"${this.config.bin} instance set"`)} to use one of the instances`);
27
+ ux.log(`\nRun ${green(`"${this.config.bin} instance set"`)} to use one of the instances.`);
19
28
  }
20
29
  }
@@ -11126,5 +11126,14 @@ const getPowerSyncApps = async (bearerToken) => {
11126
11126
  const apps = response.objects.filter((obj) => obj.features.includes(dist$4.ProjectFeatureFlags.POWERSYNC)).map((obj) => ({ name: obj.name, value: obj.id }));
11127
11127
  return apps;
11128
11128
  };
11129
+ const getCurrentPowerSyncApp = async () => {
11130
+ const headers = generateHeader(process.env.AUTH_TOKEN);
11131
+ const response = await accountsClient(headers).listApps({});
11132
+ const app = response.objects.find((obj) => obj.id === process.env.APP_ID);
11133
+ if (!app) {
11134
+ throw new Error("No app found.");
11135
+ }
11136
+ return app;
11137
+ };
11129
11138
 
11130
- export { accountsClient, getOrgs, getPowerSyncApps };
11139
+ export { accountsClient, getCurrentPowerSyncApp, getOrgs, getPowerSyncApps };
@@ -11494,6 +11494,18 @@ const generateHeader = (bearerToken) => ({
11494
11494
  Authorization: `Bearer ${bearerToken}`,
11495
11495
  ContentType: "application/json"
11496
11496
  });
11497
+ const checkAppId = (appId) => {
11498
+ if (!appId)
11499
+ throw new Error("No app id found");
11500
+ };
11501
+ const checkOrgId = (orgId) => {
11502
+ if (!orgId)
11503
+ throw new Error("No org id found");
11504
+ };
11505
+ const checkInstanceId = (instanceId) => {
11506
+ if (!instanceId)
11507
+ throw new Error("No instance id found");
11508
+ };
11497
11509
 
11498
11510
  const ALIAS = Symbol.for('yaml.alias');
11499
11511
  const DOC = Symbol.for('yaml.document');
@@ -37347,85 +37359,132 @@ const schemaGenerators = {
37347
37359
  };
37348
37360
 
37349
37361
  config_1({ path: ENV_FILE_PATH });
37350
- const { APP_ID, CARDINAL_TOKEN, INSTANCE_ID, ORG_ID } = process.env;
37362
+ const { APP_ID, AUTH_TOKEN, INSTANCE_ID, ORG_ID } = process.env;
37351
37363
  var ENV_VARIABLES;
37352
37364
  (function(ENV_VARIABLES2) {
37353
37365
  ENV_VARIABLES2["APP_ID"] = "APP_ID";
37354
- ENV_VARIABLES2["CARDINAL_TOKEN"] = "CARDINAL_TOKEN";
37366
+ ENV_VARIABLES2["AUTH_TOKEN"] = "AUTH_TOKEN";
37355
37367
  ENV_VARIABLES2["INSTANCE_ID"] = "INSTANCE_ID";
37356
37368
  ENV_VARIABLES2["ORG_ID"] = "ORG_ID";
37357
37369
  })(ENV_VARIABLES || (ENV_VARIABLES = {}));
37358
- const powerSyncClient = (requiresInstanceId = true) => {
37359
- if (!CARDINAL_TOKEN) {
37360
- throw new Error(`${ENV_VARIABLES.CARDINAL_TOKEN} is not set in environment`);
37361
- }
37362
- if (!APP_ID) {
37363
- throw new Error(`${ENV_VARIABLES.APP_ID} is not set in environment`);
37370
+ const powerSyncClient = ({ authToken } = {}) => {
37371
+ const finalToken = authToken || AUTH_TOKEN;
37372
+ if (!finalToken) {
37373
+ throw new Error("No auth token found");
37364
37374
  }
37365
- if (!ORG_ID) {
37366
- throw new Error(`${ENV_VARIABLES.ORG_ID} is not set in environment`);
37367
- }
37368
- if (requiresInstanceId && !INSTANCE_ID) {
37369
- throw new Error(`${ENV_VARIABLES.INSTANCE_ID} is not set in environment. You need to set an instance using \`powersync instance set\``);
37370
- }
37371
- const HEADERS = generateHeader(CARDINAL_TOKEN);
37375
+ const HEADERS = generateHeader(finalToken);
37372
37376
  return new Client_1({
37373
37377
  client: dist$4.createWebNetworkClient({ headers: HEADERS }),
37374
37378
  endpoint: ENDPOINTS.POWERSYNC_URL
37375
37379
  });
37376
37380
  };
37377
- const getInstancesList = async () => powerSyncClient(false).listInstances({
37378
- app_id: APP_ID,
37379
- org_id: ORG_ID
37380
- });
37381
- const getInstanceConfig = async () => powerSyncClient().getInstanceConfig({
37382
- app_id: APP_ID,
37383
- id: INSTANCE_ID,
37384
- org_id: ORG_ID
37385
- });
37386
- const getInstanceDiagnostics = async () => powerSyncClient().getInstanceDiagnostics({
37387
- app_id: APP_ID,
37388
- id: INSTANCE_ID,
37389
- org_id: ORG_ID
37390
- });
37391
- const getInstanceSchema = async () => powerSyncClient().getInstanceSchema({
37392
- app_id: APP_ID,
37393
- id: INSTANCE_ID,
37394
- org_id: ORG_ID
37395
- });
37396
- const getInstanceStatus = async (instanceId) => powerSyncClient(!instanceId).getInstanceStatus({
37397
- app_id: APP_ID,
37398
- id: instanceId ?? INSTANCE_ID,
37399
- org_id: ORG_ID
37400
- });
37401
- const destroyInstance = async () => powerSyncClient().destroyInstance({
37402
- app_id: APP_ID,
37403
- id: INSTANCE_ID,
37404
- org_id: ORG_ID
37405
- });
37406
- const deactivateInstance = async () => powerSyncClient().deactivateInstance({
37407
- app_id: APP_ID,
37408
- id: INSTANCE_ID,
37409
- org_id: ORG_ID
37410
- });
37411
- const createInstance = async (name) => powerSyncClient(false).createInstance({
37412
- app_id: APP_ID,
37413
- name,
37414
- org_id: ORG_ID
37415
- });
37416
- const testConnection = async (connection) => powerSyncClient().testConnection({
37417
- app_id: APP_ID,
37418
- connection,
37419
- id: INSTANCE_ID,
37420
- org_id: ORG_ID
37421
- });
37422
- const deployInstance = async (config, instanceId) => powerSyncClient(false).deployInstance({
37423
- app_id: APP_ID,
37424
- config,
37425
- id: instanceId,
37426
- org_id: ORG_ID
37427
- });
37381
+ const getInstancesList = async () => {
37382
+ checkOrgId(ORG_ID);
37383
+ checkAppId(APP_ID);
37384
+ return powerSyncClient().listInstances({
37385
+ app_id: APP_ID,
37386
+ org_id: ORG_ID
37387
+ });
37388
+ };
37389
+ const getInstanceConfig = async () => {
37390
+ checkOrgId(ORG_ID);
37391
+ checkAppId(APP_ID);
37392
+ checkInstanceId(INSTANCE_ID);
37393
+ return powerSyncClient().getInstanceConfig({
37394
+ app_id: APP_ID,
37395
+ id: INSTANCE_ID,
37396
+ org_id: ORG_ID
37397
+ });
37398
+ };
37399
+ const getInstanceDiagnostics = async ({ appId, authToken, instanceId, orgId }) => {
37400
+ const finalAppId = appId || APP_ID;
37401
+ const finalInstanceId = instanceId || INSTANCE_ID;
37402
+ const finalOrgId = orgId || ORG_ID;
37403
+ checkOrgId(finalOrgId);
37404
+ checkAppId(finalAppId);
37405
+ checkInstanceId(finalInstanceId);
37406
+ return powerSyncClient({ authToken }).getInstanceDiagnostics({
37407
+ app_id: finalAppId,
37408
+ id: finalInstanceId,
37409
+ org_id: finalOrgId
37410
+ });
37411
+ };
37412
+ const getInstanceSchema = async () => {
37413
+ checkOrgId(ORG_ID);
37414
+ checkAppId(APP_ID);
37415
+ checkInstanceId(INSTANCE_ID);
37416
+ return powerSyncClient().getInstanceSchema({
37417
+ app_id: APP_ID,
37418
+ id: INSTANCE_ID,
37419
+ org_id: ORG_ID
37420
+ });
37421
+ };
37422
+ const getInstanceStatus = async (instanceId) => {
37423
+ const finalInstanceId = instanceId || INSTANCE_ID;
37424
+ checkOrgId(ORG_ID);
37425
+ checkAppId(APP_ID);
37426
+ checkInstanceId(finalInstanceId);
37427
+ return powerSyncClient().getInstanceStatus({
37428
+ app_id: APP_ID,
37429
+ id: instanceId ?? INSTANCE_ID,
37430
+ org_id: ORG_ID
37431
+ });
37432
+ };
37433
+ const destroyInstance = async () => {
37434
+ checkOrgId(ORG_ID);
37435
+ checkAppId(APP_ID);
37436
+ checkInstanceId(INSTANCE_ID);
37437
+ return powerSyncClient().destroyInstance({
37438
+ app_id: APP_ID,
37439
+ id: INSTANCE_ID,
37440
+ org_id: ORG_ID
37441
+ });
37442
+ };
37443
+ const deactivateInstance = async () => {
37444
+ checkOrgId(ORG_ID);
37445
+ checkAppId(APP_ID);
37446
+ checkInstanceId(INSTANCE_ID);
37447
+ return powerSyncClient().deactivateInstance({
37448
+ app_id: APP_ID,
37449
+ id: INSTANCE_ID,
37450
+ org_id: ORG_ID
37451
+ });
37452
+ };
37453
+ const createInstance = async (name) => {
37454
+ checkOrgId(ORG_ID);
37455
+ checkAppId(APP_ID);
37456
+ checkInstanceId(INSTANCE_ID);
37457
+ return powerSyncClient().createInstance({
37458
+ app_id: APP_ID,
37459
+ name,
37460
+ org_id: ORG_ID
37461
+ });
37462
+ };
37463
+ const testConnection = async (connection) => {
37464
+ checkOrgId(ORG_ID);
37465
+ checkAppId(APP_ID);
37466
+ checkInstanceId(INSTANCE_ID);
37467
+ return powerSyncClient().testConnection({
37468
+ app_id: APP_ID,
37469
+ connection,
37470
+ id: INSTANCE_ID,
37471
+ org_id: ORG_ID
37472
+ });
37473
+ };
37474
+ const deployInstance = async (config, instanceId) => {
37475
+ checkOrgId(ORG_ID);
37476
+ checkAppId(APP_ID);
37477
+ return powerSyncClient().deployInstance({
37478
+ app_id: APP_ID,
37479
+ config,
37480
+ id: instanceId,
37481
+ org_id: ORG_ID
37482
+ });
37483
+ };
37428
37484
  const validateSyncRules = async (syncRules) => {
37485
+ checkOrgId(ORG_ID);
37486
+ checkAppId(APP_ID);
37487
+ checkInstanceId(INSTANCE_ID);
37429
37488
  return powerSyncClient().validateSyncRules({
37430
37489
  app_id: APP_ID,
37431
37490
  id: INSTANCE_ID,
@@ -37434,6 +37493,9 @@ const validateSyncRules = async (syncRules) => {
37434
37493
  });
37435
37494
  };
37436
37495
  const deploySyncRules = async (syncRules) => {
37496
+ checkOrgId(ORG_ID);
37497
+ checkAppId(APP_ID);
37498
+ checkInstanceId(INSTANCE_ID);
37437
37499
  const config = await getInstanceConfig();
37438
37500
  if (!config.config) {
37439
37501
  throw new Error("No instance config found");
@@ -37449,11 +37511,16 @@ const deploySyncRules = async (syncRules) => {
37449
37511
  sync_rules: syncRules
37450
37512
  });
37451
37513
  };
37452
- const processSyncRules = async () => powerSyncClient().reprocessSyncRules({
37453
- app_id: APP_ID,
37454
- id: INSTANCE_ID,
37455
- org_id: ORG_ID
37456
- });
37514
+ const processSyncRules = async () => {
37515
+ checkOrgId(ORG_ID);
37516
+ checkAppId(APP_ID);
37517
+ checkInstanceId(INSTANCE_ID);
37518
+ powerSyncClient().reprocessSyncRules({
37519
+ app_id: APP_ID,
37520
+ id: INSTANCE_ID,
37521
+ org_id: ORG_ID
37522
+ });
37523
+ };
37457
37524
  const waitForStatusChange = async (instanceId) => {
37458
37525
  const interval = 5e3;
37459
37526
  try {
@@ -3,5 +3,17 @@ const generateHeader = (bearerToken) => ({
3
3
  Authorization: `Bearer ${bearerToken}`,
4
4
  ContentType: "application/json"
5
5
  });
6
+ const checkAppId = (appId) => {
7
+ if (!appId)
8
+ throw new Error("No app id found");
9
+ };
10
+ const checkOrgId = (orgId) => {
11
+ if (!orgId)
12
+ throw new Error("No org id found");
13
+ };
14
+ const checkInstanceId = (instanceId) => {
15
+ if (!instanceId)
16
+ throw new Error("No instance id found");
17
+ };
6
18
 
7
- export { generateHeader };
19
+ export { checkAppId, checkInstanceId, checkOrgId, generateHeader };
package/dist/utils.js CHANGED
@@ -3,10 +3,12 @@ import fs from 'node:fs';
3
3
  import { ENV_FILE_PATH } from './constants.js';
4
4
  import { ENV_VARIABLES } from './service/powersync.client.js';
5
5
  export const checkForValidEnvFile = (cliName, requiresInstanceId = false) => {
6
- if (!fs.existsSync(ENV_FILE_PATH)) {
6
+ const { APP_ID, AUTH_TOKEN, INSTANCE_ID, ORG_ID } = process.env;
7
+ const allEnvVariablesSet = APP_ID && AUTH_TOKEN && ORG_ID && (INSTANCE_ID || !requiresInstanceId);
8
+ if (!allEnvVariablesSet && !fs.existsSync(ENV_FILE_PATH)) {
7
9
  ux.error(`CLI has not been initialized. Please run ${green(`"${cliName} init"`)}.`, { exit: 1 });
8
10
  }
9
- const requiredProperties = [ENV_VARIABLES.APP_ID, ENV_VARIABLES.CARDINAL_TOKEN, ENV_VARIABLES.ORG_ID];
11
+ const requiredProperties = [ENV_VARIABLES.APP_ID, ENV_VARIABLES.AUTH_TOKEN, ENV_VARIABLES.ORG_ID];
10
12
  const missingProperties = requiredProperties.filter((prop) => !process.env[prop]);
11
13
  if (missingProperties.length > 0) {
12
14
  return ux.error(`It appears you have not initialized the CLI. Please run ${green(`"${cliName} init"`)}.`, {
@@ -14,12 +16,16 @@ export const checkForValidEnvFile = (cliName, requiresInstanceId = false) => {
14
16
  });
15
17
  }
16
18
  if (requiresInstanceId && !process.env[ENV_VARIABLES.INSTANCE_ID]) {
17
- return ux.error(`Missing INSTANCE_ID. Please set one by running ${green('"powersync instance set"')}.`, {
19
+ return ux.error(`This command requires a configured instance. Please set one by running ${green('"powersync instance set"')}.`, {
18
20
  exit: 1
19
21
  });
20
22
  }
21
23
  };
22
24
  export const updateEnvFileVariableValue = (variableName, newValue) => {
25
+ if (!fs.existsSync(ENV_FILE_PATH)) {
26
+ // We assume they are either using flags or exporting the variables themselves
27
+ return;
28
+ }
23
29
  try {
24
30
  const data = fs.readFileSync(ENV_FILE_PATH, 'utf8');
25
31
  const lines = data.split('\n');
@@ -41,7 +47,7 @@ export const updateEnvFileVariableValue = (variableName, newValue) => {
41
47
  fs.writeFileSync(ENV_FILE_PATH, updatedContent, 'utf8');
42
48
  }
43
49
  catch (error) {
44
- console.error(error);
50
+ throw new Error('Error updating environment file: ' + error);
45
51
  }
46
52
  };
47
53
  export const bold = (text) => ux.colorize('bold', text);