Skip to content

Commit 4506cdc

Browse files
Add ability to configure custom YAML tags for VSCode.
1 parent aef9ced commit 4506cdc

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
const VSCODE_YAML_TAGS = ['!env scalar'];
5+
6+
/**
7+
* Writes or merges .vscode/settings.json in the workspace root so that YAML files
8+
* get proper schema support for the !env custom tag.
9+
*/
10+
export function writeVscodeSettingsForYamlEnv(workspaceRoot: string): void {
11+
const vscodeDir = join(workspaceRoot, '.vscode');
12+
const settingsPath = join(vscodeDir, 'settings.json');
13+
14+
let settings: Record<string, unknown> = {};
15+
if (existsSync(settingsPath)) {
16+
try {
17+
const raw = readFileSync(settingsPath, 'utf-8');
18+
settings = JSON.parse(raw) as Record<string, unknown>;
19+
} catch {
20+
// If invalid JSON, overwrite with our settings
21+
}
22+
}
23+
24+
const currentSettings = (settings['yaml.customTags'] ?? []) as string[];
25+
settings['yaml.customTags'] = [...currentSettings, ...VSCODE_YAML_TAGS];
26+
mkdirSync(vscodeDir, { recursive: true });
27+
writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
28+
}

cli/src/commands/init/cloud.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import { ux } from '@oclif/core';
1+
import { Flags, ux } from '@oclif/core';
22
import { cpSync, existsSync, mkdirSync } from 'node:fs';
33
import { dirname, join } from 'node:path';
44
import { fileURLToPath } from 'node:url';
55

66
import { InstanceCommand, PowerSyncCommand } from '@powersync/cli-core';
77

8-
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
import { writeVscodeSettingsForYamlEnv } from '../../api/write-vscode-settings-for-yaml-env.js';
99

10+
const __dirname = dirname(fileURLToPath(import.meta.url));
1011
const TEMPLATES_DIR = join(__dirname, '..', '..', '..', 'templates');
1112

1213
export default class InitCloud extends PowerSyncCommand {
1314
static description =
1415
'Copy a Cloud template into a config directory (default powersync/). Edit service.yaml then run link cloud and deploy.';
1516
static summary = 'Scaffold a PowerSync Cloud config directory from a template.';
1617
static flags = {
17-
...InstanceCommand.flags
18+
...InstanceCommand.flags,
19+
vscode: Flags.boolean({
20+
description: 'Configure the workspace with .vscode settings for YAML custom tags (!env).',
21+
default: false
22+
})
1823
};
1924

2025
async run(): Promise<void> {
2126
const { flags } = await this.parse(InitCloud);
22-
const { directory } = flags;
27+
const { directory, vscode } = flags;
2328
const targetDir = this.resolveProjectDir(flags);
2429

2530
if (existsSync(targetDir)) {
@@ -38,6 +43,10 @@ export default class InitCloud extends PowerSyncCommand {
3843
mkdirSync(targetDir, { recursive: true });
3944
cpSync(templatePath, targetDir, { recursive: true });
4045

46+
if (vscode) {
47+
writeVscodeSettingsForYamlEnv(process.cwd());
48+
}
49+
4150
const instructions = [
4251
'Create a new instance with ',
4352
ux.colorize('blue', 'powersync link cloud --create'),
@@ -48,15 +57,18 @@ export default class InitCloud extends PowerSyncCommand {
4857
'to deploy.'
4958
].join('\n');
5059

51-
this.log(
52-
[
53-
ux.colorize('green', 'Created PowerSync cloud project!'),
54-
'',
55-
ux.colorize('cyan', 'Configuration files are located in:'),
56-
ux.colorize('gray', targetDir),
57-
'',
58-
ux.colorize('yellow', instructions)
59-
].join('\n')
60-
);
60+
const lines = [
61+
ux.colorize('green', 'Created PowerSync cloud project!'),
62+
'',
63+
ux.colorize('cyan', 'Configuration files are located in:'),
64+
ux.colorize('gray', targetDir),
65+
'',
66+
ux.colorize('yellow', instructions)
67+
];
68+
if (vscode) {
69+
lines.splice(5, 0, ux.colorize('gray', 'Added .vscode/settings.json for YAML !env tag support.'));
70+
lines.splice(6, 0, '');
71+
}
72+
this.log(lines.join('\n'));
6173
}
6274
}
Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import { ux } from '@oclif/core';
1+
import { Flags, ux } from '@oclif/core';
22
import { cpSync, existsSync, mkdirSync } from 'node:fs';
33
import { dirname, join } from 'node:path';
44
import { fileURLToPath } from 'node:url';
55

66
import { InstanceCommand, PowerSyncCommand, SERVICE_FILENAME } from '@powersync/cli-core';
77

8-
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
import { writeVscodeSettingsForYamlEnv } from '../../api/write-vscode-settings-for-yaml-env.js';
99

10+
const __dirname = dirname(fileURLToPath(import.meta.url));
1011
const TEMPLATES_DIR = join(__dirname, '..', '..', '..', 'templates');
1112

1213
export default class InitSelfHosted extends PowerSyncCommand {
1314
static description =
1415
'Copy a self-hosted template into a config directory (default powersync/). Configure service.yaml with your self-hosted instance details.';
1516
static summary = 'Scaffold a PowerSync self-hosted config directory from a template.';
1617
static flags = {
17-
...InstanceCommand.flags
18+
...InstanceCommand.flags,
19+
vscode: Flags.boolean({
20+
description: 'Configure the workspace with .vscode settings for YAML custom tags (!env).',
21+
default: false
22+
})
1823
};
1924

2025
async run(): Promise<void> {
2126
const { flags } = await this.parse(InitSelfHosted);
22-
const { directory } = flags;
27+
const { directory, vscode } = flags;
2328
const targetDir = this.resolveProjectDir(flags);
2429

2530
if (existsSync(targetDir)) {
@@ -38,22 +43,29 @@ export default class InitSelfHosted extends PowerSyncCommand {
3843
mkdirSync(targetDir, { recursive: true });
3944
cpSync(templatePath, targetDir, { recursive: true });
4045

46+
if (vscode) {
47+
writeVscodeSettingsForYamlEnv(process.cwd());
48+
}
49+
4150
const instructions = [
4251
'Self Hosted projects currently require external configuration for starting and deploying.',
4352
`Configure the ${SERVICE_FILENAME} file with your self-hosted instance details.`,
4453
`See the Docker topic, with ${ux.colorize('blue', 'powersync docker --help')} for local development services.`,
4554
'Please refer to the PowerSync Self-Hosted documentation for more information.'
4655
].join('\n');
4756

48-
this.log(
49-
[
50-
ux.colorize('green', 'Created PowerSync self-hosted project!'),
51-
'',
52-
ux.colorize('gray', 'Configuration files are located in:'),
53-
ux.colorize('cyan', targetDir),
54-
'',
55-
ux.colorize('yellow', instructions)
56-
].join('\n')
57-
);
57+
const lines = [
58+
ux.colorize('green', 'Created PowerSync self-hosted project!'),
59+
'',
60+
ux.colorize('cyan', 'Configuration files are located in:'),
61+
ux.colorize('gray', targetDir),
62+
'',
63+
ux.colorize('yellow', instructions)
64+
];
65+
if (vscode) {
66+
lines.splice(5, 0, ux.colorize('gray', 'Added .vscode/settings.json for YAML !env tag support.'));
67+
lines.splice(6, 0, '');
68+
}
69+
this.log(lines.join('\n'));
5870
}
5971
}

0 commit comments

Comments
 (0)