From cdca979a7ed0d99ddbfe22d0204f96a243df4f4d Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Sat, 6 May 2023 12:28:48 +0000 Subject: [PATCH] feat: babashka scripts --- Dockerfile | 1 + babashka/pst.clj | 5 +++++ babashka/scripts.json | 3 +++ index.ts | 25 +++++++++++++++++++++++++ tsconfig.json | 2 +- util/setup.sh | 4 ++++ util/sleep.ts | 1 + 7 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 babashka/pst.clj create mode 100644 babashka/scripts.json create mode 100644 util/sleep.ts diff --git a/Dockerfile b/Dockerfile index e987238..19b1c93 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ FROM node:lts-alpine RUN apk add git bash curl +RUN bash < <(curl -s https://raw.githubusercontent.com/babashka/babashka/master/install) WORKDIR /app diff --git a/babashka/pst.clj b/babashka/pst.clj new file mode 100644 index 0000000..b7d36c2 --- /dev/null +++ b/babashka/pst.clj @@ -0,0 +1,5 @@ +(def now (java.time.ZonedDateTime/now)) +(def LA-timezone (java.time.ZoneId/of "America/Los_Angeles")) +(def LA-time (.withZoneSameInstant now LA-timezone)) +(def pattern (java.time.format.DateTimeFormatter/ofPattern "HH:mm")) +(println (.format LA-time pattern)) \ No newline at end of file diff --git a/babashka/scripts.json b/babashka/scripts.json new file mode 100644 index 0000000..d1bd9e3 --- /dev/null +++ b/babashka/scripts.json @@ -0,0 +1,3 @@ +[ + { "file": "pst.clj", "route": "/bb/getPstTime", "method": "GET" } +] \ No newline at end of file diff --git a/index.ts b/index.ts index 75eaa15..d37cf64 100644 --- a/index.ts +++ b/index.ts @@ -3,6 +3,7 @@ import bodyParser from 'body-parser'; import 'dotenv/config'; import { execa } from 'execa'; import { validateJsonWebhook } from './util/validateJsonWebhook.js'; +import babashkaScripts from './babashka/scripts.json' assert { type: 'json' } const app = express() app.use(bodyParser.json()) @@ -34,4 +35,28 @@ app.post('/wh/updateDocsJson', async (req, res) => { }) }) +for (const script of babashkaScripts) { + switch (script.method) { + case 'GET': + app.get(script.route, async (req, res) => { + const command = await execa('bb', [`babashka/${script.file}`]) + res.send({ + success: command.exitCode === 0 ? true : false, + cmdoutput: command.stdout + }) + }) + break; + case 'POST': + app.post(script.route, async (req, res) => { + const command = await execa('bb', [`babashka/${script.file}`]) + res.send({ + success: command.exitCode === 0 ? true : false, + cmdoutput: command.stdout + }) + }) + break; + } + console.log(`Babashka script ${script.file} was registered successfully in ${script.method} ${script.route}`) +} + app.listen(3000, () => console.log('Listening!')) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 7020110..903c68e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -39,7 +39,7 @@ // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ + "resolveJsonModule": true, /* Enable importing .json files. */ // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ diff --git a/util/setup.sh b/util/setup.sh index 727855b..e22e28a 100644 --- a/util/setup.sh +++ b/util/setup.sh @@ -9,6 +9,10 @@ mkdir repos cd repos echo " done" +echo -ne "Installing sern CLI" +npm install -g @sern/cli +echo " done" + echo "Cloning repos" # handler (clone it as sernHandlerV2) echo -ne "- handler" diff --git a/util/sleep.ts b/util/sleep.ts new file mode 100644 index 0000000..3aecb36 --- /dev/null +++ b/util/sleep.ts @@ -0,0 +1 @@ +export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); \ No newline at end of file