Skip to content

Commit

Permalink
Move to deno2node (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: Wojciech Pawlik <[email protected]>
  • Loading branch information
KnorpelSenf and wojpawlik authored Oct 16, 2021
1 parent 7da5e94 commit 11593b7
Show file tree
Hide file tree
Showing 30 changed files with 108 additions and 197 deletions.
4 changes: 3 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"profile": "https://github.com/wojpawlik",
"contributions": [
"ideas",
"review"
"review",
"infra",
"platform"
]
},
{
Expand Down
File renamed without changes.
83 changes: 44 additions & 39 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,60 @@
name: grammY

on:
push:
branches: [main]
pull_request:
branches: [main]
push:
branches: [main]
pull_request:
branches: [main]

jobs:
backport:
runs-on: ubuntu-latest
steps:
- name: Setup repo
uses: actions/checkout@v2
backport:
runs-on: ubuntu-latest
steps:
- name: Setup repo
uses: actions/checkout@v2

- name: Run backporting script
run: ./backport.sh --full
working-directory: node-backport
- name: Install dependencies
run: npm install --ignore-scripts

format-and-lint:
runs-on: ubuntu-latest
steps:
- name: Setup repo
uses: actions/checkout@v2
- name: Run backporting
run: npm run backport

- uses: denoland/setup-deno@main
with:
deno-version: v1.x
format-and-lint:
runs-on: ubuntu-latest
steps:
- name: Setup repo
uses: actions/checkout@v2

- name: Check Format
run: deno fmt --config deno.json --check
- uses: denoland/setup-deno@main
with:
deno-version: v1.x

- name: Lint
run: deno lint --config deno.json
- name: Check Format
run: deno fmt --config deno.json --check

test:
runs-on: ${{ matrix.os }} # runs a test on Ubuntu, Windows and macOS
- name: Lint
run: deno lint --config deno.json

strategy:
matrix:
os: [macOS-latest, windows-latest, ubuntu-latest]
test:
runs-on: ${{ matrix.os }} # runs a test on Ubuntu, Windows and macOS

steps:
- name: Setup repo
uses: actions/checkout@v2
strategy:
matrix:
os: [macOS-latest, windows-latest, ubuntu-latest]

- uses: denoland/setup-deno@main
with:
deno-version: v1.x
steps:
- name: Setup repo
uses: actions/checkout@v2

- name: Cache Dependencies
run: deno cache deno/src/mod.ts
- uses: denoland/setup-deno@main
with:
deno-version: v1.x

- name: Run Tests
run: deno test --config deno.json --allow-env
- name: Cache Dependencies
run: deno cache src/mod.ts

- name: Run Tests
run: deno test --config deno.json --allow-env

- name: Run Linting
run: deno lint --config deno.json
2 changes: 0 additions & 2 deletions node-backport/.gitignore → .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ package-lock.json
*.tgz

# Build output
.tsbuildinfo
src/
out/
5 changes: 1 addition & 4 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"recommendations": [
"denoland.vscode-deno",
"editorconfig.editorconfig"
]
"recommendations": ["denoland.vscode-deno", "editorconfig.editorconfig"]
}
52 changes: 33 additions & 19 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,16 @@ You will be responsible for maintaining it.
**TL;DR** working on grammY means working on a Deno project, and that is a good thing.

grammY started out as a hybrid project, targeting both Deno and Node.js as runtimes not long after the Deno 1.0 release.
Naturally, this poses a challenge to grammY.
So far, there are no sufficiently good tools to convert a codebase back and forth between the ecosystems.
As a result, grammY maintains its own backporting script to convert Deno code (`deno/` subdirectory) for the Node.js platform (`node-backport/` subdirectoy), which is much simpler than doing it the other way around.
In the beginning, this posed a challenge to grammY.
There were no sufficiently good tools to convert a codebase back and forth between the ecosystems, so we had to maintain our own shell scripts to convert the Deno code to run under Node.js.

The script is simple.
There are three steps to do in order to obtain TypeScript code for Node from the grammY codebase that runs on Deno.
However, after some time the amazing tool `deno2node` emerged out of grammY's need.
It solves this problem substantially better by providing a Deno-aware wrapper of the TypeScript compiler.
Hence, you can write a Deno project and directly compile it to JavaScript files that run under Node.js.

1. Platform-specifics (`Deno.open`) etc are replaced by their equivalents (`fs.createReadStream`).
2. Built-in functions in Deno (`fetch`) are replaced by equivalent dependencies (`node-fetch`)
3. Explicit file extensions are stripped because Node uses implicit ones

