From cfd298a05252f382c187301d45abaac18359980f Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 19 Mar 2024 10:57:04 +0530 Subject: [PATCH] [workers] Start migrating workers to the monorepo --- infra/workers/.gitignore | 10 +++++++++ infra/workers/.prettierrc.json | 3 +++ infra/workers/README.md | 32 +++++++++++++++++++++++++++ infra/workers/tsconfig.base.json | 38 ++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 infra/workers/.gitignore create mode 100644 infra/workers/.prettierrc.json create mode 100644 infra/workers/README.md create mode 100644 infra/workers/tsconfig.base.json diff --git a/infra/workers/.gitignore b/infra/workers/.gitignore new file mode 100644 index 000000000..7f811e128 --- /dev/null +++ b/infra/workers/.gitignore @@ -0,0 +1,10 @@ +node_modules/ +.DS_Store +.wrangler/ + +# Workers mostly have dev dependencies, and usually only wrangler, so exact +# version details and the resulting `yarn.lock` is unnecessary noise. +# +# If we need to pin some runtime dependency, we can pin it to an exact version +# in the package.json itself. +yarn.lock diff --git a/infra/workers/.prettierrc.json b/infra/workers/.prettierrc.json new file mode 100644 index 000000000..0a02bcefd --- /dev/null +++ b/infra/workers/.prettierrc.json @@ -0,0 +1,3 @@ +{ + "tabWidth": 4 +} diff --git a/infra/workers/README.md b/infra/workers/README.md new file mode 100644 index 000000000..887dd4357 --- /dev/null +++ b/infra/workers/README.md @@ -0,0 +1,32 @@ +# Cloudflare Workers + +Source code for our [Cloudflare +Workers](https://developers.cloudflare.com/workers/). + +Each worker is a self contained directory with its each `package.json`. + +## Deploying + +* Switch to a worker directory, e.g. `cd health-check`. + +* Install dependencies (if needed) with `yarn` + +* Login into wrangler (if needed) using `yarn wrangler login` + +* Deploy! `yarn wrangler publish` + +Wrangler is the CLI provided by Cloudflare to manage workers. Apart from +deploying, it also allows us to stream logs from running workers by using `yarn +wrangler tail`. + +## Creating a new worker + +Copy paste an existing one. Unironically this is a good option because the +Cloudflare template has a lot of unnecessary noise, but if really do want to +create one from scratch, use `npm create cloudflare@latest`. + +To import an existing worker from the Cloudflare dashboard, use + +```sh +npm create cloudflare@2 existing-worker-name -- --type pre-existing --existing-script existing-worker-name +``` diff --git a/infra/workers/tsconfig.base.json b/infra/workers/tsconfig.base.json new file mode 100644 index 000000000..f330c4ca5 --- /dev/null +++ b/infra/workers/tsconfig.base.json @@ -0,0 +1,38 @@ +{ + /* A shared TSConfig for use by TypeScript Cloudflare Workers */ + /* TSConfig docs: https://aka.ms/tsconfig.json */ + "compilerOptions": { + /* tsc is used for by us for type checking, not compilation (the + Cloudflare workers runtime natively supports TypeScript) */ + "noEmit": true, + + /* The Workers runtime supports the latest and greatest */ + /* https://developers.cloudflare.com/workers/reference/languages/#javascript--typescript */ + "lib": ["esnext"], + "target": "esnext", + "module": "esnext", + + /* Types that are implicitly available */ + /* https://www.npmjs.com/package/@cloudflare/workers-types */ + "types": ["@cloudflare/workers-types"], + + /* Tell TypeScript how to lookup the file for a given import */ + "moduleResolution": "node", + + /* Speed things up by not type checking `node_modules` */ + "skipLibCheck": true, + /* Require the `type` modifier when importing types */ + "verbatimModuleSyntax": true, + /* Enable importing .json files */ + "resolveJsonModule": true, + + /* strict and then some */ + "strict": true, + "noImplicitReturns": true, + "noUnusedParameters": true, + "noUnusedLocals": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true + } +}