Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed Mar 3, 2022
0 parents commit 324eae6
Show file tree
Hide file tree
Showing 18 changed files with 1,218 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI

on: push

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/setup-node@v3
- run: yarn install
- run: make build
- run: yarn test
- name: release
if: github.ref == 'refs/heads/main'
run: npx --yes semantic-release --branches main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
main.wasm
wasm_exec.js
node_modules/
types/
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"gopls": {
"build.env": {
"GOOS": "js",
"GOARCH": "wasm"
}
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 XING Developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
build: wasm_exec.js main.wasm types/node.d.mts

wasm_exec.js:
cp $$(go env GOROOT)/misc/wasm/wasm_exec.js wasm_exec.tmp.js
echo "// @ts-nocheck" > wasm_exec.js
cat wasm_exec.tmp.js >> wasm_exec.js
rm wasm_exec.tmp.js

main.wasm: main.go go.mod
GOOS=js GOARCH=wasm go build -o main.wasm

types/node.d.mts: *.cjs *.mjs *.d.ts *.json yarn.lock
$$(yarn bin)/tsc -p .

clean:
rm main.wasm
rm wasm_exec.js
rm -rf types

.PHONY: build clean
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# actionlint
Actionlint as wasm
67 changes: 67 additions & 0 deletions actionlint.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require("./wasm_exec.js");

/**
* @typedef {(go: Go) => Promise<WebAssembly.WebAssemblyInstantiatedSource>} WasmLoader
* @typedef {(source: string, path: string) => Promise<LintResult[]>} RunActionlint
*
* @typedef {Object} LintResult
* @property {string} Message
* @property {string} Filepath
* @property {number} Line
* @property {number} Column
* @property {string} Kind
*/

/**
* @param {WasmLoader} loader
* @returns {RunActionlint}
*/
module.exports.createActionlint = function createActionlint(loader) {
const go = new Go();

/** @type {(() => void)[] | undefined} */
let queued = undefined;

// This function gets called from go once the wasm module is ready and it
// executes the linter for all queued calls.
globalThis.actionlintInitialized = () => {
queued?.forEach((f) => f());
queued = globalThis.actionlintInitialized = undefined;
};

loader(go).then((wasm) => {
// Do not await this promise, because it only resolves once the go main()
// function has exited. But we need the main function to stay alive to be
// able to call the `runActionlint` function.
go.run(wasm.instance);
});

/**
* @param {string} src
* @param {string} path
* @returns {Promise<LintResult[]>}
*/
return async function runLint(src, path) {
// Return a promise, because we need to queue calls to `runLint()` while the
// wasm module is still loading and execute them once the wasm module is
//ready.
return new Promise((resolve, reject) => {
if (typeof runActionlint === "function") {
const [result, err] = runActionlint(src, path);
return err ? reject(err) : resolve(result);
}

if (!queued) {
queued = [];
}

queued.push(() => {
const [result, err] = runActionlint?.(src, path) ?? [
[],
new Error('"runActionlint" is not defined'),
];
return err ? reject(err) : resolve(result);
});
});
};
};
29 changes: 29 additions & 0 deletions browser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createActionlint } from "./actionlint.cjs";

/**
* @typedef {import("./actionlint.cjs").LintResult} LintResult
* @typedef {import("./actionlint.cjs").WasmLoader} WasmLoader
* @typedef {import("./actionlint.cjs").RunActionlint} RunActionlint
*/

/** @type {RunActionlint | undefined} */
let runLint = undefined;

/**
* @param {URL} url
* @returns {RunActionlint}
*/
export function createLinter(url = new URL("./main.wasm", import.meta.url)) {
if (runLint) {
return runLint;
}

return (runLint = createActionlint(
/** @type {WasmLoader} */ async (go) => {
return WebAssembly.instantiateStreaming(
fetch(url.toString()),
go.importObject
);
}
));
}
6 changes: 6 additions & 0 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare global {
var runActionlint:
| ((src: string, path: string) => [LintResult[], Error | null])
| undefined;
var actionlintInitialized: (() => void) | undefined;
}
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module source.xing.com/fea/act-app

