Skip to content

Commit

Permalink
抽象 patch file
Browse files Browse the repository at this point in the history
  • Loading branch information
shalldie committed Oct 17, 2024
1 parent 9727e92 commit eeb7f8d
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 242 deletions.
32 changes: 18 additions & 14 deletions src/background/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { utils } from '../utils';
import { vscodePath } from '../utils/vscodePath';
import { vsHelp } from '../utils/vsHelp';
import { CssFile } from './CssFile';
import { EJsEditType, JsFile } from './JsFile';
import { EFilePatchType, JsPatchFile } from './PatchFile';
import { PatchGenerator, TPatchGeneratorConfig } from './PatchGenerator';

/**
Expand All @@ -27,13 +27,13 @@ export class Background implements Disposable {
// #region fields 字段

/**
* css文件操作对象
* 老版本css文件操作对象
*
* @memberof Background
*/
public cssFile = new CssFile(vscodePath.cssPath); // 没必要继承,组合就行

public jsFile = new JsFile(vscodePath.jsPath);
public jsFile = new JsPatchFile(vscodePath.jsPath);

/**
* 当前用户配置
Expand Down Expand Up @@ -131,13 +131,13 @@ export class Background implements Disposable {
* @memberof Background
*/
private async onConfigChange() {
const hasInstalled = await this.jsFile.hasInstalled();
const hasInstalled = await this.hasInstalled();
const enabled = this.config.enabled;

// 禁用
if (!enabled) {
if (hasInstalled) {
await this.jsFile.uninstall();
await this.uninstall();
vsHelp.showInfoRestart('Background has been disabled! Please restart.');
}
return;
Expand All @@ -163,10 +163,7 @@ export class Background implements Disposable {
}

const scriptContent = PatchGenerator.create(this.config);

await utils.lock();
await this.jsFile.applyPatch(scriptContent);
await utils.unlock();
await this.jsFile.applyPatches(scriptContent);
}

// #endregion
Expand All @@ -179,16 +176,23 @@ export class Background implements Disposable {
* @return {*} {Promise<void>}
* @memberof Background
*/
public async setup(): Promise<void> {
public async setup(): Promise<any> {
await this.removeLegacyCssPatch(); // 移除旧版本patch
await this.jsFile.setup(); // backup

if (!this.jsFile.hasBackup) {
vscode.window.showErrorMessage('Backup files failed to save.');
return false;
}

await this.checkFirstload(); // 是否初次加载插件

const editType = await this.jsFile.getEditType(); // css 文件目前状态
const patchType = await this.jsFile.getPatchType(); // css 文件目前状态

// 如果「开启」状态,文件不是「latest」,则进行更新
if (this.config.enabled) {
// 此时一般为 vscode更新、background更新
if ([EJsEditType.IsOld, EJsEditType.NoModified].includes(editType)) {
if ([EFilePatchType.Legacy, EFilePatchType.None].includes(patchType)) {
await this.applyPatch();
vsHelp.showInfoRestart('Background has been changed! Please restart.');
}
Expand Down Expand Up @@ -218,7 +222,7 @@ export class Background implements Disposable {
* @memberof Background
*/
public hasInstalled(): Promise<boolean> {
return this.jsFile.hasInstalled();
return this.jsFile.hasPatched();
}

/**
Expand All @@ -229,7 +233,7 @@ export class Background implements Disposable {
*/
public async uninstall(): Promise<boolean> {
await this.removeLegacyCssPatch();
return this.jsFile.uninstall();
return this.jsFile.restore();
}

/**
Expand Down
212 changes: 0 additions & 212 deletions src/background/JsFile.ts

This file was deleted.

Loading

0 comments on commit eeb7f8d

Please sign in to comment.