From eeb479e01ff1d0885536cac7a8ce4f25e020c04b Mon Sep 17 00:00:00 2001 From: yisibl Date: Fri, 8 Oct 2021 18:56:16 +0800 Subject: [PATCH] feat: v1.0.0, add test case --- .github/workflows/CI.yaml | 4 ++-- README.md | 11 +++++++---- __test__/index.spec.ts | 28 +++++++++++++++++++++++++++- example/index.js | 5 ++++- index.js | 4 ++-- npm/android-arm64/package.json | 4 ++-- npm/darwin-arm64/package.json | 4 ++-- npm/darwin-x64/package.json | 4 ++-- npm/freebsd-x64/package.json | 4 ++-- npm/linux-arm-gnueabihf/package.json | 4 ++-- npm/linux-arm64-gnu/package.json | 4 ++-- npm/linux-arm64-musl/package.json | 4 ++-- npm/linux-x64-gnu/package.json | 4 ++-- npm/linux-x64-musl/package.json | 4 ++-- npm/win32-arm64-msvc/package.json | 4 ++-- npm/win32-ia32-msvc/package.json | 4 ++-- npm/win32-x64-msvc/package.json | 4 ++-- package.json | 4 +++- 18 files changed, 69 insertions(+), 35 deletions(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index c6fd94cd..4b4da9c6 100755 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -2,7 +2,7 @@ name: CI env: DEBUG: 'napi:*' - APP_NAME: 'package-template' + APP_NAME: 'resvgjs' MACOSX_DEPLOYMENT_TARGET: '10.13' on: @@ -76,7 +76,7 @@ jobs: docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL docker pull ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine docker tag ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine builder - build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/package-template -w /package-template builder sh -c "yarn build -- --target=aarch64-unknown-linux-musl" + build: docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/resvgjs -w /resvgjs builder sh -c "yarn build -- --target=aarch64-unknown-linux-musl" - host: windows-latest target: 'aarch64-pc-windows-msvc' build: yarn build --target aarch64-pc-windows-msvc diff --git a/README.md b/README.md index 46d1fe21..03f25c00 100755 --- a/README.md +++ b/README.md @@ -26,9 +26,10 @@ Font './example/SourceHanSerifCN-Light-subset.ttf':0 found in 0.006ms. ✨ Done in 55.65491008758545 ms ``` -| SVG | PNG | -| ------------------------------------------------------- | ------------------------------------------------------- | -| | | +| SVG | PNG | +| ---------------------------------------- | -------------------------------------------- | +| | | + ## Support matrix ### Operating Systems @@ -52,7 +53,9 @@ Font './example/SourceHanSerifCN-Light-subset.ttf':0 found in 0.006ms. ### Build -After `npm run build` command, you can see `package-template.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs). +You can set the name of the generated `.node` file in `napi.name` of package.json. + +After `npm run build` command, you can see `resvgjs.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs). ### Test diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 90db015e..af9c389d 100755 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -1,3 +1,29 @@ +import { promises } from 'fs' +import { join } from 'path' + import test from 'ava' +import probeImageSize from 'probe-image-size' + +import { render } from '../index' + +test('draw-svg', async (t) => { + const filePath = '../example/text.svg' + const svg = await promises.readFile(join(__dirname, filePath)) + const svgString = svg.toString('utf-8') + const pngData = render(svgString, { + background: '#eeebe6', + fitTo: { + mode: 'width', + value: 1200, + }, + font: { + fontFiles: ['./example/SourceHanSerifCN-Light-subset.ttf'], // Load custom fonts. + loadSystemFonts: false, // It will be faster to disable loading system fonts. + defaultFontFamily: 'Source Han Serif CN Light', + }, + }) + const result = probeImageSize.sync(pngData) -// TODO + t.is(result.width, 1200) + t.is(result.height, 623) +}) diff --git a/example/index.js b/example/index.js index 77e78928..02554853 100644 --- a/example/index.js +++ b/example/index.js @@ -1,8 +1,9 @@ -const { render } = require('../index') const { promises } = require('fs') const { join } = require('path') const { performance } = require('perf_hooks') +const { render } = require('../index') + async function main() { const svg = await promises.readFile(join(__dirname, './text.svg')) const svgString = svg.toString('utf-8') @@ -20,7 +21,9 @@ async function main() { }, }) const t1 = performance.now() + // eslint-disable-next-line no-console console.log('✨ Done in', t1 - t0, 'ms') + await promises.writeFile(join(__dirname, './text-out.png'), pngData) } diff --git a/index.js b/index.js index 51c4b46e..1c252481 100755 --- a/index.js +++ b/index.js @@ -6,6 +6,6 @@ const { loadBinding } = require('@node-rs/helper') * the first arguments was decided by `napi.name` field in `package.json` * the second arguments was decided by `name` field in `package.json` * loadBinding helper will load `resvg.[PLATFORM].node` from `__dirname` first - * If failed to load addon, it will fallback to load from `@resvg/resvg-[PLATFORM]` + * If failed to load addon, it will fallback to load from `@resvg/resvgjs-[PLATFORM]` */ -module.exports = loadBinding(__dirname, 'resvg', '@resvg/resvg-js') +module.exports = loadBinding(__dirname, 'resvgjs', '@resvg/resvg-js') diff --git a/npm/android-arm64/package.json b/npm/android-arm64/package.json index 6f7917b0..30cb150f 100644 --- a/npm/android-arm64/package.json +++ b/npm/android-arm64/package.json @@ -7,9 +7,9 @@ "cpu": [ "arm64" ], - "main": "resvg.android-arm64.node", + "main": "resvgjs.android-arm64.node", "files": [ - "resvg.android-arm64.node" + "resvgjs.android-arm64.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/darwin-arm64/package.json b/npm/darwin-arm64/package.json index b53e6859..36aa99f3 100644 --- a/npm/darwin-arm64/package.json +++ b/npm/darwin-arm64/package.json @@ -7,9 +7,9 @@ "cpu": [ "arm64" ], - "main": "resvg.darwin-arm64.node", + "main": "resvgjs.darwin-arm64.node", "files": [ - "resvg.darwin-arm64.node" + "resvgjs.darwin-arm64.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/darwin-x64/package.json b/npm/darwin-x64/package.json index 483d3512..65a68b5d 100644 --- a/npm/darwin-x64/package.json +++ b/npm/darwin-x64/package.json @@ -7,9 +7,9 @@ "cpu": [ "x64" ], - "main": "resvg.darwin-x64.node", + "main": "resvgjs.darwin-x64.node", "files": [ - "resvg.darwin-x64.node" + "resvgjs.darwin-x64.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/freebsd-x64/package.json b/npm/freebsd-x64/package.json index 7409d6fd..e2feeec5 100644 --- a/npm/freebsd-x64/package.json +++ b/npm/freebsd-x64/package.json @@ -7,9 +7,9 @@ "cpu": [ "x64" ], - "main": "resvg.freebsd-x64.node", + "main": "resvgjs.freebsd-x64.node", "files": [ - "resvg.freebsd-x64.node" + "resvgjs.freebsd-x64.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/linux-arm-gnueabihf/package.json b/npm/linux-arm-gnueabihf/package.json index 3c26d2c6..82ee69cb 100644 --- a/npm/linux-arm-gnueabihf/package.json +++ b/npm/linux-arm-gnueabihf/package.json @@ -7,9 +7,9 @@ "cpu": [ "arm" ], - "main": "resvg.linux-arm-gnueabihf.node", + "main": "resvgjs.linux-arm-gnueabihf.node", "files": [ - "resvg.linux-arm-gnueabihf.node" + "resvgjs.linux-arm-gnueabihf.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/linux-arm64-gnu/package.json b/npm/linux-arm64-gnu/package.json index 7c8219b1..8080a6fe 100644 --- a/npm/linux-arm64-gnu/package.json +++ b/npm/linux-arm64-gnu/package.json @@ -7,9 +7,9 @@ "cpu": [ "arm64" ], - "main": "resvg.linux-arm64-gnu.node", + "main": "resvgjs.linux-arm64-gnu.node", "files": [ - "resvg.linux-arm64-gnu.node" + "resvgjs.linux-arm64-gnu.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/linux-arm64-musl/package.json b/npm/linux-arm64-musl/package.json index dd691ebd..3214f1ea 100644 --- a/npm/linux-arm64-musl/package.json +++ b/npm/linux-arm64-musl/package.json @@ -7,9 +7,9 @@ "cpu": [ "arm64" ], - "main": "resvg.linux-arm64-musl.node", + "main": "resvgjs.linux-arm64-musl.node", "files": [ - "resvg.linux-arm64-musl.node" + "resvgjs.linux-arm64-musl.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/linux-x64-gnu/package.json b/npm/linux-x64-gnu/package.json index 373847ef..97b7093d 100644 --- a/npm/linux-x64-gnu/package.json +++ b/npm/linux-x64-gnu/package.json @@ -7,9 +7,9 @@ "cpu": [ "x64" ], - "main": "resvg.linux-x64-gnu.node", + "main": "resvgjs.linux-x64-gnu.node", "files": [ - "resvg.linux-x64-gnu.node" + "resvgjs.linux-x64-gnu.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/linux-x64-musl/package.json b/npm/linux-x64-musl/package.json index aa581835..db4fa133 100644 --- a/npm/linux-x64-musl/package.json +++ b/npm/linux-x64-musl/package.json @@ -7,9 +7,9 @@ "cpu": [ "x64" ], - "main": "resvg.linux-x64-musl.node", + "main": "resvgjs.linux-x64-musl.node", "files": [ - "resvg.linux-x64-musl.node" + "resvgjs.linux-x64-musl.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/win32-arm64-msvc/package.json b/npm/win32-arm64-msvc/package.json index 2add8247..10254599 100644 --- a/npm/win32-arm64-msvc/package.json +++ b/npm/win32-arm64-msvc/package.json @@ -7,9 +7,9 @@ "cpu": [ "arm64" ], - "main": "resvg.win32-arm64-msvc.node", + "main": "resvgjs.win32-arm64-msvc.node", "files": [ - "resvg.win32-arm64-msvc.node" + "resvgjs.win32-arm64-msvc.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/win32-ia32-msvc/package.json b/npm/win32-ia32-msvc/package.json index 3e4a58ab..222487d5 100644 --- a/npm/win32-ia32-msvc/package.json +++ b/npm/win32-ia32-msvc/package.json @@ -7,9 +7,9 @@ "cpu": [ "ia32" ], - "main": "resvg.win32-ia32-msvc.node", + "main": "resvgjs.win32-ia32-msvc.node", "files": [ - "resvg.win32-ia32-msvc.node" + "resvgjs.win32-ia32-msvc.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/npm/win32-x64-msvc/package.json b/npm/win32-x64-msvc/package.json index ce227c7a..35819d88 100644 --- a/npm/win32-x64-msvc/package.json +++ b/npm/win32-x64-msvc/package.json @@ -7,9 +7,9 @@ "cpu": [ "x64" ], - "main": "resvg.win32-x64-msvc.node", + "main": "resvgjs.win32-x64-msvc.node", "files": [ - "resvg.win32-x64-msvc.node" + "resvgjs.win32-x64-msvc.node" ], "description": "A high-performance SVG renderer, powered by Rust based resvg and napi-rs", "keywords": [ diff --git a/package.json b/package.json index 1f2a6622..c076c306 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "index.js" ], "napi": { - "name": "resvg", + "name": "resvgjs", "triples": { "defaults": true, "additional": [ @@ -53,6 +53,7 @@ "format:source": "prettier --config ./package.json --write './**/*.{js,ts}'", "format:yaml": "prettier --parser yaml --write './**/*.{yml,yaml}'", "lint": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}'", + "lint:fix": "eslint . -c ./.eslintrc.yml './**/*.{ts,tsx,js}' --fix", "prepublishOnly": "napi prepublish -t npm", "test": "ava", "version": "napi version" @@ -74,6 +75,7 @@ "lint-staged": "^11.2.0", "npm-run-all": "^4.1.5", "prettier": "^2.4.1", + "probe-image-size": "^7.2.1", "typescript": "^4.4.3" }, "dependencies": {