|
1 | | -# Creating your first extension |
| 1 | +# Creating your first Extension |
2 | 2 |
|
3 | 3 | ## Before you start... |
4 | 4 | Make sure you have `node` installed on your machine, you can verify this by using `node -v` on a terminal like Windows CMD. |
5 | | -> If you don't have `node` installed, you can get it [here](https://nodejs.org/en/download) |
6 | 5 |
|
7 | | -## Creating a workspace |
8 | | -Create a folder, somewhere in your machine, let's name it `my-first-extension`, now let's open it with an IDE like Visual Studio Code. |
| 6 | +> If you don't have `node` installed, you can get it [here](https://nodejs.org/en/download). |
| 7 | +
|
| 8 | +## Creating a Workspace |
| 9 | +Create a new folder, somewhere in your machine, let's name it `my-first-extension`, now let's open it with an IDE like Visual Studio Code. |
| 10 | + |
9 | 11 | Once opened, open a terminal, and run the following commands: |
| 12 | +1. ```bash |
| 13 | + npm i --g typescript |
| 14 | + ``` |
| 15 | +2. ```bash |
| 16 | + npm init --y |
| 17 | + ``` |
| 18 | +3. ```bash |
| 19 | + npm i typescript github:tryforge/ForgeScript#dev --save-dev |
| 20 | + ``` |
| 21 | +4. ```bash |
| 22 | + tsc --init --target es2022 --rootdir src --outdir dist |
| 23 | + ``` |
| 24 | + |
| 25 | +Now then, let's create a folder called `src`, which is where all our code will reside. |
| 26 | +
|
| 27 | +## Compiling your Changes |
| 28 | +Before you push your changes to GitHub, make sure to compile them. We use TypeScript therefore compiling is a mandatory step in the development of an extension. |
| 29 | +
|
| 30 | +After you are done with your changes, run the following command in the terminal: |
10 | 31 | ```bash |
11 | | -npm i --g typescript |
| 32 | +tsc |
| 33 | +``` |
12 | 34 |
|
13 | | -npm init --y |
| 35 | +Technically, after the process has finished, everything is now ready to be pushed to your repository on GitHub. |
14 | 36 |
|
15 | | -npm i typescript github:tryforge/ForgeScript#dev --save-dev |
| 37 | +If you want to generate metadata and docs for your extension, **do not** push your changes already after compiling. Instead, read [the next step](#generating-metadata--docs). |
16 | 38 |
|
17 | | -tsc --init --target es2022 --rootdir src --outdir dist |
| 39 | +## Generating Metadata & Docs |
| 40 | +In order to generate metadata and docs for your extension, you will need to create a new `docgen.ts` file in your `src` root folder containing this code: |
| 41 | +```ts |
| 42 | +import { generateMetadata } from "@tryforge/forgescript" |
| 43 | +
|
| 44 | +generateMetadata(`${__dirname}/native`, "native") |
18 | 45 | ``` |
| 46 | +> *Replace `native` if your native functions folder has a different name.* |
19 | 47 |
|
20 | | -Now then, let's create a folder called `src`, which is where all our code will reside. |
| 48 | +\ |
| 49 | +Then, add the following script to the existing scripts in your `package.json` file: |
| 50 | +
|
| 51 | +```json |
| 52 | +{ |
| 53 | + "scripts": { |
| 54 | + "docgen": "tsc && typedoc src/index.ts && node dist/docgen" |
| 55 | + }, |
| 56 | +} |
| 57 | +``` |
| 58 | +\ |
| 59 | +Now, **after** compiling your changes, run this command: |
| 60 | +```bash |
| 61 | +npm run docgen |
| 62 | +``` |
| 63 | +
|
| 64 | +Once this step is done you are ready to push everything to GitHub. If you prefer adding changelog notes to your commits, head over to [the next step](#initializing-changelog-notes). |
| 65 | +
|
| 66 | +--- |
| 67 | +
|
| 68 | +### Initializing Changelog Notes |
| 69 | +> Before initializing, make sure you have completed the [previous step](#generating-metadata--docs). If you do not want to add a note to your commit, simply stick to `npm run docgen`. |
| 70 | +
|
| 71 | +First you need to add a new function by creating a `prompt.ts` file somewhere in your `src` root folder containing the code below without any modification: |
| 72 | +```ts |
| 73 | +import { stdin, stdout } from "process" |
| 74 | +import { createInterface } from "readline" |
| 75 | +
|
| 76 | +export default async function(q: string) { |
| 77 | + const itf = createInterface(stdin, stdout) |
| 78 | + return new Promise<string>(r => { |
| 79 | + itf.question(q, input => { |
| 80 | + itf.close() |
| 81 | + r(input) |
| 82 | + }) |
| 83 | + }) |
| 84 | +} |
| 85 | +``` |
| 86 | +\ |
| 87 | +Create a new `commit.ts` file in your `src` root folder. Copy and paste the code below into the newly created file. You may need to adjust the import for the `prompt` function, depending on where you have this file located in your project. |
| 88 | +```ts |
| 89 | +import { execSync } from "child_process" |
| 90 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" |
| 91 | +import { join } from "path" |
| 92 | +import prompt from "./functions/prompt" |
| 93 | +
|
| 94 | +const path = "./metadata" |
| 95 | +if (!existsSync(path)) mkdirSync(path) |
| 96 | +
|
| 97 | +const version = require("../package.json").version |
| 98 | +
|
| 99 | +async function main() { |
| 100 | + let skip = false |
| 101 | +
|
| 102 | + const msg = (await prompt("Please write the commit message: ")).replace( |
| 103 | + /(--?(\w+))/gim, (match) => { |
| 104 | + const name = /(\w+)/.exec(match)![1].toLowerCase() |
| 105 | + |
| 106 | + switch (name) { |
| 107 | + case "hide": { |
| 108 | + skip = true |
| 109 | + break |
| 110 | + } |
| 111 | +
|
| 112 | + default: { |
| 113 | + throw new Error(`--${name} is not a valid flag.`) |
| 114 | + } |
| 115 | + } |
| 116 | +
|
| 117 | + return "" |
| 118 | + } |
| 119 | + ).trim() |
| 120 | +
|
| 121 | + const fileName = join(path, "changelogs.json") |
| 122 | + const json: Record<string, string[]> = existsSync(fileName) ? JSON.parse(readFileSync(fileName, "utf-8")) : {} |
| 123 | + json[version] ??= [] |
| 124 | +
|
| 125 | + if (!skip) { |
| 126 | + json[version].unshift(msg) |
| 127 | + writeFileSync(fileName, JSON.stringify(json), "utf-8") |
| 128 | + } |
| 129 | +
|
| 130 | + const branch = await prompt("Write the branch name to push to (defaults to dev): ") || "dev" |
| 131 | + const escapedMsg = msg.replace(/\$/g, "\\$") |
| 132 | +
|
| 133 | + execSync("git branch -M " + branch + " && git add . && git commit -m \"" + escapedMsg + "\" && git push -u origin " + branch, { |
| 134 | + stdio: "inherit" |
| 135 | + }) |
| 136 | +} |
| 137 | +
|
| 138 | +// Nothing |
| 139 | +main() |
| 140 | +``` |
| 141 | +\ |
| 142 | +Add the following script to the existing scripts in your `package.json` file: |
| 143 | +```json |
| 144 | +{ |
| 145 | + "scripts": { |
| 146 | + "commit": "npm run docgen && node dist/commit" |
| 147 | + }, |
| 148 | +} |
| 149 | +``` |
| 150 | +\ |
| 151 | +Instead of running `npm run docgen` after compiling, run: |
| 152 | +```bash |
| 153 | +npm run commit |
| 154 | +``` |
| 155 | +This command allows you to add an additional changelog note to your commit and automatically pushes your changes. If your extension was added as an official or community extension, all changelog notes will be displayed in the "Changelog" tab on the docs. |
| 156 | +
|
| 157 | +> Note that you should not run both scripts for the same commit. Either run `docgen` or `commit`, whatever suits your needs best. |
0 commit comments