From bf33f97c9eaf7822a193c3bc385ccb869ea81f30 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Mon, 9 Aug 2021 13:49:37 +0800 Subject: [PATCH] code re-use and eslint --- extra/healthcheck.js | 28 ++++++++++++++-------------- extra/mark-as-nightly.js | 26 +++++--------------------- extra/update-version.js | 24 ++++-------------------- src/util.js | 13 ++++++++++++- src/util.ts | 23 +++++++++++++++++++++++ tsconfig.json | 5 ++++- 6 files changed, 62 insertions(+), 57 deletions(-) diff --git a/extra/healthcheck.js b/extra/healthcheck.js index b547fbcb..c0b33b6e 100644 --- a/extra/healthcheck.js +++ b/extra/healthcheck.js @@ -1,19 +1,19 @@ -var http = require("http"); -var options = { - host: "localhost", - port: "3001", - timeout: 2000, +let http = require("http"); +let options = { + host: "localhost", + port: "3001", + timeout: 2000, }; -var request = http.request(options, (res) => { - console.log(`STATUS: ${res.statusCode}`); - if (res.statusCode == 200) { - process.exit(0); - } else { - process.exit(1); - } +let request = http.request(options, (res) => { + console.log(`STATUS: ${res.statusCode}`); + if (res.statusCode == 200) { + process.exit(0); + } else { + process.exit(1); + } }); request.on("error", function (err) { - console.log("ERROR"); - process.exit(1); + console.log("ERROR"); + process.exit(1); }); request.end(); diff --git a/extra/mark-as-nightly.js b/extra/mark-as-nightly.js index 28496511..0316596b 100644 --- a/extra/mark-as-nightly.js +++ b/extra/mark-as-nightly.js @@ -1,25 +1,9 @@ -/** - * String.prototype.replaceAll() polyfill - * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/ - * @author Chris Ferdinandi - * @license MIT - */ -if (!String.prototype.replaceAll) { - String.prototype.replaceAll = function(str, newStr){ - - // If a regex pattern - if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') { - return this.replace(str, newStr); - } +const pkg = require("../package.json"); +const fs = require("fs"); +const util = require("../src/util"); - // If a string - return this.replace(new RegExp(str, 'g'), newStr); +util.polyfill(); - }; -} - -const pkg = require('../package.json'); -const fs = require("fs"); const oldVersion = pkg.version const newVersion = oldVersion + "-nightly" @@ -35,6 +19,6 @@ if (newVersion) { // Process README.md if (fs.existsSync("README.md")) { - fs.writeFileSync("README.md", fs.readFileSync("README.md", 'utf8').replaceAll(oldVersion, newVersion)) + fs.writeFileSync("README.md", fs.readFileSync("README.md", "utf8").replaceAll(oldVersion, newVersion)) } } diff --git a/extra/update-version.js b/extra/update-version.js index ab020c38..70d54551 100644 --- a/extra/update-version.js +++ b/extra/update-version.js @@ -1,26 +1,10 @@ -/** - * String.prototype.replaceAll() polyfill - * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/ - * @author Chris Ferdinandi - * @license MIT - */ -if (!String.prototype.replaceAll) { - String.prototype.replaceAll = function(str, newStr) { - - // If a regex pattern - if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { - return this.replace(str, newStr); - } - - // If a string - return this.replace(new RegExp(str, "g"), newStr); - - }; -} - const pkg = require("../package.json"); const fs = require("fs"); const child_process = require("child_process"); +const util = require("../src/util"); + +util.polyfill(); + const oldVersion = pkg.version; const newVersion = process.argv[2]; diff --git a/src/util.js b/src/util.js index 8e80cb61..2b7141ce 100644 --- a/src/util.js +++ b/src/util.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.PENDING = exports.UP = exports.DOWN = exports.appName = void 0; +exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.PENDING = exports.UP = exports.DOWN = exports.appName = void 0; exports.appName = "Uptime Kuma"; exports.DOWN = 0; exports.UP = 1; @@ -33,3 +33,14 @@ function debug(msg) { } } exports.debug = debug; +function polyfill() { + if (!String.prototype.replaceAll) { + String.prototype.replaceAll = function (str, newStr) { + if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { + return this.replace(str, newStr); + } + return this.replace(new RegExp(str, "g"), newStr); + }; + } +} +exports.polyfill = polyfill; diff --git a/src/util.ts b/src/util.ts index 033b98f1..73a960b5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,4 @@ +// @ts-nocheck // Common Util for frontend and backend // Backend uses the compiled file util.js // Frontend uses util.ts @@ -42,3 +43,25 @@ export function debug(msg) { console.log(msg); } } + +export function polyfill() { + /** + * String.prototype.replaceAll() polyfill + * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/ + * @author Chris Ferdinandi + * @license MIT + */ + if (!String.prototype.replaceAll) { + String.prototype.replaceAll = function(str, newStr) { + + // If a regex pattern + if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { + return this.replace(str, newStr); + } + + // If a string + return this.replace(new RegExp(str, "g"), newStr); + + }; + } +} diff --git a/tsconfig.json b/tsconfig.json index d47fb63e..eb23619e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,11 @@ { "compileOnSave": true, "compilerOptions": { - "target": "ES2018", + "target": "es2018", "module": "commonjs", + "lib": [ + "es2020" + ], "removeComments": true, "preserveConstEnums": true, "sourceMap": false,