From 4b98890b5341e48ecede1c1e5907a2856c85da55 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Mon, 17 Jul 2023 17:56:49 +0700 Subject: [PATCH 1/4] feat: add color to the output using Chalk --- lib/google-rank.js | 19 +++++++++++++++++-- lib/google-rank.js.map | 2 +- package-lock.json | 9 ++------- package.json | 1 + src/google-rank.ts | 15 +++++++++++++-- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/google-rank.js b/lib/google-rank.js index 46ee7b9..372bac9 100755 --- a/lib/google-rank.js +++ b/lib/google-rank.js @@ -1,6 +1,10 @@ #!/usr/bin/env node "use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); +const chalk_1 = __importDefault(require("chalk")); const commander_1 = require("commander"); const utils_1 = require("./utils"); async function run() { @@ -18,14 +22,25 @@ async function run() { const prom = (0, utils_1.getWebsiteRank)(website, keyword, { maxPage }); rankByKeywords.push([keyword, prom]); } - process.stdout.write(`Ranks for ${website} website:\n`); + process.stdout.write(`Ranks for ${chalk_1.default.blueBright(website)} website:\n`); for (const [keyword, prom] of rankByKeywords) { const rank = await prom; if (rank === undefined) { process.stdout.write(`page ? rank ?`); } else { - process.stdout.write(`page ${rank.page + 1} rank ${rank.rank + 1}`); + if (rank.page <= 0) { + process.stdout.write(`page ${chalk_1.default.greenBright(rank.page + 1)}`); + } + else { + process.stdout.write(`page ${chalk_1.default.redBright(rank.page + 1)}`); + } + if (rank.page <= 0 && rank.rank <= 2) { + process.stdout.write(` rank ${chalk_1.default.greenBright(rank.rank + 1)}`); + } + else { + process.stdout.write(` rank ${chalk_1.default.redBright(rank.rank + 1)}`); + } } process.stdout.write(` ${keyword}\n`); } diff --git a/lib/google-rank.js.map b/lib/google-rank.js.map index 3d36e40..81f1471 100644 --- a/lib/google-rank.js.map +++ b/lib/google-rank.js.map @@ -1 +1 @@ -{"version":3,"file":"google-rank.js","s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,EAAE;YACtB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;SACxC;aAAM;YACL,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,EAAE;gBAClB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC;aAClE;iBAAM;gBACL,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC;aAChE;YAED,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,EAAE;gBACpC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC;aACpE;iBAAM;gBACL,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC;aAClE;SACF;QACD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,OAAO,IAAI,CAAC,CAAC;KACxC;AACH,CAAC;AAED,GAAG,EAAE,CAAC"} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d51b503..d1d09ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.1", "license": "MIT", "dependencies": { + "chalk": "^4.1.2", "commander": "^11.0.0", "googlethis": "^1.7.1" }, @@ -1836,7 +1837,6 @@ "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dev": true, "dependencies": { "color-convert": "^2.0.1" }, @@ -2296,7 +2296,6 @@ "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "dev": true, "dependencies": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" @@ -2427,7 +2426,6 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, "dependencies": { "color-name": "~1.1.4" }, @@ -2438,8 +2436,7 @@ "node_modules/color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, "node_modules/combined-stream": { "version": "1.0.8", @@ -3912,7 +3909,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true, "engines": { "node": ">=8" } @@ -6780,7 +6776,6 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dev": true, "dependencies": { "has-flag": "^4.0.0" }, diff --git a/package.json b/package.json index 8704176..8f2aee2 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "test": "dev test" }, "dependencies": { + "chalk": "^4.1.2", "commander": "^11.0.0", "googlethis": "^1.7.1" }, diff --git a/src/google-rank.ts b/src/google-rank.ts index 2097059..24f6240 100644 --- a/src/google-rank.ts +++ b/src/google-rank.ts @@ -1,5 +1,6 @@ #!/usr/bin/env node +import chalk from "chalk"; import { program } from "commander"; import { getWebsiteRank, WebsiteRank } from "./utils"; @@ -22,13 +23,23 @@ async function run() { rankByKeywords.push([keyword, prom]); } - process.stdout.write(`Ranks for ${website} website:\n`); + process.stdout.write(`Ranks for ${chalk.blueBright(website)} website:\n`); for (const [keyword, prom] of rankByKeywords) { const rank = await prom; if (rank === undefined) { process.stdout.write(`page ? rank ?`); } else { - process.stdout.write(`page ${rank.page + 1} rank ${rank.rank + 1}`); + if (rank.page <= 0) { + process.stdout.write(`page ${chalk.greenBright(rank.page + 1)}`); + } else { + process.stdout.write(`page ${chalk.redBright(rank.page + 1)}`); + } + + if (rank.page <= 0 && rank.rank <= 2) { + process.stdout.write(` rank ${chalk.greenBright(rank.rank + 1)}`); + } else { + process.stdout.write(` rank ${chalk.redBright(rank.rank + 1)}`); + } } process.stdout.write(` ${keyword}\n`); } From d71f642312da031db0bb1ec8d5f87f042a4b8eba Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Mon, 17 Jul 2023 22:15:45 +0700 Subject: [PATCH 2/4] refactor: move formatting in `google-rank.ts` to `utils/format.ts` --- lib/google-rank.js | 48 ++++++++++++++++++++++------------------ lib/google-rank.js.map | 2 +- lib/utils/format.d.ts | 8 +++++++ lib/utils/format.js | 26 ++++++++++++++++++++++ lib/utils/format.js.map | 1 + lib/utils/index.d.ts | 1 + lib/utils/index.js | 4 +++- lib/utils/index.js.map | 2 +- src/google-rank.ts | 27 ++++++---------------- src/utils/format.test.ts | 31 ++++++++++++++++++++++++++ src/utils/format.ts | 27 ++++++++++++++++++++++ src/utils/index.ts | 1 + 12 files changed, 134 insertions(+), 44 deletions(-) create mode 100644 lib/utils/format.d.ts create mode 100644 lib/utils/format.js create mode 100644 lib/utils/format.js.map create mode 100644 src/utils/format.test.ts create mode 100644 src/utils/format.ts diff --git a/lib/google-rank.js b/lib/google-rank.js index dd460a7..10d3426 100755 --- a/lib/google-rank.js +++ b/lib/google-rank.js @@ -1,12 +1,35 @@ #!/usr/bin/env node "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = __importDefault(require("chalk")); const commander_1 = require("commander"); -const utils_1 = require("./utils"); +const utils = __importStar(require("./utils")); async function run() { commander_1.program .argument("", "website name") @@ -19,30 +42,13 @@ async function run() { const maxPage = Number.parseInt(opts.maxPage); const rankByKeywords = []; for (const keyword of keywords) { - const prom = (0, utils_1.googleGetWebsiteRank)(website, keyword, { maxPage }); + const prom = utils.googleGetWebsiteRank(website, keyword, { maxPage }); rankByKeywords.push([keyword, prom]); } process.stdout.write(`Ranks for ${chalk_1.default.blueBright(website)} website:\n`); for (const [keyword, prom] of rankByKeywords) { - const rank = await prom; - if (rank === undefined) { - process.stdout.write(`page ? rank ?`); - } - else { - if (rank.page <= 0) { - process.stdout.write(`page ${chalk_1.default.greenBright(rank.page + 1)}`); - } - else { - process.stdout.write(`page ${chalk_1.default.redBright(rank.page + 1)}`); - } - if (rank.page <= 0 && rank.rank <= 2) { - process.stdout.write(` rank ${chalk_1.default.greenBright(rank.rank + 1)}`); - } - else { - process.stdout.write(` rank ${chalk_1.default.redBright(rank.rank + 1)}`); - } - } - process.stdout.write(` ${keyword}\n`); + const str = utils.formatKeywordRank(keyword, await prom); + process.stdout.write(`${str}\n`); } } run(); diff --git a/lib/google-rank.js.map b/lib/google-rank.js.map index 7204302..8e77101 100644 --- a/lib/google-rank.js.map +++ b/lib/google-rank.js.map @@ -1 +1 @@ -{"version":3,"file":"google-rank.js","sourceRoot":"","sources":["../src/google-rank.ts"],"names":[],"mappings":";;;;;;AAEA,kDAA0B;AAC1B,yCAAoC;AACpC,mCAAkE;AAElE,KAAK,UAAU,GAAG;IAChB,mBAAO;SACJ,QAAQ,CAAC,WAAW,EAAE,cAAc,CAAC;SACrC,SAAS,CAAC,eAAe,CAAC;SAC1B,MAAM,CAAC,qBAAqB,EAAE,4BAA4B,EAAE,GAAG,CAAC;SAChE,KAAK,EAAE,CAAC;IAEX,MAAM,OAAO,GAAG,mBAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,mBAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAEvC,MAAM,IAAI,GAAG,mBAAO,CAAC,IAAI,EAAE,CAAC;IAC5B,MAAM,OAAO,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAE9C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AG,KAAK,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,IAAI,CAAC,CAAC;QACzD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;KAClC;AACH,CAAC;AAED,GAAG,EAAE,CAAC"} \ No newline at end of file diff --git a/lib/utils/format.d.ts b/lib/utils/format.d.ts new file mode 100644 index 0000000..e9f3b97 --- /dev/null +++ b/lib/utils/format.d.ts @@ -0,0 +1,8 @@ +import { GoogleWebsiteRank } from "./google"; +/** + * Formats the rank of a keyword as a string. + * @param keyword - The keyword string. + * @param rank - The rank of the keyword. + * @returns A formatted string. The rank will be displayed as a question mark if it is undefined. + */ +export declare function formatKeywordRank(keyword: string, rank?: GoogleWebsiteRank): string; diff --git a/lib/utils/format.js b/lib/utils/format.js new file mode 100644 index 0000000..029a599 --- /dev/null +++ b/lib/utils/format.js @@ -0,0 +1,26 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.formatKeywordRank = void 0; +const chalk_1 = __importDefault(require("chalk")); +/** + * Formats the rank of a keyword as a string. + * @param keyword - The keyword string. + * @param rank - The rank of the keyword. + * @returns A formatted string. The rank will be displayed as a question mark if it is undefined. + */ +function formatKeywordRank(keyword, rank) { + if (rank === undefined) + return `page ? rank ? ${keyword}`; + const pageStr = rank.page <= 0 + ? `page ${chalk_1.default.greenBright(rank.page + 1)}` + : `page ${chalk_1.default.redBright(rank.page + 1)}`; + const rankStr = rank.page <= 0 && rank.rank <= 2 + ? `rank ${chalk_1.default.greenBright(rank.rank + 1)}` + : `rank ${chalk_1.default.redBright(rank.rank + 1)}`; + return `${pageStr} ${rankStr} ${keyword}`; +} +exports.formatKeywordRank = formatKeywordRank; +//# sourceMappingURL=format.js.map \ No newline at end of file diff --git a/lib/utils/format.js.map b/lib/utils/format.js.map new file mode 100644 index 0000000..5725604 --- /dev/null +++ b/lib/utils/format.js.map @@ -0,0 +1 @@ +{"version":3,"file":"format.js","sourceRoot":"","sources":["../../src/utils/format.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAG1B;;;;;GAKG;AACH,SAAgB,iBAAiB,CAC/B,OAAe,EACf,IAAwB;IAExB,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,mBAAmB,OAAO,EAAE,CAAC;IAE5D,MAAM,OAAO,GACX,IAAI,CAAC,IAAI,IAAI,CAAC;QACZ,CAAC,CAAC,QAAQ,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE;QAC5C,CAAC,CAAC,QAAQ,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;IAE/C,MAAM,OAAO,GACX,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC;QAC9B,CAAC,CAAC,QAAQ,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE;QAC5C,CAAC,CAAC,QAAQ,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;IAE/C,OAAO,GAAG,OAAO,KAAK,OAAO,KAAK,OAAO,EAAE,CAAC;AAC9C,CAAC;AAjBD,8CAiBC"} \ No newline at end of file diff --git a/lib/utils/index.d.ts b/lib/utils/index.d.ts index be201dd..8aeda99 100644 --- a/lib/utils/index.d.ts +++ b/lib/utils/index.d.ts @@ -1 +1,2 @@ +export { formatKeywordRank } from "./format"; export { googleGetWebsiteRank, GoogleWebsiteRank } from "./google"; diff --git a/lib/utils/index.js b/lib/utils/index.js index 57d13f1..ffffa4a 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -1,6 +1,8 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.googleGetWebsiteRank = void 0; +exports.googleGetWebsiteRank = exports.formatKeywordRank = void 0; +var format_1 = require("./format"); +Object.defineProperty(exports, "formatKeywordRank", { enumerable: true, get: function () { return format_1.formatKeywordRank; } }); var google_1 = require("./google"); Object.defineProperty(exports, "googleGetWebsiteRank", { enumerable: true, get: function () { return google_1.googleGetWebsiteRank; } }); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/lib/utils/index.js.map b/lib/utils/index.js.map index d786e6a..bc98300 100644 --- a/lib/utils/index.js.map +++ b/lib/utils/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;AAAA,mCAAmE;AAA1D,8GAAA,oBAAoB,OAAA"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;AAAA,mCAA6C;AAApC,2GAAA,iBAAiB,OAAA;AAC1B,mCAAmE;AAA1D,8GAAA,oBAAoB,OAAA"} \ No newline at end of file diff --git a/src/google-rank.ts b/src/google-rank.ts index f816106..8a6d438 100644 --- a/src/google-rank.ts +++ b/src/google-rank.ts @@ -2,7 +2,9 @@ import chalk from "chalk"; import { program } from "commander"; -import { googleGetWebsiteRank, GoogleWebsiteRank } from "./utils"; +import * as utils from "./utils"; + +type RankPromise = Promise; async function run() { program @@ -17,31 +19,16 @@ async function run() { const opts = program.opts(); const maxPage = Number.parseInt(opts.maxPage); - const rankByKeywords: [string, Promise][] = []; + const rankByKeywords: [string, RankPromise][] = []; for (const keyword of keywords) { - const prom = googleGetWebsiteRank(website, keyword, { maxPage }); + const prom = utils.googleGetWebsiteRank(website, keyword, { maxPage }); rankByKeywords.push([keyword, prom]); } process.stdout.write(`Ranks for ${chalk.blueBright(website)} website:\n`); for (const [keyword, prom] of rankByKeywords) { - const rank = await prom; - if (rank === undefined) { - process.stdout.write(`page ? rank ?`); - } else { - if (rank.page <= 0) { - process.stdout.write(`page ${chalk.greenBright(rank.page + 1)}`); - } else { - process.stdout.write(`page ${chalk.redBright(rank.page + 1)}`); - } - - if (rank.page <= 0 && rank.rank <= 2) { - process.stdout.write(` rank ${chalk.greenBright(rank.rank + 1)}`); - } else { - process.stdout.write(` rank ${chalk.redBright(rank.rank + 1)}`); - } - } - process.stdout.write(` ${keyword}\n`); + const str = utils.formatKeywordRank(keyword, await prom); + process.stdout.write(`${str}\n`); } } diff --git a/src/utils/format.test.ts b/src/utils/format.test.ts new file mode 100644 index 0000000..d7783bd --- /dev/null +++ b/src/utils/format.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from "@jest/globals"; +import chalk from "chalk"; +import { formatKeywordRank } from "./format"; + +describe("Format the rank of a keyword as a string", () => { + it("should format undefined rank correctly", () => { + const str = formatKeywordRank("a keyword", undefined); + expect(str).toBe("page ? rank ? a keyword"); + }); + + it("should format green rank correctly", () => { + const str = formatKeywordRank("a keyword", { page: 0, rank: 2 }); + expect(str).toBe( + `page ${chalk.greenBright(1)} rank ${chalk.greenBright(3)} a keyword` + ); + }); + + it("should format green page and red rank correctly", () => { + const str = formatKeywordRank("a keyword", { page: 0, rank: 3 }); + expect(str).toBe( + `page ${chalk.greenBright(1)} rank ${chalk.redBright(4)} a keyword` + ); + }); + + it("should format red rank correctly", () => { + const str = formatKeywordRank("a keyword", { page: 1, rank: 0 }); + expect(str).toBe( + `page ${chalk.redBright(2)} rank ${chalk.redBright(1)} a keyword` + ); + }); +}); diff --git a/src/utils/format.ts b/src/utils/format.ts new file mode 100644 index 0000000..a121230 --- /dev/null +++ b/src/utils/format.ts @@ -0,0 +1,27 @@ +import chalk from "chalk"; +import { GoogleWebsiteRank } from "./google"; + +/** + * Formats the rank of a keyword as a string. + * @param keyword - The keyword string. + * @param rank - The rank of the keyword. + * @returns A formatted string. The rank will be displayed as a question mark if it is undefined. + */ +export function formatKeywordRank( + keyword: string, + rank?: GoogleWebsiteRank +): string { + if (rank === undefined) return `page ? rank ? ${keyword}`; + + const pageStr = + rank.page <= 0 + ? `page ${chalk.greenBright(rank.page + 1)}` + : `page ${chalk.redBright(rank.page + 1)}`; + + const rankStr = + rank.page <= 0 && rank.rank <= 2 + ? `rank ${chalk.greenBright(rank.rank + 1)}` + : `rank ${chalk.redBright(rank.rank + 1)}`; + + return `${pageStr} ${rankStr} ${keyword}`; +} diff --git a/src/utils/index.ts b/src/utils/index.ts index be201dd..8aeda99 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1,2 @@ +export { formatKeywordRank } from "./format"; export { googleGetWebsiteRank, GoogleWebsiteRank } from "./google"; From 1f1ca5103160648b6299030743a499eab8c08119 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Mon, 17 Jul 2023 22:51:27 +0700 Subject: [PATCH 3/4] feat: show loading output using ora --- lib/google-rank.js | 7 +- lib/google-rank.js.map | 2 +- package-lock.json | 246 +++++++++++++++++++++++++++++++++++++++-- package.json | 3 +- src/google-rank.ts | 8 +- 5 files changed, 253 insertions(+), 13 deletions(-) diff --git a/lib/google-rank.js b/lib/google-rank.js index 10d3426..e67c0b0 100755 --- a/lib/google-rank.js +++ b/lib/google-rank.js @@ -29,6 +29,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = __importDefault(require("chalk")); const commander_1 = require("commander"); +const ora_1 = __importDefault(require("ora")); const utils = __importStar(require("./utils")); async function run() { commander_1.program @@ -46,10 +47,14 @@ async function run() { rankByKeywords.push([keyword, prom]); } process.stdout.write(`Ranks for ${chalk_1.default.blueBright(website)} website:\n`); + const loading = (0, ora_1.default)("Getting ranks..."); + loading.start(); for (const [keyword, prom] of rankByKeywords) { + loading.text = `Getting ranks of ${chalk_1.default.blueBright(keyword)} keyword...`; const str = utils.formatKeywordRank(keyword, await prom); - process.stdout.write(`${str}\n`); + process.stdout.write(`\r\x1b[K${str}\n`); } + loading.stop(); } run(); //# sourceMappingURL=google-rank.js.map \ No newline at end of file diff --git a/lib/google-rank.js.map b/lib/google-rank.js.map index 8e77101..e588be8 100644 --- a/lib/google-rank.js.map +++ b/lib/google-rank.js.map @@ -1 +1 @@ -{"version":3,"file":"google-rank.js","sourceRoot":"","sources":["../src/google-rank.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,kDAA0B;AAC1B,yCAAoC;AACpC,+CAAiC;AAIjC,KAAK,UAAU,GAAG;IAChB,mBAAO;SACJ,QAAQ,CAAC,WAAW,EAAE,cAAc,CAAC;SACrC,SAAS,CAAC,eAAe,CAAC;SAC1B,MAAM,CAAC,qBAAqB,EAAE,4BAA4B,EAAE,GAAG,CAAC;SAChE,KAAK,EAAE,CAAC;IAEX,MAAM,OAAO,GAAG,mBAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,mBAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAEvC,MAAM,IAAI,GAAG,mBAAO,CAAC,IAAI,EAAE,CAAC;IAC5B,MAAM,OAAO,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAE9C,MAAM,cAAc,GAA4B,EAAE,CAAC;IACnD,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC9B,MAAM,IAAI,GAAG,KAAK,CAAC,oBAAoB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;QACvE,cAAc,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;KACtC;IAED,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,eAAK,CAAC,UAAU,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAC1E,KAAK,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,IAAI,cAAc,EAAE;QAC5C,MAAM,GAAG,GAAG,KAAK,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,IAAI,CAAC,CAAC;QACzD,OAAO,CAAC,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AC,OAAO,EAAE,IAAI,CAAC,IAAI,cAAc,EAAE;QAC5C,OAAO,CAAC,IAAI,GAAG,oBAAoB,eAAK,CAAC,UAAU,CAAC,OAAO,CAAC,aAAa,CAAC;QAC1E,MAAM,GAAG,GAAG,KAAK,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,IAAI,CAAC,CAAC;QACzD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;KAC1C;IACD,OAAO,CAAC,IAAI,EAAE,CAAC;AACjB,CAAC;AAED,GAAG,EAAE,CAAC"} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d1d09ee..e49f3a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "chalk": "^4.1.2", "commander": "^11.0.0", - "googlethis": "^1.7.1" + "googlethis": "^1.7.1", + "ora": "^5.4.1" }, "bin": { "google-rank": "lib/google-rank.js" @@ -1828,7 +1829,6 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", - "dev": true, "engines": { "node": ">=8" } @@ -2113,6 +2113,35 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, "node_modules/boolbase": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz", @@ -2193,6 +2222,29 @@ "node-int64": "^0.4.0" } }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -2392,6 +2444,28 @@ "integrity": "sha512-0TNiGstbQmCFwt4akjjBg5pLRTSyj/PkWQ1ZoO2zntmg9yLqSRxwEa4iCfQLGjqhiqBfOJa7W/E8wfGrTDmlZQ==", "dev": true }, + "node_modules/cli-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", + "integrity": "sha512-I/zHAwsKf9FqGoXM4WWRACob9+SNukZTd94DWF57E4toouRulbCxcUh6RKUEOQlYTHJnzkPMySvPNaaSLNfLZw==", + "dependencies": { + "restore-cursor": "^3.1.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/cli-spinners": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.9.0.tgz", + "integrity": "sha512-4/aL9X3Wh0yiMQlE+eeRhWP6vclO3QRtw1JHKIT0FFUs5FjpFmESqtMvYZ0+lbzBw900b95mS0hohy+qn2VK/g==", + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/cliui": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", @@ -2406,6 +2480,14 @@ "node": ">=12" } }, + "node_modules/clone": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz", + "integrity": "sha512-JQHZ2QMW6l3aH/j6xCqQThY/9OH4D/9ls34cgkUBiEeocRTU04tHfKPBsUK1PqZCUQM7GiA0IIXJSuXHI64Kbg==", + "engines": { + "node": ">=0.8" + } + }, "node_modules/co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -2605,6 +2687,17 @@ "node": ">=0.10.0" } }, + "node_modules/defaults": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/defaults/-/defaults-1.0.4.tgz", + "integrity": "sha512-eFuaLoy/Rxalv2kr+lqMlUnrDWV+3j4pljOIJgLIhI058IQfWJ7vXhyEIHu+HtC738klGALYxOKDO0bQP3tg8A==", + "dependencies": { + "clone": "^1.0.2" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/define-properties": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.2.0.tgz", @@ -4018,6 +4111,25 @@ "node": ">=10.17.0" } }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/ignore": { "version": "5.2.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz", @@ -4096,8 +4208,7 @@ "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "node_modules/internal-slot": { "version": "1.0.5", @@ -4239,6 +4350,14 @@ "node": ">=0.10.0" } }, + "node_modules/is-interactive": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-1.0.0.tgz", + "integrity": "sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==", + "engines": { + "node": ">=8" + } + }, "node_modules/is-negative-zero": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/is-negative-zero/-/is-negative-zero-2.0.2.tgz", @@ -4382,6 +4501,17 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-weakref": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-weakref/-/is-weakref-1.0.2.tgz", @@ -5248,6 +5378,21 @@ "integrity": "sha512-sReKOYJIJf74dhJONhU4e0/shzi1trVbSWDOhKYE5XV2O+H7Sb2Dihwuc7xWxVl+DgFPyTqIN3zMfT9cq5iWDg==", "dev": true }, + "node_modules/log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dependencies": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -5397,7 +5542,6 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", - "dev": true, "engines": { "node": ">=6" } @@ -5629,7 +5773,6 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz", "integrity": "sha512-kbpaSSGJTWdAY5KPVeMOKXSrPtr8C8C7wodJbcsd51jRnmD+GZu8Y0VoU6Dm5Z4vWr0Ig/1NKuWRKf7j5aaYSg==", - "dev": true, "dependencies": { "mimic-fn": "^2.1.0" }, @@ -5657,6 +5800,28 @@ "node": ">= 0.8.0" } }, + "node_modules/ora": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/ora/-/ora-5.4.1.tgz", + "integrity": "sha512-5b6Y85tPxZZ7QytO+BQzysW31HJku27cRIlkbAXaNx+BdcVi+LlRFmVXzeF6a7JCwJpyw5c4b+YSVImQIrBpuQ==", + "dependencies": { + "bl": "^4.1.0", + "chalk": "^4.1.0", + "cli-cursor": "^3.1.0", + "cli-spinners": "^2.5.0", + "is-interactive": "^1.0.0", + "is-unicode-supported": "^0.1.0", + "log-symbols": "^4.1.0", + "strip-ansi": "^6.0.0", + "wcwidth": "^1.0.1" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/p-limit": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", @@ -6221,6 +6386,19 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dev": true }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/redent": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/redent/-/redent-4.0.0.tgz", @@ -6325,6 +6503,18 @@ "node": ">=10" } }, + "node_modules/restore-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", + "integrity": "sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==", + "dependencies": { + "onetime": "^5.1.0", + "signal-exit": "^3.0.2" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/reusify": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", @@ -6422,6 +6612,25 @@ "queue-microtask": "^1.2.2" } }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/safe-regex-test": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/safe-regex-test/-/safe-regex-test-1.0.0.tgz", @@ -6507,8 +6716,7 @@ "node_modules/signal-exit": { "version": "3.0.7", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", - "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", - "dev": true + "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==" }, "node_modules/sisteransi": { "version": "1.0.5", @@ -6603,6 +6811,14 @@ "node": ">=8" } }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/string-length": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz", @@ -6706,7 +6922,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", - "dev": true, "dependencies": { "ansi-regex": "^5.0.1" }, @@ -7132,6 +7347,11 @@ "punycode": "^2.1.0" } }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", @@ -7177,6 +7397,14 @@ "makeerror": "1.0.12" } }, + "node_modules/wcwidth": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz", + "integrity": "sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg==", + "dependencies": { + "defaults": "^1.0.3" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 8f2aee2..75e66b0 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,8 @@ "dependencies": { "chalk": "^4.1.2", "commander": "^11.0.0", - "googlethis": "^1.7.1" + "googlethis": "^1.7.1", + "ora": "^5.4.1" }, "devDependencies": { "@actions-kit/dev": "^0.2.0", diff --git a/src/google-rank.ts b/src/google-rank.ts index 8a6d438..c512bf8 100644 --- a/src/google-rank.ts +++ b/src/google-rank.ts @@ -2,6 +2,7 @@ import chalk from "chalk"; import { program } from "commander"; +import ora from "ora"; import * as utils from "./utils"; type RankPromise = Promise; @@ -26,10 +27,15 @@ async function run() { } process.stdout.write(`Ranks for ${chalk.blueBright(website)} website:\n`); + + const loading = ora("Getting ranks..."); + loading.start(); for (const [keyword, prom] of rankByKeywords) { + loading.text = `Getting ranks of ${chalk.blueBright(keyword)} keyword...`; const str = utils.formatKeywordRank(keyword, await prom); - process.stdout.write(`${str}\n`); + process.stdout.write(`\r\x1b[K${str}\n`); } + loading.stop(); } run(); From f61765e793e17c86ae3d2d5ae6b7745443f3c81e Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Mon, 17 Jul 2023 22:54:19 +0700 Subject: [PATCH 4/4] feat: modify keyword rank format on undefined --- lib/utils/format.js | 24 +++++++++++++++--------- lib/utils/format.js.map | 2 +- src/utils/format.test.ts | 3 ++- src/utils/format.ts | 28 +++++++++++++++------------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/utils/format.js b/lib/utils/format.js index 029a599..f6a9ad2 100644 --- a/lib/utils/format.js +++ b/lib/utils/format.js @@ -5,6 +5,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); exports.formatKeywordRank = void 0; const chalk_1 = __importDefault(require("chalk")); +function formatPageRank(rank) { + if (rank === undefined) + return `page ${chalk_1.default.blackBright("?")}`; + return rank.page <= 0 + ? `page ${chalk_1.default.greenBright(rank.page + 1)}` + : `page ${chalk_1.default.redBright(rank.page + 1)}`; +} +function formatRank(rank) { + if (rank === undefined) + return `rank ${chalk_1.default.blackBright("?")}`; + return rank.page <= 0 && rank.rank <= 2 + ? `rank ${chalk_1.default.greenBright(rank.rank + 1)}` + : `rank ${chalk_1.default.redBright(rank.rank + 1)}`; +} /** * Formats the rank of a keyword as a string. * @param keyword - The keyword string. @@ -12,15 +26,7 @@ const chalk_1 = __importDefault(require("chalk")); * @returns A formatted string. The rank will be displayed as a question mark if it is undefined. */ function formatKeywordRank(keyword, rank) { - if (rank === undefined) - return `page ? rank ? ${keyword}`; - const pageStr = rank.page <= 0 - ? `page ${chalk_1.default.greenBright(rank.page + 1)}` - : `page ${chalk_1.default.redBright(rank.page + 1)}`; - const rankStr = rank.page <= 0 && rank.rank <= 2 - ? `rank ${chalk_1.default.greenBright(rank.rank + 1)}` - : `rank ${chalk_1.default.redBright(rank.rank + 1)}`; - return `${pageStr} ${rankStr} ${keyword}`; + return `${formatPageRank(rank)} ${formatRank(rank)} ${keyword}`; } exports.formatKeywordRank = formatKeywordRank; //# sourceMappingURL=format.js.map \ No newline at end of file diff --git a/lib/utils/format.js.map b/lib/utils/format.js.map index 5725604..9c168f3 100644 --- a/lib/utils/format.js.map +++ b/lib/utils/format.js.map @@ -1 +1 @@ -{"version":3,"file":"format.js","sourceRoot":"","sources":["../../src/utils/format.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAG1B;;;;;GAKG;AACH,SAAgB,iBAAiB,CAC/B,OAAe,EACf,IAAwB;IAExB,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,mBAAmB,OAAO,EAAE,CAAC;IAE5D,MAAM,OAAO,GACX,IAAI,CAAC,IAAI,IAAI,CAAC;QACZ,CAAC,CAAC,QAAQ,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE;QAC5C,CAAC,CAAC,QAAQ,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;IAE/C,MAAM,OAAO,GACX,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC;QAC9B,CAAC,CAAC,QAAQ,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE;QAC5C,CAAC,CAAC,QAAQ,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;IAE/C,OAAO,GAAG,OAAO,KAAK,OAAO,KAAK,OAAO,EAAE,CAAC;AAC9C,CAAC;AAjBD,8CAiBC"} \ No newline at end of file +{"version":3,"file":"format.js","sourceRoot":"","sources":["../../src/utils/format.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAG1B,SAAS,cAAc,CAAC,IAAwB;IAC9C,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,QAAQ,eAAK,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;IAChE,OAAO,IAAI,CAAC,IAAI,IAAI,CAAC;QACnB,CAAC,CAAC,QAAQ,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE;QAC5C,CAAC,CAAC,QAAQ,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;AAC/C,CAAC;AAED,SAAS,UAAU,CAAC,IAAwB;IAC1C,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,QAAQ,eAAK,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;IAChE,OAAO,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC;QACrC,CAAC,CAAC,QAAQ,eAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE;QAC5C,CAAC,CAAC,QAAQ,eAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;AAC/C,CAAC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAC/B,OAAe,EACf,IAAwB;IAExB,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,KAAK,OAAO,EAAE,CAAC;AACpE,CAAC;AALD,8CAKC"} \ No newline at end of file diff --git a/src/utils/format.test.ts b/src/utils/format.test.ts index d7783bd..2af0419 100644 --- a/src/utils/format.test.ts +++ b/src/utils/format.test.ts @@ -5,7 +5,8 @@ import { formatKeywordRank } from "./format"; describe("Format the rank of a keyword as a string", () => { it("should format undefined rank correctly", () => { const str = formatKeywordRank("a keyword", undefined); - expect(str).toBe("page ? rank ? a keyword"); + const mark = chalk.blackBright("?"); + expect(str).toBe(`page ${mark} rank ${mark} a keyword`); }); it("should format green rank correctly", () => { diff --git a/src/utils/format.ts b/src/utils/format.ts index a121230..095d850 100644 --- a/src/utils/format.ts +++ b/src/utils/format.ts @@ -1,6 +1,20 @@ import chalk from "chalk"; import { GoogleWebsiteRank } from "./google"; +function formatPageRank(rank?: GoogleWebsiteRank): string { + if (rank === undefined) return `page ${chalk.blackBright("?")}`; + return rank.page <= 0 + ? `page ${chalk.greenBright(rank.page + 1)}` + : `page ${chalk.redBright(rank.page + 1)}`; +} + +function formatRank(rank?: GoogleWebsiteRank): string { + if (rank === undefined) return `rank ${chalk.blackBright("?")}`; + return rank.page <= 0 && rank.rank <= 2 + ? `rank ${chalk.greenBright(rank.rank + 1)}` + : `rank ${chalk.redBright(rank.rank + 1)}`; +} + /** * Formats the rank of a keyword as a string. * @param keyword - The keyword string. @@ -11,17 +25,5 @@ export function formatKeywordRank( keyword: string, rank?: GoogleWebsiteRank ): string { - if (rank === undefined) return `page ? rank ? ${keyword}`; - - const pageStr = - rank.page <= 0 - ? `page ${chalk.greenBright(rank.page + 1)}` - : `page ${chalk.redBright(rank.page + 1)}`; - - const rankStr = - rank.page <= 0 && rank.rank <= 2 - ? `rank ${chalk.greenBright(rank.rank + 1)}` - : `rank ${chalk.redBright(rank.rank + 1)}`; - - return `${pageStr} ${rankStr} ${keyword}`; + return `${formatPageRank(rank)} ${formatRank(rank)} ${keyword}`; }