go 1.17

require (
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rhysd/actionlint v1.6.9 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rhysd/actionlint v1.6.9 h1:8rQQ76o88zctUCzukt0A5O/FO003wTGbkLQuwQkMf9c=
github.com/rhysd/actionlint v1.6.9/go.mod h1:0AA4pvZ2nrZHT6D86eUhieH2NFmLqhxrNex0NEa2A2g=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 h1:7NCfEGl0sfUojmX78nK9pBJuUlSZWEJA/TwASvfiPLo=
golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54 changes: 54 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"io/ioutil"
"reflect"
"syscall/js"

"github.com/rhysd/actionlint"
)

func toMap(input interface{}) map[string]interface{} {
out := make(map[string]interface{})
value := reflect.ValueOf(input)
if value.Kind() == reflect.Ptr {
value = value.Elem()
}

for i := 0; i < value.NumField(); i++ {
out[value.Type().Field(i).Name] = value.Field(i).Interface()
}

return out
}

func runActionlint(source string, filePath string) (interface{}, error) {
opts := actionlint.LinterOptions{}
linter, err := actionlint.NewLinter(ioutil.Discard, &opts)
if err != nil {
return nil, err
}

errs, err := linter.Lint(filePath, []byte(source), nil)
if err != nil {
return nil, err
}

ret := make([]interface{}, 0, len(errs))
for _, err := range errs {
ret = append(ret, toMap(*err))
}

return ret, nil
}

func main() {
js.Global().Set("runActionlint", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
result, err := runActionlint(args[0].String(), args[1].String())
return js.Global().Get("Array").New(result, err)
}))

js.Global().Call("actionlintInitialized")

select {}
}
31 changes: 31 additions & 0 deletions node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { join } = require("path");
const { pathToFileURL } = require("url");
const { readFile } = require("node:fs/promises");
const { createActionlint } = require("./actionlint.cjs");

/**
* @typedef {import("./actionlint.cjs").LintResult} LintResult
* @typedef {import("./actionlint.cjs").WasmLoader} WasmLoader
* @typedef {import("./actionlint.cjs").RunActionlint} RunActionlint
*/

/** @type {RunActionlint | undefined} */
let runLint = undefined;

/**
* @param {URL} url
* @returns {RunActionlint}
*/
module.exports.createLinter = function createLinter(
url = pathToFileURL(join(__dirname, "main.wasm"))
) {
if (runLint) {
return runLint;
}

return (runLint = createActionlint(
/** @type {WasmLoader} */ async (go) => {
return WebAssembly.instantiate(await readFile(url), go.importObject);
}
));
};
27 changes: 27 additions & 0 deletions node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { readFile } from "node:fs/promises";
import { createActionlint } from "./actionlint.cjs";

/**
* @typedef {import("./actionlint.cjs").LintResult} LintResult
* @typedef {import("./actionlint.cjs").WasmLoader} WasmLoader
* @typedef {import("./actionlint.cjs").RunActionlint} RunActionlint
*/

/** @type {RunActionlint | undefined} */
let runLint = undefined;

/**
* @param {URL} url
* @returns {RunActionlint}
*/
export function createLinter(url = new URL("./main.wasm", import.meta.url)) {
if (runLint) {
return runLint;
}

return (runLint = createActionlint(
/** @type {WasmLoader} */ async (go) => {
return WebAssembly.instantiate(await readFile(url), go.importObject);
}
));
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "actionlint",
"version": "0.0.0",
"license": "MIT",
"sideEffects": true,
"scripts": {
"test": "tape test.mjs | tap-spec"
},
"main": "./node.cjs",
"exports": {
"types": "./types/node.d.mts",
"node": {
"import": "./node.mjs",
"require": "./node.cjs"
},
"browser": "./browser.mjs"
},
"devDependencies": {
"@types/golang-wasm-exec": "^1.15.0",
"@types/node": "^17.0.21",
"@types/tape": "^4.13.2",
"tap-spec": "^5.0.0",
"tape": "^5.5.2",
"typescript": "^4.6.2"
}
}
Loading

0 comments on commit 324eae6

Please sign in to comment.