Skip to content

Commit

Permalink
feat: add publisher to JSR
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed May 12, 2024
1 parent a3e4dda commit 8764bee
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 10 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: workflow_dispatch

permissions:
contents: write
id-token: write

jobs:
publish_package:
Expand All @@ -25,14 +26,26 @@ jobs:
- name: Install modules
run: bun install

- name: Prepare to JSR publish
run: bun jsr

- name: Type-check
run: tsc --noEmit

- name: Setup Deno
uses: denoland/setup-deno@v1

- name: Publish package to JSR
run: deno publish --allow-dirty --unstable-sloppy-imports

- name: Install Node
uses: actions/setup-node@v4
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
scope: "@gramio"

- name: Publish package
- name: Publish package to NPM
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# @gramio/autoload

[![npm](https://img.shields.io/npm/v/@gramio/autoload?logo=npm&style=flat&labelColor=000&color=3b82f6)](https://www.npmjs.org/package/@gramio/autoload)
[![JSR](https://jsr.io/badges/@gramio/autoload)](https://jsr.io/@gramio/autoload)
[![JSR Score](https://jsr.io/badges/@gramio/autoload/score)](https://jsr.io/@gramio/autoload)

Autoload commands plugin for GramIO.

## Usage

> [full example](https://github.com/gramiojs/autoload/tree/main/example)
> [!IMPORTANT]
> Please read about [Lazy-load plugins](https://gramio.netlify.app/plugins/official/autoload.html)
## Register the plugin

```ts
Expand Down
Binary file modified bun.lockb
Binary file not shown.
12 changes: 12 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@gramio/i18n",
"version": "0.0.3",
"exports": "./src/index.ts",
"publish": {
"include": ["deno.json", "src", "README.md", "tsconfig.json"]
},
"imports": {
"glob": "npm:glob@^10.3.15",
"gramio": "jsr:@gramio/core@^0.0.32"
}
}
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@gramio/autoload",
"version": "0.0.2",
"version": "0.0.3",
"description": "Autoload commands plugin for GramIO",
"main": "dist/index.js",
"type": "commonjs",
"homepage": "https://github.com/gramiojs/autoload",
"readme": "https://gramio.netlify.app/",
"readme": "https://gramio.netlify.app/plugins/official/autoload.html",
"scripts": {
"prepublishOnly": "tsc && bunx tsc-alias"
"prepublishOnly": "tsc",
"jsr": "bun scripts/release-jsr.ts"
},
"keywords": [
"gramio",
Expand All @@ -19,9 +20,9 @@
"commander"
],
"devDependencies": {
"@types/bun": "^1.0.7",
"gramio": "^0.0.22",
"typescript": "^5.3.3"
"@types/bun": "^1.1.1",
"gramio": "^0.0.31",
"typescript": "^5.4.5"
},
"peerDependencies": {
"gramio": "^0.0.22"
Expand All @@ -30,6 +31,6 @@
"dist"
],
"dependencies": {
"glob": "^10.3.10"
"glob": "^10.3.15"
}
}
16 changes: 16 additions & 0 deletions scripts/release-jsr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { execSync } from "node:child_process";
import fs from "node:fs";

const version = execSync("npm pkg get version")
.toString()
.replace(/"|\n/gi, "");

const jsrConfig = JSON.parse(String(fs.readFileSync("deno.json")));

jsrConfig.version = version;

fs.writeFileSync("deno.json", JSON.stringify(jsrConfig, null, 4));

// execSync("bun x @teidesu/slow-types-compiler fix --entry deno.json");

console.log("Prepared to release on JSR!");
45 changes: 43 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,63 @@ import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { type GlobOptionsWithFileTypesTrue, glob } from "glob";
import { Plugin } from "gramio";
import { getPath } from "utils";
import { getPath } from "./utils";

/** Params that used in {@link AutoloadOptions.onLoad | onLoad} and {@link AutoloadOptions.onFinish | onFinish} hooks */
export interface AutoloadOptionsPathParams {
absolute: string;
relative: string;
}

/** Options for {@link autoload} plugin extended by {@link GlobOptionsWithFileTypesTrue} */
export interface AutoloadOptions
extends Omit<GlobOptionsWithFileTypesTrue, "cwd" | "withFileTypes"> {
/**
* [Glob patterns](<https://en.wikipedia.org/wiki/Glob_(programming)>)
* @default "**\/*.{ts,js,cjs,mjs}"
* */
pattern?: string;
/**
* The path to the folder
* @default "./commands"
* */
path?: string;
/** The hook that is called when loading a file */
onLoad?: (path: AutoloadOptionsPathParams) => unknown;
/** The hook that is called after loading all files */
onFinish?: (paths: AutoloadOptionsPathParams[]) => unknown;
}

export async function autoload(options?: AutoloadOptions) {
/**
* Autoload commands plugin for GramIO.
* @example
* ## Register the plugin
*
* ```ts
* // index.ts
* import { Bot } from "gramio";
* import { autoload } from "@gramio/autoload";
*
* const bot = new Bot(process.env.TOKEN as string)
* .extend(autoload())
* .onStart(console.log);
*
* bot.start();
*
* export type BotType = typeof bot;
* ```
*
* ## Create command
*
* ```ts
* // commands/command.ts
* import type { BotType } from "..";
*
* export default (bot: BotType) =>
* bot.command("start", (context) => context.send("hello!"));
* ```
*/
export async function autoload(options?: AutoloadOptions): Promise<Plugin> {
const pattern = options?.pattern ?? "**/*.{ts,js,cjs,mjs}";
const path = options?.path ?? "./commands";
const directoryPath = getPath(path);
Expand Down

0 comments on commit 8764bee

Please sign in to comment.