From 892ee52591a170502d7dac0ee54645a7313dc786 Mon Sep 17 00:00:00 2001 From: SaekiRaku Date: Mon, 13 Jul 2020 12:47:48 +0800 Subject: [PATCH] Release 1.4.0 - Allow to customize the port in case that can't find available port automatically. Fixed [Issue #117](https://github.com/SaekiRaku/vscode-rainbow-fart/issues/117). - Optimize the sound request cycle when the server is down. Fixed [Issue #110](https://github.com/SaekiRaku/vscode-rainbow-fart/issues/110 --- CHANGELOG.md | 10 ++++++ VERSION | 2 +- docs/CHANGELOG.md | 10 ++++++ docs/global.js | 2 +- package.json | 2 +- src/commands.js | 7 +++- src/findAvailablePort.js | 4 +-- src/message/index.js | 26 ++++++++++++++ src/message/messages.json | 24 +++++++++++++ src/page/src/components/player/index.vue | 9 ++++- src/service.js | 46 +++++++++++++++++++++++- src/share.js | 3 +- 12 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 src/message/index.js create mode 100644 src/message/messages.json diff --git a/CHANGELOG.md b/CHANGELOG.md index a4d3407..91d9c7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to the "vscode-rainbow-fart" extension will be documented in Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [1.4.0] - 2020-06-30 + +### Added + +- Allow to customize the port in case that can't find available port automatically. Fixed [Issue #117](https://github.com/SaekiRaku/vscode-rainbow-fart/issues/117). + +### Fixed + +- Optimize the sound request cycle when the server is down. Fixed [Issue #110](https://github.com/SaekiRaku/vscode-rainbow-fart/issues/110) + ## [1.3.0] - 2020-06-29 ### Added diff --git a/VERSION b/VERSION index 589268e..e21e727 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.0 \ No newline at end of file +1.4.0 \ No newline at end of file diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a4d3407..91d9c7d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to the "vscode-rainbow-fart" extension will be documented in Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [1.4.0] - 2020-06-30 + +### Added + +- Allow to customize the port in case that can't find available port automatically. Fixed [Issue #117](https://github.com/SaekiRaku/vscode-rainbow-fart/issues/117). + +### Fixed + +- Optimize the sound request cycle when the server is down. Fixed [Issue #110](https://github.com/SaekiRaku/vscode-rainbow-fart/issues/110) + ## [1.3.0] - 2020-06-29 ### Added diff --git a/docs/global.js b/docs/global.js index 7ba8027..e5f16b2 100644 --- a/docs/global.js +++ b/docs/global.js @@ -1,2 +1,2 @@ -window.VERSION = "1.3.0"; +window.VERSION = "1.4.0"; window.URL_PREFIX = location.pathname === "/" ? "" : location.pathname; \ No newline at end of file diff --git a/package.json b/package.json index 31e19fa..c3bcbd0 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "name": "rainbow-fart", "displayName": "🌈 Rainbow Fart", "description": "This extension will keep giving you compliment while you are coding.", - "version": "1.3.0", + "version": "1.4.0", "engines": { "vscode": "^1.33.0" }, diff --git a/src/commands.js b/src/commands.js index 00eee8e..ba954e2 100644 --- a/src/commands.js +++ b/src/commands.js @@ -1,9 +1,14 @@ const vscode = require('vscode'); const { registerCommand } = vscode.commands; const share = require("./share.js"); +const initService = require("./service.js"); module.exports = function (context) { context.subscriptions.push(registerCommand('rainbow-fart.enable', function () { - share.showTip && share.showTip(); + if (share.showTip) { + share.showTip(); + } else { + initService(); + } })); } diff --git a/src/findAvailablePort.js b/src/findAvailablePort.js index cf92fc0..b123cd8 100644 --- a/src/findAvailablePort.js +++ b/src/findAvailablePort.js @@ -30,8 +30,8 @@ async function checkAvailable(port) { return true; } -module.exports = async function(port) { - while (true) { +module.exports = async function (port, retry = Infinity) { + for (let i = 0; i < retry; i++){ let available = await checkAvailable(port); if (!available) { port += Math.ceil(Math.random() * 7); diff --git a/src/message/index.js b/src/message/index.js new file mode 100644 index 0000000..8b3011f --- /dev/null +++ b/src/message/index.js @@ -0,0 +1,26 @@ +const messages = require("./messages.json"); + +let locale; +const currentLanguage = JSON.parse(process.env.VSCODE_NLS_CONFIG).locale; +const supportLanguage = ["en", "zh"] +if (supportLanguage.indexOf(currentLanguage) != -1) { + locale = currentLanguage +} else { + for (let lang of supportLanguage) { + if (currentLanguage.indexOf(lang) == 0) { + locale = lang; + break; + } + } +} + +module.exports = function (str) { + let result = messages[locale] || messages["en"]; + let props = str.split("."); + props.forEach(prop => { result = result[prop] }); + if (!result && locale != "en") { + result = messages["en"]; + props.forEach(prop => { result = result[prop] }); + } + return result; +} \ No newline at end of file diff --git a/src/message/messages.json b/src/message/messages.json new file mode 100644 index 0000000..0e09253 --- /dev/null +++ b/src/message/messages.json @@ -0,0 +1,24 @@ +{ + "en": { + "service": { + "require-port": { + "title": "Can't find available port after retry 3 times, please provide one.", + "placeholder": "E.g. 7777, 6666, 5555 ...", + "available": "✅ Current port is available, please press Enter to confirm.", + "unavailable": "❌ Current port is unavailable, please change." + }, + "failed": "Can't enable Rainbow Fart, please retry." + } + }, + "zh": { + "service": { + "require-port": { + "title": "经过 3 次尝试后仍无法自动找到可用端口,请手动提供一个。", + "placeholder": "例如 7777, 6666, 5555 ...", + "available": "✅ 当前端口可用,请按回车确认。", + "unavailable": "❌ 当前端口不可用,请更换。" + }, + "failed": "无法启用彩虹屁,请重试。" + } + } +} \ No newline at end of file diff --git a/src/page/src/components/player/index.vue b/src/page/src/components/player/index.vue index 8bb752f..370da82 100644 --- a/src/page/src/components/player/index.vue +++ b/src/page/src/components/player/index.vue @@ -61,6 +61,7 @@ export default { data(){ return { authorized: false, + failedTimes: 0 } }, mounted(){ @@ -87,9 +88,15 @@ export default { try { response = await axios.get("/playsound"); }catch(e){ - this.requestPlaySound(); + if(this.failedTimes > 5){ + setTimeout(this.requestPlaySound, 1000); + }else{ + this.failedTimes++; + this.requestPlaySound(); + } return; } + this.failedTime = 0; let voice = response.data; if(voice.lastIndexOf(".") < voice.length - 5){ return; diff --git a/src/service.js b/src/service.js index 343d794..d9c7559 100644 --- a/src/service.js +++ b/src/service.js @@ -7,6 +7,7 @@ const multer = require('multer') const findAvailablePort = require("./findAvailablePort.js"); const open = require("open"); const _ = require("lodash"); +const message = require("./message"); const assets = require("./assets.js"); const share = require("./share.js"); @@ -24,9 +25,52 @@ const settings = require("./settings.js"); // } // }; +function requireCustomPort() { + let checkTimeoutID, currentInput; + + let inputbox = vscode.window.createInputBox(); + inputbox.ignoreFocusOut = true; + inputbox.title = message("service.require-port.title"); + inputbox.placeholder = message("service.require-port.placeholder"); + inputbox.show() + + inputbox.onDidChangeValue((value) => { + currentInput = value; + if (checkTimeoutID) { + clearTimeout(checkTimeoutID); + } + checkTimeoutID = setTimeout(async () => { + let isAvailable = await findAvailablePort(value, 1); + if (isAvailable !== undefined) { + inputbox.validationMessage = message("service.require-port.available"); + } else { + inputbox.validationMessage = message("service.require-port.unavailable"); + } + }, 500); + }) + + return new Promise((resolve, reject) => { + inputbox.onDidAccept(async (value) => { + resolve(currentInput); + inputbox.hide(); + inputbox.dispose(); + }); + }) + +} + module.exports = async function () { - let port = await findAvailablePort(7777); + var port = await findAvailablePort(7777, 3) + + if (!port) { + port = await requireCustomPort(); + let isAvailable = await findAvailablePort(port, 1); + if (!isAvailable) { + vscode.window.showInformationMessage(message("service.failed")); + return; + } + } const app = express(); diff --git a/src/share.js b/src/share.js index 04a3fcb..1ab9caa 100644 --- a/src/share.js +++ b/src/share.js @@ -6,11 +6,12 @@ const os = require("os"); module.exports = { play(name) { - if (!name) { + if (!name || !this.playVoiceRes) { return; } console.log("Playing voice - " + name); this.playVoiceRes && this.playVoiceRes.send(name); + this.playVoiceRes = null; }, uri(thepath) { if (os.type() == "Windows_NT") {