Steps 2. and 3. are achieved using regular coreutil operations on the codebase that replace file extensions in imports and inject polyfilling import calls for Node.js.
Step 1. is achieved by having extracted all platform-specific code into a file called `platform.ts` that resides in the top level of the project.
The backporting script's first operation (after duplicating the codebase into a fresh directory) is to replace the `platform.ts` file with a predefined second, Node-specific version of the same file that “coincidentally” exports exactly the same set of members with identical (or at least compatible) type signatures.
Also, all dependencies of grammY are imported and re-exported here, as the import syntax for external modules differs between the platforms.

Long story short: if you want to work on grammY, you effectively work on a Deno project.
In other words, working on grammY effectively means work on a Deno project.
We use Deno testing, Deno linting, and the [Deno extension](https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno) for VSCode.
We just make sure that _the code also runs on Node.js_, but this transpilation process is automated, and you usually don't even have to think about it.
You usual TypeScript tooling does not work—and once you tried developing for Deno, you know that this is a good thing.

> Note that not all plugins of grammY have to have the same setup: many of them only integrate with grammY itself, and hence can be written for Node and automatically ported to Deno via <https://skypack.dev/> and similar services.
Expand All @@ -60,15 +51,38 @@ There are several areas of contributions, and they have different ways to get yo
We're happy to hear from you if you want to report a bug, request a feature, or contribute anything else—also if it is not code.
There are no technical steps to this.

### Working on the Core of grammY
### Working on the Core of grammY using Deno (recommended)

If you just want to build from the newest version of the source code on GitHub, you can directly import from `https://raw.githubusercontent.com/grammyjs/grammY/main/src/mod.ts`.

If you want to read or modify grammY's code, you can do the following.

1. Install Deno from <https://deno.land>.
2. Use <https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno> or a similar extension if you are using a different editor.
3. `cd` into `deno/` and run `deno cache src/mod.ts`.
3. Clone this repo.
4. Run `deno cache src/mod.ts` in the root directory of the repo.

You are now ready to work on grammY.

Before you open a PR, make sure to run `deno test` and `deno lint` on the Deno codebase, i.e. inside the `deno/` subdir.
Before you open a PR, make sure to run `deno test`, `deno fmt` and `deno lint` on the Deno codebase.
Specify `--config deno.json` for the latter two commands.

### Working on the Core of grammY using Node.js

You can install grammY directly from source via

```sh
npm install grammyjs/grammy
```

which will download grammY and build the code locally.

If you want to read or modify grammY's code, you can do the following.

