Skip to content

Commit

Permalink
build(npm): Create and publish typescript defs as npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
mart3323 committed Jul 21, 2022
1 parent 3d4cec9 commit 6383d11
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 36 deletions.
98 changes: 66 additions & 32 deletions .github/workflows/betabuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,73 @@ on: push
jobs:
# This workflow contains a single job called "build"
build:
outputs:
artifact_name: ${{ steps.fname.outputs.result }}
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- uses: actions/github-script@v2
id: fname
with:
result-encoding: string
script: |
const fs = require("fs");
let file = fs.readFileSync("./gradle.properties");
file = file.toString().split("\n").map(e => e.trim().startsWith("mod_version") ? `${e}-beta-${process.env.GITHUB_SHA.substring(0, 7)}` : e).join("\n");
fs.writeFileSync("./gradle.properties", file);
const mod_version = file.toString().split("\n").filter(e => e.trim().startsWith("mod_version"))[0].split("=")[1].trim();
const archives_base_name = file.toString().split("\n").filter(e => e.trim().startsWith("archives_base_name"))[0].split("=")[1].trim();
return `${archives_base_name}-${mod_version}`;
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build

- uses: actions/upload-artifact@v2
with:
name: ${{ steps.fname.outputs.result }}
path: ./dist/**/*
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- uses: actions/github-script@v2
id: fname
with:
result-encoding: string
script: |
const fs = require("fs");
let file = fs.readFileSync("./gradle.properties");
file = file.toString().split("\n").map(e => e.trim().startsWith("mod_version") ? `${e}-beta-${process.env.GITHUB_SHA.substring(0, 7)}` : e).join("\n");
fs.writeFileSync("./gradle.properties", file);
const mod_version = file.toString().split("\n").filter(e => e.trim().startsWith("mod_version"))[0].split("=")[1].trim();
const archives_base_name = file.toString().split("\n").filter(e => e.trim().startsWith("archives_base_name"))[0].split("=")[1].trim();
return `${archives_base_name}-${mod_version}`;
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '17'

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build

- uses: actions/upload-artifact@v2
with:
name: ${{ steps.fname.outputs.result }}
path: ./dist/**/*

publish_npm:
runs-on: ubuntu-latest
needs:
- build
steps:
- name: Download Artifacts
uses: actions/download-artifact@v2
with:
name: ${{ needs.build.outputs.artifact_name }}
path: dist

- uses: actions/setup-node@v3
name: Setup node
with:
node-version: latest
registry-url: https://npm.pkg.github.com/

- uses: actions/github-script@v2
id: shortsha
name: Calculate short SHA
with:
result-encoding: string
script: return process.env.GITHUB_SHA.substring(0, 7);

- name: Publish to node
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cd dist/typescript
npm version 0.0.0-${{ steps.shortsha.outputs.result }}
npm publish
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,39 @@ jobs:
cd dist/python
python setup.py sdist bdist_wheel
twine upload dist/*
publish_npm:
runs-on: ubuntu-latest
needs:
- build_1_19
steps:
- name: Download Artifacts
uses: actions/download-artifact@v2
with:
name: main-1.19
path: dist

- uses: actions/setup-node@v3
name: Setup node
with:
node-version: latest
registry-url: https://npm.pkg.github.com/

- name: Extract Version Number
uses: actions/github-script@v5
id: version_num
env:
REF: '${{ github.ref }}'
with:
result-encoding: string
script: |
return `${process.env.REF.replace(/\/?refs\/ta4gs\//, '')}`
- name: Publish to node
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cd dist/typescript
npm version ${{ steps.version_num.outputs.result }}
npm publish
2 changes: 1 addition & 1 deletion .github/workflows/releasebuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
uses: actions/upload-artifact@v2
with:
name: ${{ inputs.branch }}
path: ./dist/**/*
path: ./dist/**/*
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean run(DocletEnvironment environment) {
Set<EventParser> eventClasses = new LinkedHashSet<>();


outputTS = new FileHandler(new File(OutputDirectory.outputDir, "JsMacros-" + Version.version + ".d.ts"));
outputTS = new FileHandler(new File(OutputDirectory.outputDir, "JsMacros.d.ts"));

if (!OutputDirectory.outputDir.exists() && !OutputDirectory.outputDir.mkdirs()) {
reporter.print(Diagnostic.Kind.ERROR, "Failed to create version dir\n");
Expand Down
43 changes: 43 additions & 0 deletions docs/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# JsMacros-typescript
Type information for the JsMacros minecraft mod

# Setup
## Install the package
[Detailed guide](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry#installing-a-package)

1. Generate a personal access token
2. Install the version of the package that matches your mod version (for full releases), or 0.0.0-sha (for beta releases)
`$ npm install jsmacros/jsmacros-typescript@1.7.0` (release 1.7.0)
`$ npm install jsmacros/jsmacros-typescript@0.0.0-30d74d2` (beta-30d74d2)

## Add a tsconfig.json file
### Mandatory fields
These fields tell typescript to use the ambient type definitions without having to import them

`compilerOptions.typeRoots: ["node_modules"]`
`compilerOptions.types: ["wagyourtail/jsmacros-typescript"]`

`skipLibCheck: true`: Prevents typescript from reporting errors in the type declaration file - the errors are caused by differences in the typescript and java type systems and are unavoidable

### Example config
```json
{
"include": ["src"],
"compilerOptions": {
"outDir": "dist",
"typeRoots": ["node_modules"],
"types": ["jsmacros/jsmacros-typescript"],
"skipLibCheck": true
}
}
```

# Compiling
If you're writing typescript then you need to compile it to javascript before you can use it in the mod.

1. Compile the scripts
Compile once:
`$ tsc`
or compile automatically whenever you make changes to the sources
`$ tsc --watch`
2. In the mod, select the compiled files from the `outDir` folder instead of the source files
2 changes: 2 additions & 0 deletions docs/typescript/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="headers/Graal.d.ts"/>
/// <reference path="headers/JsMacros.d.ts"/>
33 changes: 33 additions & 0 deletions docs/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@jsmacros/jsmacros-typescript",
"version": "0.0.1",
"description": "Typescript definitions & configuration to compile typescript into valid JsMacros code",
"main": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/JsMacros/JsMacros.git"
},
"keywords": [
"minecraft",
"mod",
"jsmacros",
"jsm",
"types",
"typescript",
"macro",
"macros"
],
"files": ["headers", "index.d.ts", "tsconfig.json"],
"author": "William Gray <wagyourtail@wagyourtail.xyz> (https://wagyourtail.xyz/)",
"license": "MPL-2.0",
"bugs": {
"url": "https://github.com/JsMacros/JsMacros/issues"
},
"homepage": "https://github.com/JsMacros/JsMacros#readme",
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
}
}
3 changes: 1 addition & 2 deletions docs/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
"skipLibCheck": true
},
"include": [
"scripts/**/*.ts",
"headers/*.d.ts"
],
"exclude": [
"build/*"
]
}
}

0 comments on commit 6383d11

Please sign in to comment.