Skip to content

Commit 4d83bad

Browse files
feat: improved YAML comments for pull operations
1 parent b018f1e commit 4d83bad

16 files changed

Lines changed: 768 additions & 432 deletions

File tree

.changeset/witty-cobras-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'powersync': patch
3+
---
4+
5+
Added YAML comments to the configuration files genered when running powersync pull instance

cli/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ $ npm install -g powersync
239239
$ powersync COMMAND
240240
running command...
241241
$ powersync (--version)
242-
powersync/0.9.2 linux-x64 node-v24.14.0
242+
powersync/0.9.2 darwin-arm64 node-v24.13.0
243243
$ powersync --help [COMMAND]
244244
USAGE
245245
$ powersync COMMAND

cli/bin/dev.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ setCliClientHeaders({
1111
'user-agent': `POWERSYNC_CLI/${packageJSON.version}`
1212
});
1313

14+
// Ensure pnpm scripts run in the shell's original cwd (pnpm sets INIT_CWD for this).
15+
if (process.env.INIT_CWD && process.env.INIT_CWD !== process.cwd()) {
16+
process.chdir(process.env.INIT_CWD);
17+
}
18+
1419
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1520

1621
await execute({
1722
development: true,
18-
dir: import.meta.url,
23+
// Force oclif to ignore the baked manifest so it resolves commands from TS via tsx.
1924
loadOptions: {
25+
ignoreManifest: true,
2026
root: path.resolve(__dirname, '..')
2127
}
2228
});

cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
"scripts": {
121121
"dev": "tsx bin/dev.js",
122122
"clean": "rm -rf dist tsconfig.tsbuildinfo",
123+
"build:ts": "tsc -b",
123124
"build": "tsc -b && pnpm prepack",
124125
"lint": "eslint",
125126
"postpack": "shx rm -f oclif.manifest.json",

cli/src/commands/init/cloud.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import {
99
YAML_SERVICE_SCHEMA,
1010
YAML_SYNC_RULES_SCHEMA
1111
} from '@powersync/cli-core';
12-
import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
12+
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
1313
import { dirname, join } from 'node:path';
1414
import { fileURLToPath } from 'node:url';
1515

16+
import { buildServiceYaml } from '../../utils/build-service-yaml.js';
17+
1618
const __dirname = dirname(fileURLToPath(import.meta.url));
1719
const TEMPLATES_DIR = join(__dirname, '..', '..', '..', 'templates');
1820

@@ -48,13 +50,25 @@ export default class InitCloud extends InstanceCommand {
4850
}
4951

5052
mkdirSync(targetDir, { recursive: true });
51-
cpSync(templatePath, targetDir, { recursive: true });
53+
copyFileSync(join(templatePath, CLI_FILENAME), join(targetDir, CLI_FILENAME));
54+
copyFileSync(join(templatePath, SYNC_FILENAME), join(targetDir, SYNC_FILENAME));
5255

5356
const servicePath = join(targetDir, SERVICE_FILENAME);
5457
const syncPath = join(targetDir, SYNC_FILENAME);
5558
const cliPath = join(targetDir, CLI_FILENAME);
5659

57-
writeFileSync(servicePath, `${YAML_SERVICE_SCHEMA}\n\n${readFileSync(servicePath, 'utf8')}`);
60+
const serviceTemplatePath = join(templatePath, 'service.template.yaml');
61+
const renderedServiceYaml = buildServiceYaml({
62+
baseConfig: {
63+
_type: 'cloud',
64+
name: 'my-cli-instance',
65+
region: 'us'
66+
},
67+
schemaHeader: YAML_SERVICE_SCHEMA,
68+
templatePath: serviceTemplatePath
69+
});
70+
71+
writeFileSync(servicePath, renderedServiceYaml);
5872
writeFileSync(syncPath, `${YAML_SYNC_RULES_SCHEMA}\n\n${readFileSync(syncPath, 'utf8')}`);
5973
writeFileSync(cliPath, `${YAML_CLI_SCHEMA}\n\n${readFileSync(cliPath, 'utf8')}`);
6074

cli/src/commands/init/self-hosted.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import {
99
YAML_SERVICE_SCHEMA,
1010
YAML_SYNC_RULES_SCHEMA
1111
} from '@powersync/cli-core';
12-
import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
12+
import { BaseServiceSelfHostedConfig } from '@powersync/cli-schemas';
13+
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
1314
import { dirname, join } from 'node:path';
1415
import { fileURLToPath } from 'node:url';
1516

17+
import { buildServiceYaml } from '../../utils/build-service-yaml.js';
18+
1619
const __dirname = dirname(fileURLToPath(import.meta.url));
1720
const TEMPLATES_DIR = join(__dirname, '..', '..', '..', 'templates');
1821

@@ -48,13 +51,38 @@ export default class InitSelfHosted extends InstanceCommand {
4851
}
4952

5053
mkdirSync(targetDir, { recursive: true });
51-
cpSync(templatePath, targetDir, { recursive: true });
54+
copyFileSync(join(templatePath, CLI_FILENAME), join(targetDir, CLI_FILENAME));
55+
copyFileSync(join(templatePath, SYNC_FILENAME), join(targetDir, SYNC_FILENAME));
5256

5357
const servicePath = join(targetDir, SERVICE_FILENAME);
5458
const syncPath = join(targetDir, SYNC_FILENAME);
5559
const cliPath = join(targetDir, CLI_FILENAME);
5660

57-
writeFileSync(servicePath, `${YAML_SERVICE_SCHEMA}\n\n${readFileSync(servicePath, 'utf8')}`);
61+
const serviceTemplatePath = join(templatePath, 'service.template.yaml');
62+
const renderedServiceYaml = buildServiceYaml({
63+
baseConfig: {
64+
_type: 'self-hosted',
65+
api: {
66+
tokens: ['use_a_better_token_in_production']
67+
},
68+
replication: {
69+
connections: []
70+
},
71+
// This is just used as a placeholder
72+
storage: {} as BaseServiceSelfHostedConfig['storage'],
73+
sync_config: {
74+
path: 'sync-config.yaml'
75+
},
76+
telemetry: {
77+
disable_telemetry_sharing: false
78+
}
79+
},
80+
schemaHeader: YAML_SERVICE_SCHEMA,
81+
templatePath: serviceTemplatePath,
82+
templateReplacementPaths: [['storage']]
83+
});
84+
85+
writeFileSync(servicePath, renderedServiceYaml);
5886
writeFileSync(syncPath, `${YAML_SYNC_RULES_SCHEMA}\n\n${readFileSync(syncPath, 'utf8')}`);
5987
writeFileSync(cliPath, `${YAML_CLI_SCHEMA}\n\n${readFileSync(cliPath, 'utf8')}`);
6088

cli/src/commands/pull/instance.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@ import {
1111
} from '@powersync/cli-core';
1212
import { ServiceCloudConfig } from '@powersync/cli-schemas';
1313
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
14-
import { join } from 'node:path';
15-
import { stringify } from 'yaml';
14+
import { dirname, join } from 'node:path';
15+
import { fileURLToPath } from 'node:url';
1616

1717
import { decodeFetchedCloudConfig } from '../../api/cloud/fetch-cloud-config.js';
1818
import { validateCloudLinkConfig } from '../../api/cloud/validate-cloud-link-config.js';
1919
import { writeCloudLink } from '../../api/cloud/write-cloud-link.js';
20+
import { buildServiceYaml } from '../../utils/build-service-yaml.js';
2021

2122
const SERVICE_FETCHED_FILENAME = 'service-fetched.yaml';
2223
const SYNC_FETCHED_FILENAME = 'sync-fetched.yaml';
2324

25+
const __dirname = dirname(fileURLToPath(import.meta.url));
26+
const TEMPLATES_DIR = join(__dirname, '..', '..', '..', 'templates');
27+
2428
const PULL_CONFIG_HEADER = `# PowerSync Cloud config (fetched from cloud)
2529
# yaml-language-server: $schema=https://unpkg.com/@powersync/cli-schemas@latest/json-schema/service-config.json
26-
#
27-
`;
30+
`.trim();
2831

2932
export default class PullInstance extends CloudInstanceCommand {
3033
static commandHelpGroup = CommandHelpGroup.PROJECT_SETUP;
@@ -124,7 +127,13 @@ export default class PullInstance extends CloudInstanceCommand {
124127
);
125128
}
126129

127-
const serviceYaml = PULL_CONFIG_HEADER + stringify(ServiceCloudConfig.encode(fetched.config));
130+
const fetchedEncodedConfig = ServiceCloudConfig.encode(fetched.config);
131+
const serviceTemplatePath = join(TEMPLATES_DIR, 'cloud', 'powersync', 'service.template.yaml');
132+
const serviceYaml = buildServiceYaml({
133+
baseConfig: fetchedEncodedConfig,
134+
schemaHeader: PULL_CONFIG_HEADER,
135+
templatePath: serviceTemplatePath
136+
});
128137

129138
const serviceOutputName = serviceExists ? SERVICE_FETCHED_FILENAME : SERVICE_FILENAME;
130139
const serviceOutputPath = join(projectDir, serviceOutputName);

0 commit comments

Comments
 (0)