diff --git a/l10n/bundle.l10n.ja.json b/l10n/bundle.l10n.ja.json index 0051ab2..1f373b6 100644 --- a/l10n/bundle.l10n.ja.json +++ b/l10n/bundle.l10n.ja.json @@ -1,11 +1,10 @@ { "Show `background` commands": "「`background`」コマンドの表示", "Background extension has been uninstalled. See you next time!": "Backgroundはアンインストールされています。また今度!", - "Restart vscode": "vscodeの再起動", - "Welcome to use background@{version}!": "ようこそbackground@{version}!", + "Reload vscode": "vscode を再読み込み", + "Background@{version} is ready! Apply to take effect.": "Background@{version} の準備ができました!「適用」をクリックして反映してください。", "More": "詳細", - "Background has been disabled! Please restart.": "Backgroundは無効になっています!再起動してください。", - "Configuration has been changed, click to update.": "構成が変更されたので、更新をクリックします。", - "Update and restart": "更新と再起動", - "Background has been changed! Please restart.": "Backgroundは変わった!再起動してください。" + "Background has been disabled! Please reload.": "Backgroundは無効になっています!再起動してください。", + "Configuration has been changed, click to apply.": "構成が変更されたので、更新をクリックします。", + "Apply and Reload": "適用して再読み込み" } diff --git a/l10n/bundle.l10n.json b/l10n/bundle.l10n.json index 505f996..4e4b854 100644 --- a/l10n/bundle.l10n.json +++ b/l10n/bundle.l10n.json @@ -1,11 +1,10 @@ { "Show `background` commands": "Show `background` commands", "Background extension has been uninstalled. See you next time!": "Background extension has been uninstalled. See you next time!", - "Restart vscode": "Restart vscode", - "Welcome to use background@{version}!": "Welcome to use background@{version}!", + "Reload vscode": "Reload vscode", + "Background@{version} is ready! Apply to take effect.": "Background@{version} is ready! Apply to take effect.", "More": "More", - "Background has been disabled! Please restart.": "Background has been disabled! Please restart.", - "Configuration has been changed, click to update.": "Configuration has been changed, click to update.", - "Update and restart": "Update and restart", - "Background has been changed! Please restart.": "Background has been changed! Please restart." + "Background has been disabled! Please reload.": "Background has been disabled! Please reload.", + "Configuration has been changed, click to apply.": "Configuration has been changed, click to apply.", + "Apply and Reload": "Apply and Reload" } diff --git a/l10n/bundle.l10n.zh-cn.json b/l10n/bundle.l10n.zh-cn.json index a94e683..a454547 100644 --- a/l10n/bundle.l10n.zh-cn.json +++ b/l10n/bundle.l10n.zh-cn.json @@ -1,11 +1,10 @@ { "Show `background` commands": "查看 `background` 命令", "Background extension has been uninstalled. See you next time!": "Background 已经卸载,下次见!", - "Restart vscode": "重启 vscode", - "Welcome to use background@{version}!": "欢迎使用 background@{version}!", + "Reload vscode": "重载 vscode", + "Background@{version} is ready! Apply to take effect.": "Background@{version} 已就绪!点击应用生效。", "More": "更多", - "Background has been disabled! Please restart.": "Background 已经禁用! 请重启。", - "Configuration has been changed, click to update.": "配置已改变,点击更新。", - "Update and restart": "更新并重启", - "Background has been changed! Please restart.": "Background 已经改变!请重启。" + "Background has been disabled! Please reload.": "Background 已经禁用! 请重载。", + "Configuration has been changed, click to apply.": "配置已改变,点击应用。", + "Apply and Reload": "应用并重载" } diff --git a/package-lock.json b/package-lock.json index 8ebdd7f..2781317 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,23 +1,21 @@ { "name": "background", - "version": "2.0.5", + "version": "2.0.6-alpha.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "background", - "version": "2.0.5", + "version": "2.0.6-alpha.0", "license": "MIT", "dependencies": { "@vscode/sudo-prompt": "^9.3.1", "fast-glob": "^3.3.3", - "lockfile": "^1.0.4", "stylis": "^4.3.6", "uglify-js": "^3.19.3" }, "devDependencies": { "@ianvs/prettier-plugin-sort-imports": "^4.7.0", - "@types/lockfile": "^1.0.4", "@types/node": "^24", "@types/stylis": "^4.2.7", "@types/uglify-js": "^3.17.5", @@ -1167,13 +1165,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@types/lockfile": { - "version": "1.0.4", - "resolved": "https://registry.npmmirror.com/@types/lockfile/-/lockfile-1.0.4.tgz", - "integrity": "sha512-Q8oFIHJHr+htLrTXN2FuZfg+WXVHQRwU/hC2GpUu+Q8e3FUM9EDkS2pE3R2AO1ZGu56f479ybdMCNF1DAu8cAQ==", - "dev": true, - "license": "MIT" - }, "node_modules/@types/node": { "version": "24.10.4", "resolved": "https://registry.npmmirror.com/@types/node/-/node-24.10.4.tgz", @@ -3708,21 +3699,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/lockfile": { - "version": "1.0.4", - "resolved": "https://registry.npmmirror.com/lockfile/-/lockfile-1.0.4.tgz", - "integrity": "sha512-cvbTwETRfsFh4nHsL1eGWapU1XFi5Ot9E85sWAwia7Y7EgB7vfqcZhTKZ+l7hCGxSPoushMv5GKhT5PdLv03WA==", - "license": "ISC", - "dependencies": { - "signal-exit": "^3.0.2" - } - }, - "node_modules/lockfile/node_modules/signal-exit": { - "version": "3.0.7", - "resolved": "https://registry.npmmirror.com/signal-exit/-/signal-exit-3.0.7.tgz", - "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", - "license": "ISC" - }, "node_modules/lodash": { "version": "4.17.21", "resolved": "https://registry.npmmirror.com/lodash/-/lodash-4.17.21.tgz", diff --git a/package.json b/package.json index facd8fd..3d2b8c1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "background", "displayName": "background", "description": "Bring background images to your vscode", - "version": "2.0.5", + "version": "2.0.6-alpha.0", "scripts": { "vscode:prepublish": "npm run compile", "vscode:uninstall": "node ./out/uninstall", @@ -316,7 +316,6 @@ }, "devDependencies": { "@ianvs/prettier-plugin-sort-imports": "^4.7.0", - "@types/lockfile": "^1.0.4", "@types/node": "^24", "@types/stylis": "^4.2.7", "@types/uglify-js": "^3.17.5", @@ -334,7 +333,6 @@ "dependencies": { "@vscode/sudo-prompt": "^9.3.1", "fast-glob": "^3.3.3", - "lockfile": "^1.0.4", "stylis": "^4.3.6", "uglify-js": "^3.19.3" } diff --git a/package.nls.ja.json b/package.nls.ja.json index 088dc30..82f8f74 100644 --- a/package.nls.ja.json +++ b/package.nls.ja.json @@ -1,7 +1,7 @@ { "extension.background.command.info.title": "backgroundへようこそ。", - "extension.background.command.install.title": "「background」をインストールして有効にします。", - "extension.background.command.disable.title": "「background」を無効にする。", + "extension.background.command.install.title": "有効にして適用「Background」 を有効にして適用", + "extension.background.command.disable.title": "「Background」を無効にする。", "extension.background.command.uninstall.title": "拡張機能をアンインストール", "extension.background.enabled.description": "拡張機能の有効化を制御します", diff --git a/package.nls.json b/package.nls.json index d6dd9ba..a217747 100644 --- a/package.nls.json +++ b/package.nls.json @@ -1,6 +1,6 @@ { "extension.background.command.info.title": "Welcome to background.", - "extension.background.command.install.title": "Install and enable the background.", + "extension.background.command.install.title": "Enable and apply the background.", "extension.background.command.disable.title": "Disable the background.", "extension.background.command.uninstall.title": "Uninstall the extension.", diff --git a/package.nls.zh.json b/package.nls.zh.json index d2f5b30..68f6d0e 100644 --- a/package.nls.zh.json +++ b/package.nls.zh.json @@ -1,6 +1,6 @@ { "extension.background.command.info.title": "欢迎使用 background 插件", - "extension.background.command.install.title": "安装/激活插件", + "extension.background.command.install.title": "激活/应用插件", "extension.background.command.disable.title": "禁用插件", "extension.background.command.uninstall.title": "卸载插件", diff --git a/src/background/Background.ts b/src/background/Background.ts index 946d008..71da9e5 100644 --- a/src/background/Background.ts +++ b/src/background/Background.ts @@ -61,7 +61,7 @@ export class Background implements Disposable { // #region private methods 私有方法 /** - * 检测是否初次加载,并在初次加载的时候提示用户 + * 检测是否初次加载 * * @private * @returns {boolean} 是否初次加载 @@ -71,22 +71,6 @@ export class Background implements Disposable { const firstLoad = !fs.existsSync(TOUCH_JSFILE_PATH); if (firstLoad) { - // 提示 - vscode.window - .showInformationMessage(l10n.t('Welcome to use background@{version}!', { version: VERSION }), { - title: l10n.t('More') - }) - .then(confirm => { - if (!confirm) { - return; - } - this.showWelcome(); - }); - - // 新版本强制提示下吧 - // if (VERSION === '2.0.0' || true) { - // this.showWelcome(); - // } // 标识插件已启动过 await fs.promises.writeFile(TOUCH_JSFILE_PATH, vscodePath.jsPath, ENCODING); return true; @@ -155,25 +139,20 @@ export class Background implements Disposable { if (!enabled) { if (hasInstalled) { await this.uninstall(); - vsHelp.showInfoRestart(l10n.t('Background has been disabled! Please restart.')); - } - return; - } - // 更新,需要二次确认 - const confirm = await vscode.window.showInformationMessage( - l10n.t('Configuration has been changed, click to update.'), - { - title: l10n.t('Update and restart') + vsHelp.reload({ + message: l10n.t('Background has been disabled! Please reload.') + }); } - ); - - if (!confirm) { return; } - await this.applyPatch(); - vscode.commands.executeCommand('workbench.action.reloadWindow'); + // 更新,需要二次确认 + vsHelp.reload({ + message: l10n.t('Configuration has been changed, click to apply.'), + btnReload: l10n.t('Apply and Reload'), + beforeReload: () => this.applyPatch() + }); } public async applyPatch() { @@ -193,25 +172,48 @@ export class Background implements Disposable { /** * 初始化 * - * @return {*} {Promise} + * @return {*} {Promise} * @memberof Background */ public async setup(): Promise { - await this.removeLegacyCssPatch(); // 移除v1旧版本patch + await this.removeLegacyCssPatch(); // 移除「v1旧版本」patch await this.checkFirstload(); // 是否初次加载插件 - const patchType = await this.jsFile.getPatchType(); // css 文件目前状态 + const patchType = await this.jsFile.getPatchType(); // 「js文件」目前状态 - // 如果「开启」状态,文件不是「latest」,则进行更新 - if (this.config.enabled) { - // 此时一般为 vscode更新、background更新 - if ([EFilePatchType.Legacy, EFilePatchType.None].includes(patchType)) { - if (await this.applyPatch()) { - vsHelp.showInfoRestart(l10n.t('Background has been changed! Please restart.')); - } - } + // 如果「开启」状态,文件不是「latest」,则进行「提示更新」 + // 此时一般为 「background更新」、「vscode更新」 + const needApply = [EFilePatchType.Legacy, EFilePatchType.None].includes(patchType); + if (this.config.enabled && needApply) { + // 提示 + vscode.window + .showInformationMessage( + l10n.t('Background@{version} is ready! Apply to take effect.', { version: VERSION }), + { + title: l10n.t('Apply and Reload'), + action: async () => { + await this.applyPatch(); + await vsHelp.reload(); + } + }, + { + title: l10n.t('More'), + action: () => this.showWelcome() + } + ) + .then(confirm => { + confirm?.action(); + }); } + // if ([EFilePatchType.Legacy, EFilePatchType.None].includes(patchType)) { + // // 提示: 欢迎使用 background@version! 「应用并重载」、「更多」 + // if (await this.applyPatch()) { + // vsHelp.reload({ + // message: l10n.t('Background has been changed! Please reload.') + // }); + // } + // } // 监听文件改变 this.disposables.push( diff --git a/src/background/CssFile.ts b/src/background/CssFile.ts index 7f04138..8c874ef 100644 --- a/src/background/CssFile.ts +++ b/src/background/CssFile.ts @@ -177,7 +177,6 @@ export class CssFile { */ public async uninstall(): Promise { try { - await _.lock(); let content = await this.getContent(); content = this.clearContent(content); // 异常case return @@ -188,8 +187,6 @@ export class CssFile { } catch (ex) { console.log(ex); return false; - } finally { - await _.unlock(); } } } diff --git a/src/background/PatchFile/PatchFile.base.ts b/src/background/PatchFile/PatchFile.base.ts index bf1cb60..b063cfe 100644 --- a/src/background/PatchFile/PatchFile.base.ts +++ b/src/background/PatchFile/PatchFile.base.ts @@ -146,14 +146,11 @@ export abstract class AbsPatchFile { public async restore() { try { - await _.lock(); let content = await this.getContent(); content = this.cleanPatches(content); return await this.write(content); } catch { return false; - } finally { - await _.unlock(); } } } diff --git a/src/background/PatchFile/PatchFile.javascript.ts b/src/background/PatchFile/PatchFile.javascript.ts index c6cb7f9..bf8b3c5 100644 --- a/src/background/PatchFile/PatchFile.javascript.ts +++ b/src/background/PatchFile/PatchFile.javascript.ts @@ -1,4 +1,3 @@ -import { _ } from '../../utils'; import { BACKGROUND_VER, VERSION } from '../../utils/constants'; import { AbsPatchFile } from './PatchFile.base'; @@ -15,7 +14,6 @@ import { AbsPatchFile } from './PatchFile.base'; export class JsPatchFile extends AbsPatchFile { public async applyPatches(patchContent: string): Promise { try { - await _.lock(); const curContent = await this.getContent(); let content = this.cleanPatches(curContent); content += [ @@ -33,8 +31,6 @@ export class JsPatchFile extends AbsPatchFile { return await this.write(content); } catch { return false; - } finally { - await _.unlock(); } } diff --git a/src/extension.ts b/src/extension.ts index 8e7a368..9748638 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -40,7 +40,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand('extension.background.install', async () => { await background.config.update('enabled', true, true); await background.applyPatch(); - await vscode.commands.executeCommand('workbench.action.reloadWindow'); + await vsHelp.reload(); }) ); @@ -48,18 +48,17 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand('extension.background.disable', async () => { await background.config.update('enabled', false, true); await background.uninstall(); - await vscode.commands.executeCommand('workbench.action.reloadWindow'); + await vsHelp.reload(); }) ); context.subscriptions.push( vscode.commands.registerCommand('extension.background.uninstall', async () => { - if (await background.uninstall()) { - // 当且仅当成功删除样式时才会卸载扩展 - // 否则可能导致没有成功删掉样式时扩展就被卸载掉 - await vscode.commands.executeCommand('workbench.extensions.uninstallExtension', EXTENSION_ID); - await vsHelp.showInfoRestart(l10n.t('Background extension has been uninstalled. See you next time!')); - } + await background.uninstall(); + await vscode.commands.executeCommand('workbench.extensions.uninstallExtension', EXTENSION_ID); + vsHelp.reload({ + message: l10n.t('Background extension has been uninstalled. See you next time!') + }); }) ); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 5961ba2..1cf30c5 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -20,8 +20,5 @@ export const EXTENSION_NAME: string = pkg.name; /** 扩展ID */ export const EXTENSION_ID = `${PUBLISHER}.${EXTENSION_NAME}`; -/** 文件锁路径 */ -export const LOCK_PATH = path.join(__dirname, '../../', `${EXTENSION_ID}.lock`); - /** 版本临时文件,存放js路径、标识初次安装 */ export const TOUCH_JSFILE_PATH = path.join(__dirname, `../../vscb.${VERSION}.js.touch`); diff --git a/src/utils/index.ts b/src/utils/index.ts index d923b66..84cee2e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,15 +1,8 @@ import sudo from '@vscode/sudo-prompt'; -import lockfile from 'lockfile'; -import { LOCK_PATH } from './constants'; import { vsc } from './vsc'; export namespace _ { - /** - * if zh-CN - */ - export const isZHCN = /^zh/.test(vsc?.env.language || ''); - /** * if desktop * @@ -19,61 +12,6 @@ export namespace _ { */ export const isDesktop = vsc?.env.appHost === 'desktop'; - /** - * 等待若干时间 - * - * @export - * @param {number} [delay=0] - * @return {*} - */ - export function sleep(delay = 0) { - return new Promise(resolve => { - setTimeout(resolve, delay); - }); - } - - /** - * 添加文件锁 - * - * @export - * @return {*} - */ - export function lock() { - return new Promise((resolve, reject) => { - lockfile.lock( - LOCK_PATH, - { - // When multiple VSCode instances are running, all instances' commands need to be executed within the `wait` time - // 在打开了多个vscode实例时,需要所有实例的命令在`wait`时间内执行完毕 - wait: 1000 * 30 - }, - err => { - if (err) { - return reject(err); - } - resolve(); - } - ); - }); - } - - /** - * 取消文件锁 - * - * @export - * @return {*} - */ - export function unlock() { - return new Promise((resolve, reject) => { - lockfile.unlock(LOCK_PATH, err => { - if (err) { - return reject(err); - } - resolve(); - }); - }); - } - /** * 提权运行 * diff --git a/src/utils/vsHelp.ts b/src/utils/vsHelp.ts index 88fdb46..ee4088b 100644 --- a/src/utils/vsHelp.ts +++ b/src/utils/vsHelp.ts @@ -1,16 +1,37 @@ import vscode, { l10n } from 'vscode'; +class ReloadOptions { + /** reload 提示内容 */ + message = ''; + /** reload 按钮文案 */ + btnReload = l10n.t('Reload vscode'); + /** reload 前置动作 */ + beforeReload?: () => any; +} + export const vsHelp = { /** - * 提示信息并重启 + * 重新加载 vscode * - * @param {string} content 提示内容 - * @returns {Thenable} + * @param {Partial} [options={}] 定义重新加载的相关配置 + * @return {*} {Promise} */ - showInfoRestart(content: string): Thenable { - return vscode.window.showInformationMessage(content, { title: l10n.t('Restart vscode') }).then(function (item) { - if (!item) return; - vscode.commands.executeCommand('workbench.action.reloadWindow'); - }); + async reload(options: Partial = {}): Promise { + // 填充默认值 + options = { + ...new ReloadOptions(), + ...options + }; + // 如果需要确认 + if (options.message) { + const goon = await vscode.window.showInformationMessage(options.message, { title: options.btnReload! }); + // 关闭了 提示框 + if (!goon) { + return Promise.resolve(); + } + // 关闭前置动作 + await options.beforeReload?.(); + } + return vscode.commands.executeCommand('workbench.action.reloadWindow'); } };