-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings-parameter.ts
More file actions
75 lines (63 loc) · 2.45 KB
/
Copy pathsettings-parameter.ts
File metadata and controls
75 lines (63 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Plan, ParameterSetting, SpawnStatus, StatefulParameter, Utils } from '@codifycli/plugin-core';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { VscodeConfig } from './vscode.js';
type Settings = Record<string, unknown>;
export class SettingsParameter extends StatefulParameter<VscodeConfig, Settings> {
getSettings(): ParameterSetting {
return { type: 'object' };
}
override async refresh(): Promise<Settings | null> {
try {
const content = await fs.readFile(getSettingsPath(), 'utf8');
return JSON.parse(content) as Settings;
} catch {
return null;
}
}
async add(valueToAdd: Settings): Promise<void> {
await writeSettings(valueToAdd);
}
async modify(newValue: Settings, previousValue: Settings): Promise<void> {
const filePath = getSettingsPath();
let existing: Settings = {};
try {
existing = JSON.parse(await fs.readFile(filePath, 'utf8'));
} catch { /* file may not exist */ }
// Remove keys that were in the previous declaration but are no longer desired
for (const key of Object.keys(previousValue)) {
if (!(key in newValue)) {
delete existing[key];
}
}
// Apply all new/changed keys
Object.assign(existing, newValue);
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, JSON.stringify(existing, null, 2));
}
async remove(valueToRemove: Settings): Promise<void> {
const filePath = getSettingsPath();
try {
const existing = JSON.parse(await fs.readFile(filePath, 'utf8')) as Settings;
for (const key of Object.keys(valueToRemove)) {
delete existing[key];
}
await fs.writeFile(filePath, JSON.stringify(existing, null, 2));
} catch { /* nothing to do if file doesn't exist */ }
}
}
function getSettingsPath(): string {
return Utils.isMacOS()
? path.join(os.homedir(), 'Library', 'Application Support', 'Code', 'User', 'settings.json')
: path.join(os.homedir(), '.config', 'Code', 'User', 'settings.json');
}
async function writeSettings(settings: Settings): Promise<void> {
const filePath = getSettingsPath();
let existing: Settings = {};
try {
existing = JSON.parse(await fs.readFile(filePath, 'utf8'));
} catch { /* file may not exist yet */ }
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, JSON.stringify({ ...existing, ...settings }, null, 2));
}