Skip to content

Commit

Permalink
调整初次加载校验方式
Browse files Browse the repository at this point in the history
  • Loading branch information
shalldie committed Jul 18, 2022
1 parent 7b1a5cd commit 7119d90
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
.vscode-test/
*.vsix
.DS_Store
.husky
.husky
*.touch
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ package-lock.json
.huskyrc
.prettierignore
.github
*.touch
3 changes: 0 additions & 3 deletions assets/config.json

This file was deleted.

57 changes: 30 additions & 27 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// sys
import { tmpdir } from 'os';
import { constants as fsConstants } from 'fs';
import fs, { constants as fsConstants } from 'fs';
import fsp from 'fs/promises';
import path from 'path';
import { randomUUID } from 'crypto';
Expand All @@ -14,7 +14,7 @@ import { vsHelp } from './vsHelp';
import { vscodePath } from './vscodePath';
import { getCss } from './getCss';
import { defBase64 } from './defBase64';
import { version, BACKGROUND_VER, ENCODE } from './constants';
import { VERSION, BACKGROUND_VER, ENCODE } from './constants';

/**
* css文件修改状态类型
Expand Down Expand Up @@ -54,6 +54,13 @@ class Background implements Disposable {
*/
private config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('background');

/**
* 需要释放的资源
*
* @private
* @type {Disposable[]}
* @memberof Background
*/
private disposables: Disposable[] = [];

//#endregion
Expand All @@ -74,7 +81,7 @@ class Background implements Disposable {
const cssContent = await this.getCssContent();

// hack 过的旧版本,即不包含当前版本
const ifVerOld = !~cssContent.indexOf(`/*${BACKGROUND_VER}.${version}*/`);
const ifVerOld = !~cssContent.indexOf(`/*${BACKGROUND_VER}.${VERSION}*/`);

if (ifVerOld) {
return ECSSEditType.isOld;
Expand Down Expand Up @@ -136,26 +143,18 @@ class Background implements Disposable {
*
* @private
* @param {string} cmd 命令
* @param {{ name?: string; icns?: string; env?: { [key: string]: string } }} [options] 选项
* @return {*} {Promise<[stdout?} 命令输出
* @param {{ name?: string }} [options={}] 选项
* @return {*} {Promise<any>} 命令输出
* @memberof Background
*/
private async sudoCommand(
cmd: string,
options?: { name?: string; icns?: string; env?: { [key: string]: string } }
): Promise<[stdout?: string | Buffer, stderr?: string | Buffer]> {
private async sudoCommand(cmd: string, options: { name?: string } = {}): Promise<any> {
return new Promise((resolve, reject) => {
const callback = (error: Error, stdout: string | Buffer, stderr: string | Buffer) => {
sudo.exec(cmd, options, (error: Error, stdout: string | Buffer, stderr: string | Buffer) => {
if (error) {
reject(error);
}
resolve([stdout, stderr]);
};
if (!options) {
sudo.exec(cmd, callback);
return;
}
sudo.exec(cmd, options, callback);
});
});
}

Expand All @@ -167,8 +166,8 @@ class Background implements Disposable {
* @returns 临时文件路径
* @memberof Background
*/
private async saveCssContentToTemp(content: string): Promise<string> {
const tempPath = path.resolve(tmpdir(), `vscode-background-${randomUUID()}.css`);
private async saveCssContentToTemp(content: string) {
const tempPath = path.join(tmpdir(), `vscode-background-${randomUUID()}.css`);
await fsp.writeFile(tempPath, content, ENCODE);
return tempPath;
}
Expand All @@ -181,22 +180,22 @@ class Background implements Disposable {
* @memberof Background
*/
private async checkFirstload(): Promise<boolean> {
const configPath = path.join(__dirname, '../assets/config.json');
const info: { firstload: boolean } = JSON.parse(await fsp.readFile(configPath, ENCODE));
const versionTouchFile = path.join(__dirname, `../vscb.${VERSION}.touch`);

if (info.firstload) {
const firstLoad = !fs.existsSync(versionTouchFile);

if (firstLoad) {
// 提示

vsHelp.showInfo(
[
//
`Welcome to use background@${version}!`,
`Welcome to use background@${VERSION}!`,
'You can config it in settings.json.'
].join('\n')
);
// 标识插件已启动过
info.firstload = false;
fsp.writeFile(configPath, JSON.stringify(info, null, ' '), ENCODE);
fsp.writeFile(versionTouchFile, '', ENCODE);

return true;
}
Expand Down Expand Up @@ -282,7 +281,7 @@ class Background implements Disposable {
/**
* 初始化
*
* @private
* @return {*} {Promise<void>}
* @memberof Background
*/
public async setup(): Promise<void> {
Expand Down Expand Up @@ -317,9 +316,8 @@ class Background implements Disposable {
/**
* 卸载
*
* @returns {boolean}
* @return {*} {Promise<boolean>} 是否成功卸载
* @memberof Background
* @returns 是否成功卸载
*/
public async uninstall(): Promise<boolean> {
try {
Expand All @@ -332,6 +330,11 @@ class Background implements Disposable {
}
}

/**
* 释放资源
*
* @memberof Background
*/
public dispose() {
this.disposables.forEach(n => n.dispose());
}
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pkg from '../package.json';

/** 版本号 */
export const version = pkg.version;
export const VERSION: string = pkg.version;

/** 版本标识 */
export const BACKGROUND_VER = 'background.ver';
Expand Down
4 changes: 2 additions & 2 deletions src/getCss.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fsp from 'fs/promises';
import path from 'path';
import { URL } from 'url';
import { version, BACKGROUND_VER } from './constants';
import { VERSION, BACKGROUND_VER } from './constants';

/**
* 通过配置获取样式文本
Expand Down Expand Up @@ -101,7 +101,7 @@ export function getCss(

const content = `
/*css-background-start*/
/*${BACKGROUND_VER}.${version}*/
/*${BACKGROUND_VER}.${VERSION}*/
${imageStyleContent}
[id="workbench.parts.editor"] .split-view-view .editor-container .editor-instance>.monaco-editor .overflow-guard>.monaco-scrollable-element>.monaco-editor-background{background: none;}
/*css-background-end*/
Expand Down

0 comments on commit 7119d90

Please sign in to comment.