Skip to content

Commit

Permalink
Merge pull request #67 from wangfenjin/node
Browse files Browse the repository at this point in the history
add node example
  • Loading branch information
wangfenjin authored Oct 16, 2021
2 parents c04dba8 + fc05be7 commit 3cd42fc
Show file tree
Hide file tree
Showing 7 changed files with 5,899 additions and 9 deletions.
50 changes: 41 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
include:
- arch: x86
- arch: ia32
- arch: x64

steps:
Expand Down Expand Up @@ -80,6 +80,17 @@ jobs:
cmakeAppendedArgs: -A ${{steps.build_type.outputs.BUILD_ARCH }}
buildWithCMakeArgs: --config ${{ steps.build_type.outputs.BUILD_TYPE }}

# can't build
# npm run
# - uses: actions/setup-node@v2
# with:
# node-version: '16'
# - name: run node example
# working-directory: ./examples/node/
# run: |
# npm install
# npm run p

# - name: 'Run CTest'
# run: ctest -C ${{ env.BUILD_TYPE }}
# working-directory: "${{ github.workspace }}/../../_temp/windows"
Expand Down Expand Up @@ -171,6 +182,16 @@ jobs:
if: startsWith(github.ref, 'refs/tags/') != true
run: bash <(curl -s https://codecov.io/bash) -f coverage_filter.info || echo "Codecov did not collect coverage reports"

# npm run
- uses: actions/setup-node@v2
with:
node-version: '16'
- name: run node example
working-directory: ./examples/node/
run: |
npm install
npm run p
# create release
- name: "Build Changelog"
id: build_changelog
Expand All @@ -186,16 +207,16 @@ jobs:
- name: Package
if: startsWith(github.ref, 'refs/tags/')
run: |
mkdir libsimple-linux-amd64
cp -r src/libsimple.so test/dict/ libsimple-linux-amd64/
zip -r libsimple-linux-amd64.zip libsimple-linux-amd64
mkdir libsimple-linux-x64
cp -r src/libsimple.so test/dict/ libsimple-linux-x64/
zip -r libsimple-linux-x64.zip libsimple-linux-x64
working-directory: "${{ github.workspace }}/build"
- name: Release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v1
with:
draft: true
files: ${{ github.workspace }}/build/libsimple-linux-amd64.zip
files: ${{ github.workspace }}/build/libsimple-linux-x64.zip
body: ${{steps.build_changelog.outputs.changelog}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -227,6 +248,17 @@ jobs:
run: ctest -C ${{ env.BUILD_TYPE }}
working-directory: "${{ github.workspace }}/../../_temp/macos"

# TODO: disable for now, need to move jieba_dict to dict path, same as other platform
# npm run
# - uses: actions/setup-node@v2
# with:
# node-version: '16'
# - name: run node example
# working-directory: ./examples/node/
# run: |
# npm install
# npm run p

- name: "Check file existence"
uses: andstor/file-existence-action@v1
with:
Expand All @@ -235,15 +267,15 @@ jobs:
- name: Package
if: startsWith(github.ref, 'refs/tags/')
run: |
mkdir libsimple-osx-amd64
cp -r src/libsimple.dylib test/dict/ libsimple-osx-amd64/
zip -r libsimple-osx-amd64.zip libsimple-osx-amd64
mkdir libsimple-osx-x64
cp -r src/libsimple.dylib test/dict libsimple-osx-x64/
zip -r libsimple-osx-x64.zip libsimple-osx-x64
working-directory: "${{ github.workspace }}/../../_temp/macos"
- name: Release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v1
with:
draft: true
files: ${{ github.workspace }}/../../_temp/macos/libsimple-osx-amd64.zip
files: ${{ github.workspace }}/../../_temp/macos/libsimple-osx-x64.zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ bin/
output/
output-no-jieba/
chat.db
examples/node/node_modules/
examples/node/lib/

# CLion
.idea/
Expand Down
10 changes: 10 additions & 0 deletions examples/node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# node example

```
# download lib from github
npm install
# run example
npm run p
# remove build folder
npm run clean
```
31 changes: 31 additions & 0 deletions examples/node/just-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { task, logger } = require('just-scripts')
const download = require('download')
const path = require('path')

task('install', () => {
return new Promise((resolve, reject) => {
const localPath = path.join(__dirname, 'lib')
var platform = process.env.npm_config_target_platform || process.platform
logger.info(`[install] Target platform: ${platform}`)
if (platform === 'darwin') {
platform = 'osx';
} else if (platform === 'win32') {
platform = 'windows';
}
var arch = process.env.npm_config_target_arch || process.arch
logger.info(`[install] Target arch: ${arch}`)
if (platform !== 'windows' && arch === 'x64') {
arch = 'amd64';
}
const downloadUrl = `https://github.com/wangfenjin/simple/releases/download/v0.0.4/libsimple-${platform}-${arch}.zip`
logger.info(`[install] Download prebuilt binaries from ${downloadUrl}`)
download(downloadUrl, localPath, {
extract: true, strip: 1
}).then(() => {
resolve()
}).catch(err => {
logger.warn(`[install] Failed to download package from: ${downloadUrl}, err: ${err}`)
reject()
})
})
})
36 changes: 36 additions & 0 deletions examples/node/node-sqlite3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var path = require("path");
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');

db.serialize(function() {
const extension_path = path.resolve("./lib/");
console.log("extension path: " + extension_path);
// load extension
var platform = process.env.npm_config_target_platform || process.platform
if (platform === 'win32') {
db.loadExtension(path.join(extension_path, "simple"));
} else {
db.loadExtension(path.join(extension_path, "libsimple"));
}
// set the jieba dict file path
db.run("select jieba_dict(?)", path.join(extension_path, "dict"));
// create table
db.run("CREATE VIRTUAL TABLE t1 USING fts5(x, tokenize = 'simple')");
// insert some data
db.run("insert into t1(x) values ('周杰伦 Jay Chou:我已分不清,你是友情还是错过的爱情'), ('周杰伦 Jay Chou:最美的不是下雨天,是曾与你躲过雨的屋檐'), ('I love China! 我爱中国!我是中华人民共和国公民!'), ('@English &special _characters.\"''bacon-&and''-eggs%')");

// with match 周杰伦
db.each("select rowid as id, simple_highlight(t1, 0, '[', ']') as info from t1 where x match simple_query('zjl')", function(err, row) {
console.log(row.id + ": " + row.info);
});
// will match 中国 and 中华人民共和国
db.each("select rowid as id, simple_highlight(t1, 0, '[', ']') as info from t1 where x match simple_query('中国')", function(err, row) {
console.log(row.id + ": " + row.info);
});
// will match 中国 but not 中华人民共和国
db.each("select rowid as id, simple_highlight(t1, 0, '[', ']') as info from t1 where x match jieba_query('中国')", function(err, row) {
console.log(row.id + ": " + row.info);
});
});

db.close();
Loading

0 comments on commit 3cd42fc

Please sign in to comment.