From c041590788ba12e48f43475e23e6c30dceec2499 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 11 Jan 2025 10:40:16 +0800 Subject: [PATCH] feat: add libraries --- .gitignore | 1 + libraries/deno-commons-mod.ts | 27 ++++++++++++ libraries/deno-dingtalk-mod.ts | 79 ++++++++++++++++++++++++++++++++++ update-meta.rs | 2 +- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 libraries/deno-commons-mod.ts create mode 100644 libraries/deno-dingtalk-mod.ts diff --git a/.gitignore b/.gitignore index d4777d2..cffe83e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ # ---> macOS # General .DS_Store diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts new file mode 100644 index 0000000..a78c52a --- /dev/null +++ b/libraries/deno-commons-mod.ts @@ -0,0 +1,27 @@ +export function compareVersion(ver1: string, ver2: string) { + if (ver1 === ver2) { return 0; } + let ver1Parts = ver1.split("."); + let ver2Parts = ver2.split("."); + let ver1Main = parseInt(ver1Parts[0]); + let ver2Main = parseInt(ver2Parts[0]); + if (ver1Main > ver2Main) { return 1; } + if (ver1Main < ver2Main) { return -1; } + let ver1Second = parseInt(ver1Parts[1]); + let ver2Second = parseInt(ver2Parts[1]); + if (ver1Second > ver2Second) { return 1; } + if (ver1Second < ver2Second) { return -1; } + let ver1Third = parseInt(ver1Parts[2]); + let ver2Third = parseInt(ver2Parts[2]); + if (ver1Third > ver2Third) { return 1; } + if (ver1Third < ver2Third) { return -1; } + return 0; +} + +export function isOn(val: string) { + let lowerVal = (val == null)? val: val.toLowerCase(); + return lowerVal === "on" || lowerVal === "yes" || lowerVal === "1" || lowerVal === "true"; +} + +export function isEnvOn(envKey: string) { + return Deno.env.get(envKey); +} diff --git a/libraries/deno-dingtalk-mod.ts b/libraries/deno-dingtalk-mod.ts new file mode 100644 index 0000000..e595785 --- /dev/null +++ b/libraries/deno-dingtalk-mod.ts @@ -0,0 +1,79 @@ +import { hmac } from "https://deno.land/x/hmac@v2.0.1/mod.ts"; + +async function postHttpJson(url: string, body: any) { + const resp = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }); + if (resp.status !== 200) { + console.error("Send HTTP POST failed", resp); + throw `Send HTTP POST to: ${url}, failed: ${resp.status}`; + } + return resp; +} + +const BASE_DING_TALK_URL = "https://oapi.dingtalk.com/robot/send"; + +export interface SendDingTalkMessageOptions { + base_url?: string; + access_token: string; + sec_token?: string; +} + +export interface DingTalkMessageAt { + atMobiles?: Array; + atUserIds?: Array; + isAtAll?: boolean, +} + +export interface DingTalkTextMessage { + content: string; + at?: DingTalkMessageAt; +} + +export interface DingTalkMarkdownMessage { + title?: string, + content: string; + at?: DingTalkMessageAt; +} + +export async function sendDingTalkTextMessage(message: DingTalkTextMessage, options: SendDingTalkMessageOptions) { + return await sendDingTalkMessage({ + msgtype: "text", + text: { + content: message.content, + } + }, options); +} + +export async function sendDingTalkMarkdownMessage(message: DingTalkMarkdownMessage, options: SendDingTalkMessageOptions) { + return await sendDingTalkMessage({ + msgtype: "markdown", + markdown: { + title: message.title || "untitled", + text: message.content, + } + }, options); +} + +export async function sendDingTalkMessage(message: any, options: SendDingTalkMessageOptions) { + let send_url = options.base_url || BASE_DING_TALK_URL; + send_url += "?access_token=" + encodeURIComponent(options.access_token); + if (options.sec_token) { + const timestamp = new Date().getTime(); + const timestamp_and_secret = `${timestamp}\n${options.sec_token}`; + const sec_token_sign = hmac("sha256", options.sec_token, timestamp_and_secret, "utf8", "base64"); + send_url += "×tamp=" + timestamp; + send_url += "&sign=" + encodeURIComponent(sec_token_sign); + } + const resp = await postHttpJson(send_url, message); + const send_ding_talk_resp_body = await resp.json(); + if (send_ding_talk_resp_body.errcode !== 0) { + console.error("Send DingTalk message failed", send_ding_talk_resp_body); + throw `Send DingTalk message failed: ${send_ding_talk_resp_body.errcode}`; + } +} + diff --git a/update-meta.rs b/update-meta.rs index 9305994..4bb6114 100755 --- a/update-meta.rs +++ b/update-meta.rs @@ -40,7 +40,7 @@ fn main() -> XResult<()> { debugging!("Skip none dir: {}", script_dir); continue; } - if /*"update-meta-rs" == script_dir ||*/ script_dir.starts_with(".") { + if "libraries" == script_dir || script_dir.starts_with(".") { debugging!("Skip update meta rs: {}", script_dir); continue; }