Skip to content

Commit d49966a

Browse files
committed
add code-server support (#497)
1 parent 24dcefb commit d49966a

File tree

7 files changed

+56
-24
lines changed

7 files changed

+56
-24
lines changed

Diff for: docs/common-issues.md

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ three ways:
5050
- linux:
5151
- Run `sudo chmod -R a+rw /usr/share/code`
5252
- Some Arch Linux: `sudo chmod -R a+rw /opt/visual-studio-code`
53+
- Code Server (docker): `sudo chmod -R a+rw '/usr/lib/code-server'`
54+
- code-server needs to force browser refresh (avoid caching) for configuration to take effect.
5355

5456
## Unsupported environment
5557

Diff for: docs/common-issues.zh-CN.md

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
- linux:
5151
- 执行 `sudo chmod -R a+rw /usr/share/code`
5252
- 一些 Arch Linux: `sudo chmod -R a+rw /opt/visual-studio-code`
53+
- Code Server (docker): `sudo chmod -R a+rw '/usr/lib/code-server'`
54+
- code-server 需要强制刷新浏览器(避免缓存)来使配置生效。
5355

5456
## 不支持的环境
5557

Diff for: src/background/Background.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ export class Background implements Disposable {
130130
* @memberof Background
131131
*/
132132
private async removeLegacyCssPatch() {
133-
const hasInstalled = await this.cssFile.hasInstalled();
134-
if (!hasInstalled) {
135-
return;
136-
}
137-
await this.cssFile.uninstall();
133+
try {
134+
const hasInstalled = await this.cssFile.hasInstalled();
135+
if (!hasInstalled) {
136+
return;
137+
}
138+
await this.cssFile.uninstall();
139+
} catch (ex) {}
138140
}
139141

140142
/**
@@ -180,7 +182,7 @@ export class Background implements Disposable {
180182
}
181183

182184
const scriptContent = PatchGenerator.create(this.config);
183-
await this.jsFile.applyPatches(scriptContent);
185+
return this.jsFile.applyPatches(scriptContent);
184186
}
185187

186188
// #endregion
@@ -204,8 +206,9 @@ export class Background implements Disposable {
204206
if (this.config.enabled) {
205207
// 此时一般为 vscode更新、background更新
206208
if ([EFilePatchType.Legacy, EFilePatchType.None].includes(patchType)) {
207-
await this.applyPatch();
208-
vsHelp.showInfoRestart(l10n.t('Background has been changed! Please restart.'));
209+
if (await this.applyPatch()) {
210+
vsHelp.showInfoRestart(l10n.t('Background has been changed! Please restart.'));
211+
}
209212
}
210213
}
211214

@@ -217,9 +220,9 @@ export class Background implements Disposable {
217220
return;
218221
}
219222

220-
// 0~500ms 的延时,对于可能的多实例,错开对于文件的操作
223+
// 50~550ms 的延时,对于可能的多实例,错开对于文件的操作
221224
// 虽然有锁了,但这样更安心 =。=
222-
await utils.sleep(200 + ~~(Math.random() * 800));
225+
await utils.sleep(50 + ~~(Math.random() * 500));
223226

224227
this.onConfigChange();
225228
})

Diff for: src/background/PatchFile/PatchFile.base.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ export abstract class AbsPatchFile {
9999
await utils.sudoExec(cmdarg, { name: 'Background Extension' });
100100
return true;
101101
} catch (e: any) {
102-
await vsc.window.showErrorMessage(e.message);
102+
vsc.window.showErrorMessage(e.message, { title: 'Common Issue' }).then(confirm => {
103+
if (!confirm) {
104+
return;
105+
}
106+
const helpLink =
107+
'https://github.com/shalldie/vscode-background/blob/master/docs/common-issues.md#read-only-file-system';
108+
109+
vsc!.env!.openExternal(vsc!.Uri.parse(helpLink));
110+
});
103111
return false;
104112
} finally {
105113
await fs.promises.rm(tempFilePath, { force: true });
@@ -119,10 +127,10 @@ export abstract class AbsPatchFile {
119127
*
120128
* @abstract
121129
* @param {string} patch
122-
* @return {*} {Promise<void>}
130+
* @return {*} {Promise<boolean>}
123131
* @memberof AbsPatchFile
124132
*/
125-
public abstract applyPatches(patch: string): Promise<void>;
133+
public abstract applyPatches(patch: string): Promise<boolean>;
126134

127135
/**
128136
* Get the clean content without patches.

Diff for: src/background/PatchFile/PatchFile.javascript.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { AbsPatchFile } from './PatchFile.base';
1212
* @extends {AbsPatchFile}
1313
*/
1414
export class JsPatchFile extends AbsPatchFile {
15-
public async applyPatches(patchContent: string) {
15+
public async applyPatches(patchContent: string): Promise<boolean> {
1616
let content = await this.getContent();
1717
content = this.cleanPatches(content);
1818
content += [
@@ -22,7 +22,7 @@ export class JsPatchFile extends AbsPatchFile {
2222
'// vscode-background-end'
2323
].join('\n');
2424

25-
await this.write(content);
25+
return this.write(content);
2626
}
2727

2828
protected cleanPatches(content: string): string {

Diff for: src/utils/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export namespace utils {
1010
*/
1111
export const isZHCN = /^zh/.test(vsc?.env.language || '');
1212

13+
/**
14+
* if desktop
15+
*
16+
* desktop: `desktop`
17+
* code-server: `server-distro`
18+
* See: https://code.visualstudio.com/api/references/vscode-api#env
19+
*/
20+
export const isDesktop = vsc?.env.appHost === 'desktop';
21+
1322
/**
1423
* 等待若干时间
1524
*

Diff for: src/utils/vscodePath.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
22

3+
import { utils } from './index';
34
import { vsc } from './vsc';
45

56
// 基础目录
@@ -17,18 +18,25 @@ const cssPath = (() => {
1718
// https://github.com/microsoft/vscode/pull/141263
1819
const webPath = getCssPath('workbench.web.main.css');
1920

20-
// See https://code.visualstudio.com/api/references/vscode-api#env
21-
switch (vsc?.env.appHost) {
22-
case 'desktop':
23-
return defPath;
24-
case 'web':
25-
default:
26-
return webPath;
21+
if (utils.isDesktop) {
22+
return defPath;
2723
}
24+
return webPath;
2825
})();
2926

30-
// /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js
31-
const jsPath = path.join(base, 'vs/workbench/workbench.desktop.main.js');
27+
const jsPath = (() => {
28+
// See https://code.visualstudio.com/api/references/vscode-api#env
29+
30+
// desktop
31+
// /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.desktop.main.js
32+
if (utils.isDesktop) {
33+
return path.join(base, 'vs/workbench/workbench.desktop.main.js');
34+
}
35+
36+
// code-server
37+
// /usr/lib/code-server/lib/vscode/out/vs/code/browser/workbench/workbench.js
38+
return path.join(base, 'vs/code/browser/workbench/workbench.js');
39+
})();
3240

3341
export const vscodePath = {
3442
/**

0 commit comments

Comments
 (0)