Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
feat: complete ir
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaringe committed Jan 23, 2021
1 parent 2c338fb commit d504214
Show file tree
Hide file tree
Showing 28 changed files with 2,616 additions and 2,177 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# @info
# https://github.community/t5/GitHub-Actions/Start-a-workflow-from-another-workflow/td-p/37586
name: main
on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
# https://github.com/JamesIves/github-pages-deploy-action#getting-started-airplane
- name: checkout 🛎️
uses: actions/checkout@v2
with:
persist-credentials: false
# https://github.com/denolib/setup-deno
- uses: denolib/setup-deno@master
with:
deno-version: 1.4.0
- uses: actions/setup-node@v1
with:
node-version: 15.0.1
- name: install
run: yarn
- name: test
run: ./rad test
# - name: deploy
# uses: peaceiris/actions-gh-pages@v3
# with:
# github_token: ${{ secrets.GITHUB_TOKEN }}
# publish_dir: ./public

# release
# - name: setup-node
# uses: actions/setup-node@v1
# with:
# node-version: 12
# - name: Release
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: |
# npm init -y \
# && \
# npm install -D \
# "@semantic-release/commit-analyzer" \
# "@semantic-release/release-notes-generator" \
# "@semantic-release/github" \
# "@semantic-release/exec" \
# && \
# npx semantic-release
Empty file added docs/typescript-guide.md
Empty file.
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"name": "@dino-dna/factorio-type-kit",
"name": "factorio-type-kit",
"version": "1.0.0",
"main": "index.js",
"main": "src/index.js",
"bin": "src/bin.js",
"author": "cdaringe <cdaringe@gmail.com>",
"license": "MIT",
"files": [
"src/**/*.js"
],
"dependencies": {
"bluebird": "^3.7.2",
"happy-dom": "^2.1.3",
"json-decycle": "^1.0.0",
"json-schema-to-typescript": "^10.0.3",
"meow": "^9.0.0",
"node-fetch": "^2.6.1",
"turndown": "^7.0.0",
"whatwg-encoding": "^1.0.5"
"turndown": "^7.0.0"
},
"devDependencies": {
"@types/bluebird": "^3.5.33",
"@types/json-schema": "^7.0.6",
"@types/node-fetch": "^2.5.7",
"@types/turndown": "^5.0.0",
"ava": "^3.15.0",
"del-cli": "^3.0.1",
"prettier": "^2.2.1",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
Expand Down
8 changes: 3 additions & 5 deletions rad.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// @ts-ignore
import type { Task, Tasks } from "https://deno.land/x/rad/src/mod.ts";

const generateApiJson: Task = `docker run --rm -v $PWD:/app cdaringe/factorio-api-scraper:latest`;

const bootstrap: Task = `yarn && npx lerna bootstrap`;
const format: Task = `npx prettier --write .`;
const test: Task = `npx ava --coverage`;
const run: Task = `node -r ts-node/register/transpile-only src/bin.ts`;
Expand All @@ -14,12 +11,13 @@ const runAndFormat: Task = {
await sh(format);
},
};

const clean: Task = 'npx del "src/**/*.js" "./*.js"';
export const tasks: Tasks = {
...{ generateApiJson, gaj: generateApiJson },
...{ clean, c: clean },
...{ format, f: format },
...{ dev: `node -r ts-node/register src/factorio.ts` },
...{ debug: `node --inspect-brk -r ts-node/register src/factorio.ts` },
...{ bootstrap, b: bootstrap },
...{ run, r: run },
...{ test, t: test },
...{ runAndFormat, raf: runAndFormat },
Expand Down
53 changes: 46 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
# Problem
# factorio-type-kit

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

Generate type definitions for the [Factorio Lua API](https://lua-api.factorio.com/latest/).

Currently supports typescript and a JSON [IR](<https://en.wikipedia.org/wiki/Intermediate_representation#:~:text=An%20intermediate%20representation%20(IR)%20is,such%20as%20optimization%20and%20translation.>).

## Usage

`factorio-type-kit` can be used in various ways, depending on your use case.

- **write mods**
- `npm install factorio-type-kit` or `yarn add factorio-type-kit`
- [see the guide for writing statically typed factorio mods](./docs/typescript-guide.md)
- **generate API definitions**
- as a library?
- install via the npm/yarn steps above. you can now `import * from "factorio-type-kit"` directly into your JS or TS application
- the API exports a few functions, namely `produce` & `printTs`, etc. Please see `src/bin.ts` to see them in self-documenting action :)
- as a cli?
- install [nodejs](https://nodejs.org/), which also installs `npx`
- run `npx factorio-type-kit --help` to use the CLI

```
$ npx factorio-type-kit --help
Usage
$ factorio-type-kit --language=[...]
Options
--language, -l ts,json
--output, -o path/to/filename/outupt.[ts|json]
Examples
$ factorio-type-kit -l ts -o factorio.schema.d.ts
```

## Motivation

### Problem

- factorio API is hard to explore, because it is both _wide_ and _deep_
- many classes/events to interact with
Expand All @@ -7,12 +46,12 @@
- lua is dynamically typed
- **objective** iterate less & have higher quality by applying typechecking on mod

# Solution
### Solution

`factorio-type-kit` is an essential part of the solution. FTK does or supports the following:

- scrape factorio API
- convert scraped API datas => [json-schema](https://json-schema.org/)
- convert json-schema to typed language target
- typescript
- haxe?
- convert scraped API datas into an [intermediate representation (IR)](<https://en.wikipedia.org/wiki/Intermediate_representation#:~:text=An%20intermediate%20representation%20(IR)%20is,such%20as%20optimization%20and%20translation.>)
- convert the IR to typed language target, or just JSON

## Usage
With your programming tools of choice, hopefully, you can now author factorio greatness with more power than Lua. Lua is a fine scripting language--but mods can be challenging without sufficient static analysis. Types can help!
68 changes: 58 additions & 10 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
import { produce } from "./generate";

produce({
urls: {
apiRoot: "https://lua-api.factorio.com/latest",
},
}).catch((err) => {
console.error(err);
process.exit(1);
});
import * as fs from "fs/promises";
import { produce, GenerateResult, printTs } from ".";
import meow from "meow";

const printLang = (lang: string, ir: GenerateResult) => {
switch (lang) {
case "ts":
case "typescript":
return printTs(ir);
case "json":
case "json-ir":
return JSON.stringify(ir, null, 2);
default:
throw new Error(`unsupported lang: ${lang}. did you mean ts or json?`);
}
};

async function go() {
const cli = meow(
`
Usage
$ factorio-type-kit --language=[...]
Options
--language, -l ts,json
--output, -o path/to/filename/outupt.[ts|json]
Examples
$ factorio-type-kit -l ts -o factorio.schema.d.ts
`,
{
flags: {
language: {
type: "string",
alias: "l",
},
out: {
type: "string",
alias: "o",
},
},
}
);
try {
const ir = await produce();
const output = await printLang(cli.flags.language!, ir);
if (cli.flags.out) {
await fs.writeFile(cli.flags.out, output);
} else {
console.log(output);
}
} catch (err) {
console.error(err);
process.exit(1);
}
}

go();
100 changes: 0 additions & 100 deletions src/factorio-meta/factorio-lua-json-schema.ts

This file was deleted.

0 comments on commit d504214

Please sign in to comment.