Skip to content

Commit eff4b4c

Browse files
committed
feat: Add extension and settings support for vscode
1 parent 59a6acf commit eff4b4c

7 files changed

Lines changed: 304 additions & 85 deletions

File tree

completions-cron/src/__generated__/completions-index.ts

Lines changed: 32 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/resources/(resources)/vscode.mdx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,37 @@ title: vscode
33
description: A reference page for the vscode resource
44
---
55

6-
The vscode resource reference. This resource installs `vscode` to your system. Vscode is a popular
7-
lightweight code editor developed by Microsoft. Vscode supports many coding languages via plugins.
6+
The vscode resource reference. This resource installs `vscode` to your system and manages extensions
7+
and editor settings. Vscode is a popular lightweight code editor developed by Microsoft.
88
For more information on Vscode [see here](https://code.visualstudio.com)
99

1010
## Parameters:
1111

12-
- **directory**: *(string)* A custom directory to install the vscode application into. The default is
13-
`$HOME/Applications/`.
12+
- **directory**: *(string)* A custom directory to install the vscode application into. Defaults to
13+
`/Applications` on macOS and `$HOME/.local/bin` on Linux.
14+
15+
- **extensions**: *(string[])* A list of VS Code extension IDs to install (e.g. `"ms-python.python"`).
16+
Extensions are managed statefully — Codify will install missing extensions and uninstall ones removed
17+
from the list.
18+
19+
- **settings**: *(object)* Key-value pairs to merge into `settings.json`. On apply, the declared keys
20+
are written to the user settings file. On destroy, only the declared keys are removed.
1421

1522
## Example usage:
1623

1724
```json title="codify.jsonc"
1825
[
1926
{
20-
"type": "vscode"
27+
"type": "vscode",
28+
"extensions": [
29+
"ms-python.python",
30+
"ms-python.vscode-pylance",
31+
"eamodio.gitlens"
32+
],
33+
"settings": {
34+
"editor.fontSize": 14,
35+
"editor.formatOnSave": true
36+
}
2137
}
2238
]
2339
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default async function loadVscodeExtensions(): Promise<string[]> {
2+
const body = {
3+
filters: [{
4+
criteria: [{ filterType: 8, value: 'Microsoft.VisualStudio.Code' }],
5+
pageSize: 200,
6+
sortBy: 4,
7+
}],
8+
flags: 914,
9+
};
10+
11+
const response = await fetch('https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery', {
12+
method: 'POST',
13+
headers: {
14+
'Content-Type': 'application/json',
15+
'Accept': 'application/json;api-version=7.2-preview.1',
16+
},
17+
body: JSON.stringify(body),
18+
});
19+
20+
const data = await response.json() as any;
21+
return data.results[0].extensions.map(
22+
(e: any) => `${e.publisher.publisherName}.${e.extensionName}` as string
23+
);
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ArrayParameterSetting, SpawnStatus, StatefulParameter, getPty } from '@codifycli/plugin-core';
2+
3+
import { VscodeConfig } from './vscode.js';
4+
5+
export class ExtensionsParameter extends StatefulParameter<VscodeConfig, string[]> {
6+
getSettings(): ArrayParameterSetting {
7+
return {
8+
type: 'array',
9+
isElementEqual(desired, current) {
10+
return desired.toLowerCase() === current.toLowerCase();
11+
},
12+
};
13+
}
14+
15+
override async refresh(): Promise<string[] | null> {
16+
const $ = getPty();
17+
const result = await $.spawnSafe('code --list-extensions');
18+
if (result.status !== SpawnStatus.SUCCESS || result.data == null) {
19+
return null;
20+
}
21+
return result.data.split('\n').filter(Boolean);
22+
}
23+
24+
async add(toAdd: string[]): Promise<void> {
25+
const $ = getPty();
26+
for (const ext of toAdd) {
27+
await $.spawn(`code --install-extension ${ext} --force`, { interactive: true });
28+
}
29+
}
30+
31+
async modify(newValue: string[], previousValue: string[]): Promise<void> {
32+
const toAdd = newValue.filter((n) => !previousValue.some((p) => p.toLowerCase() === n.toLowerCase()));
33+
const toRemove = previousValue.filter((p) => !newValue.some((n) => n.toLowerCase() === p.toLowerCase()));
34+
await this.remove(toRemove);
35+
await this.add(toAdd);
36+
}
37+
38+
async remove(toRemove: string[]): Promise<void> {
39+
const $ = getPty();
40+
for (const ext of toRemove) {
41+
await $.spawnSafe(`code --uninstall-extension ${ext}`);
42+
}
43+
}
44+
}

src/resources/vscode/vscode-schema.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)