1. Clone this repo.
2. Install the dependencies via `npm install`.
This will also compile the project for you.
3. Use [`npm link`](https://docs.npmjs.com/cli/v7/commands/npm-link) to integrate grammY into your bot project.

### Working on an Official Plugin of grammY

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center"><a href="https://github.com/KnorpelSenf"><img src="https://avatars.githubusercontent.com/u/12952387?v=4?s=100" width="100px;" alt=""/><br /><sub><b>KnorpelSenf</b></sub></a><br /><a href="#ideas-KnorpelSenf" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/grammyjs/grammY/commits?author=KnorpelSenf" title="Code">💻</a> <a href="https://github.com/grammyjs/grammY/commits?author=KnorpelSenf" title="Documentation">📖</a> <a href="#design-KnorpelSenf" title="Design">🎨</a> <a href="#example-KnorpelSenf" title="Examples">💡</a> <a href="#question-KnorpelSenf" title="Answering Questions">💬</a> <a href="https://github.com/grammyjs/grammY/commits?author=KnorpelSenf" title="Tests">⚠️</a> <a href="#plugin-KnorpelSenf" title="Plugin/utility libraries">🔌</a> <a href="#platform-KnorpelSenf" title="Packaging/porting to new platform">📦</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3AKnorpelSenf" title="Reviewed Pull Requests">👀</a> <a href="#mentoring-KnorpelSenf" title="Mentoring">🧑‍🏫</a></td>
<td align="center"><a href="https://github.com/Tecardo1"><img src="https://avatars.githubusercontent.com/u/42873000?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tecardo1</b></sub></a><br /><a href="#plugin-Tecardo1" title="Plugin/utility libraries">🔌</a> <a href="#userTesting-Tecardo1" title="User Testing">📓</a></td>
<td align="center"><a href="https://github.com/wojpawlik"><img src="https://avatars.githubusercontent.com/u/23058303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Wojciech Pawlik</b></sub></a><br /><a href="#ideas-wojpawlik" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Awojpawlik" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/wojpawlik"><img src="https://avatars.githubusercontent.com/u/23058303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Wojciech Pawlik</b></sub></a><br /><a href="#ideas-wojpawlik" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Awojpawlik" title="Reviewed Pull Requests">👀</a> <a href="#infra-wojpawlik" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#platform-wojpawlik" title="Packaging/porting to new platform">📦</a></td>
<td align="center"><a href="https://github.com/MegaITA"><img src="https://avatars.githubusercontent.com/u/32493080?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alessandro Bertozzi</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=MegaITA" title="Documentation">📖</a></td>
<td align="center"><a href="https://trgwii.no/"><img src="https://avatars.githubusercontent.com/u/11262022?v=4?s=100" width="100px;" alt=""/><br /><sub><b>trgwii</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=trgwii" title="Code">💻</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Atrgwii" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/KnightNiwrem"><img src="https://avatars.githubusercontent.com/u/9781814?v=4?s=100" width="100px;" alt=""/><br /><sub><b>KnightNiwrem</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=KnightNiwrem" title="Code">💻</a> <a href="https://github.com/grammyjs/grammY/issues?q=author%3AKnightNiwrem" title="Bug reports">🐛</a> <a href="#plugin-KnightNiwrem" title="Plugin/utility libraries">🔌</a> <a href="https://github.com/grammyjs/grammY/commits?author=KnightNiwrem" title="Documentation">📖</a> <a href="#example-KnightNiwrem" title="Examples">💡</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3AKnightNiwrem" title="Reviewed Pull Requests">👀</a></td>
Expand Down
14 changes: 2 additions & 12 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@
"proseWrap": "preserve"
},
"files": {
"exclude": [
"node-backport/node_modules",
"node-backport/src",
"node-backport/out",
"node-backport/package-lock.json"
]
"exclude": ["./node_modules/", "./out/", "./package-lock.json"]
}
},
"lint": {
"files": {
"exclude": [
"node-backport/node_modules",
"node-backport/src",
"node-backport/out",
"node-backport/package-lock.json"
]
"exclude": ["./node_modules/", "./out/", "./package-lock.json"]
}
}
}
43 changes: 0 additions & 43 deletions node-backport/README.md

This file was deleted.

59 changes: 0 additions & 59 deletions node-backport/backport.sh

This file was deleted.

5 changes: 3 additions & 2 deletions node-backport/package.json → package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"url": "https://github.com/grammyjs/grammY"
},
"scripts": {
"build": "tsc"
"prepare": "npm run backport",
"backport": "deno2node tsconfig.json"
},
"dependencies": {
"@grammyjs/types": "^2.2.6",
Expand All @@ -25,7 +26,7 @@
"@types/debug": "^4.1.7",
"@types/node": "^12.20.27",
"@types/node-fetch": "^2.5.12",
"typescript": "^4.4.3"
"deno2node": "^1.0.0"
},
"files": [
"out/"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion deno/src/bot.ts → src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Context } from "./context.ts";
import { Api } from "./core/api.ts";
import { ApiClientOptions, WebhookReplyEnvelope } from "./core/client.ts";
import { GrammyError } from "./core/error.ts";
import { debug as d, Update, UserFromGetMe } from "./platform.ts";
import { debug as d, Update, UserFromGetMe } from "./platform.deno.ts";
const debug = d("grammy:bot");
const debugErr = d("grammy:error");

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion deno/src/context.ts → src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
Update,
User,
UserFromGetMe,
} from "./platform.ts";
} from "./platform.deno.ts";

type Other<M extends Methods<RawApi>, X extends string = never> = OtherApi<
RawApi,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { InlineKeyboardButton, KeyboardButton, LoginUrl } from "../platform.ts";
import {
InlineKeyboardButton,
KeyboardButton,
LoginUrl,
} from "../platform.deno.ts";

/**
* Use this class to simplify building a keyboard (something like this:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "../context.ts";
import { MiddlewareFn } from "../composer.ts";
import { debug as d } from "../platform.ts";
import { debug as d } from "../platform.deno.ts";
const debug = d("grammy:session");

type MaybePromise<T> = Promise<T> | T;
Expand Down
Loading

0 comments on commit 11593b7

Please sign in to comment.