Skip to content

Commit

Permalink
feat: try add ffi api for nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Feb 11, 2022
1 parent ce6ca39 commit 8b7cc52
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/whatchanged-ffi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### macOS

```bash
go build -buildmode=c-shared -o whatchanged.dylib github.com/release-lab/whatchanged/cmd/whatchanged-ffi
```
47 changes: 47 additions & 0 deletions cmd/whatchanged-ffi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// filename: whatchanged.so
// filename: whatchanged.a
// filename: whatchanged.dll
// filename: whatchanged.dylib
package main

import "C"

import (
"bytes"
"context"
"encoding/json"
"fmt"
"regexp"

"github.com/release-lab/whatchanged"
)

//export Generate
func Generate(repo *C.char, version *C.char) *C.char {
type Result struct {
Error string `json:"error"`
Result string `json:"result"`
}

var result Result

output := bytes.NewBuffer([]byte{})

err := whatchanged.Generate(context.Background(), C.GoString(repo), output, &whatchanged.Options{
Version: regexp.MustCompile(`\s+`).Split(C.GoString(version), -1),
Branch: "master",
Preset: whatchanged.EnumPreset("full"),
})

if err != nil {
result.Error = fmt.Sprintf("%+v\n", err)
} else {
result.Result = output.String()
}

b, _ := json.Marshal(result)

return C.CString(string(b))
}

func main() {}
31 changes: 31 additions & 0 deletions npm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const ffi = require("ffi-napi");
const path = require("path");

const lib = ffi.Library(path.join(__dirname, "lib", "whatchanged"), {
Generate: ["string", ["string", "string"]],
});

/**
* Generate changelog
* @param {string} repo
* @param {string} version
* @returns {Promise<string>}
*/
function generate(repo, version) {
return new Promise((resolve, reject) => {
lib.Generate.async(repo, version, function (err, res) {
if (err) {
return reject(err);
}

const { result: changelog, error } = JSON.parse(res);
if (error) {
return reject(new Error(error));
}

resolve(changelog)
});
});
}

module.exports.generate = generate;
Empty file added npm/lib/.gitkeep
Empty file.
14 changes: 14 additions & 0 deletions npm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@axetroy/whatchanged",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"ffi-napi": "^4.0.3"
}
}
9 changes: 9 additions & 0 deletions npm/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { generate } = require("./index");

generate(path.join(__dirname, ".."), "HEAD~")
.then((res) => {
console.log(res);
})
.catch((err) => {
console.error(err);
});

0 comments on commit 8b7cc52

Please sign in to comment.