Skip to content

Commit

Permalink
fix(program): 完善异常捕获
Browse files Browse the repository at this point in the history
  • Loading branch information
aliothor committed Oct 16, 2023
1 parent 8e61413 commit 1ae8e83
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 13 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ permissions:
contents: write
pull-requests: write


jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
with:
release-type: node
package-name: mtk-tool
package-name:
mtk-tool
# The logic below handles the npm publication:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
# these if statements ensure that a publication only occurs when
# a new release is created:
if: ${{ steps.release.outputs.release_created }}
- uses: actions/setup-node@v1
- uses: actions/setup-node@v3
with:
node-version: 20
registry-url: 'https://registry.npmjs.org'
if: ${{ steps.release.outputs.release_created }}
- run: npm ci
- run: |
npm install
npm run build
if: ${{ steps.release.outputs.release_created }}
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
if: ${{ steps.release.outputs.release_created }}
if: ${{ steps.release.outputs.release_created }}
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { program } from 'commander'
import { runCommand } from './util'

program.name('mtk-tool').description('用与开发maptalks应用或插件的cli工具').version('0.0.1')
program.name('mtk-tool').description('用与开发maptalks应用或插件的cli工具').version('1.0.0')

program
.command('compile')
Expand All @@ -10,13 +10,18 @@ program
.option('-o,--output <string>', '编译后的worker文件目录', 'mtk-worker')
.action(runCompile)

async function runCompile(options:any) {
/**
* 开始编译
* @param options
*/
async function runCompile(options: any) {
try {
await runCommand(options.input, options.output)
// console.log(options);
} catch (error) {
console.error(error)
}
}

program.parseAsync(process.argv)
program.parseAsync(process.argv).catch(error => {
console.error(error);
})
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { join, basename } from 'node:path'
import { readdir, mkdir, stat, readFile, writeFile, rm } from 'node:fs/promises'
import { readdir, mkdir, stat, readFile, writeFile } from 'node:fs/promises'

import { nodeResolve } from '@rollup/plugin-node-resolve'
import swc from '@rollup/plugin-swc'
import commonjs from '@rollup/plugin-commonjs'
import { rollup, OutputOptions, RollupBuild } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'

const currentPath = process.cwd()
const testInputDir = 'test/worker-source'
const testOutputDir = 'test/mtk-worker'

/**
* 运行命令
* 执行编译命令
* @param inputDir
* @param outputDir
* @returns
Expand Down
187 changes: 187 additions & 0 deletions test/worker-compiled/imagefilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
export default function (exports) {

let canvas$1;
const OPTIONS = {
width: 100,
height: 10
};
// const ISNODE = typeof global === 'object';
let offscreenCanvas = false;
try {
const canvas = new OffscreenCanvas(1, 1);
const ctx = canvas.getContext('2d');
ctx.fillText('hello', 0, 0);
offscreenCanvas = true;
} catch (err) {
offscreenCanvas = false;
}
function getCanvas() {
if (!canvas$1) {
const { width, height } = OPTIONS;
if (offscreenCanvas) {
canvas$1 = new OffscreenCanvas(width, height);
} else {
canvas$1 = document.createElement('canvas');
canvas$1.width = width;
canvas$1.height = height;
}
}
return canvas$1;
}
class ColorIn {
constructor(colors, options = {}){
if (!Array.isArray(colors)) {
console.error('colors is not array');
return;
}
if (colors.length < 2) {
console.error('colors.length should >1');
return;
}
this.colors = colors;
let min = Infinity, max = -Infinity;
for(let i = 0, len = colors.length; i < len; i++){
const value = colors[i][0];
min = Math.min(value, min);
max = Math.max(value, max);
}
this.min = min;
this.max = max;
this.valueOffset = this.max - this.min;
this.options = Object.assign({}, OPTIONS, options);
this._initImgData();
}
getImageData() {
return this.imgData;
}
_initImgData() {
const canvas = getCanvas();
const { width, height } = this.options;
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
const { colors, valueOffset } = this;
for(let i = 0, len = colors.length; i < len; i++){
const [stop, color] = colors[i];
const s = (stop - this.min) / valueOffset;
gradient.addColorStop(s, color);
}
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
this.imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
}
getColor(stop) {
stop = Math.max(this.min, stop);
stop = Math.min(stop, this.max);
const s = (stop - this.min) / this.valueOffset;
let x = Math.round(s * this.imgData.width);
x = Math.min(x, this.imgData.width - 1);
const idx = x * 4;
const r = this.imgData.data[idx];
const g = this.imgData.data[idx + 1];
const b = this.imgData.data[idx + 2];
const a = this.imgData.data[idx + 3];
return [
r,
g,
b,
a
];
}
}

const colors = [
[
0,
'#226412'
],
[
1000,
'#B9E287'
],
[
2000,
'#E7F5D1'
],
[
3000,
'#F7F7F7'
],
[
3500,
'#80BD3F'
],
[
4000,
'#226412'
],
[
4500,
'#4C931B'
],
[
5000,
'#80BD3F'
],
[
6000,
'#F1B7DB'
],
[
7000,
'#DF78AF'
],
[
8000,
'#C6147E'
],
[
9000,
'#8F0051'
]
];
const ci = new ColorIn(colors);
const canvas = new OffscreenCanvas(1, 1);
const TILESIZE = 256;
const initialize = function() {
console.log('tileimagebitmap init');
};
const onmessage = function(msg, postResponse) {
ci.getColor(45);
const url = msg.data.url;
canvas.width = TILESIZE;
canvas.height = TILESIZE;
const ctx = canvas.getContext('2d');
// const ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, canvas.width, canvas.height);
//fetch image
fetch(url).then((res)=>res.arrayBuffer()).then((arrayBuffer)=>{
const blob = new Blob([
arrayBuffer
]);
createImageBitmap(blob).then((bitmap)=>{
ctx.filter = 'sepia(100%) invert(90%)';
ctx.drawImage(bitmap, 0, 0);
const image = canvas.transferToImageBitmap();
postResponse(null, {
image
}, [
image
]);
});
}).catch((error)=>{
const image = canvas.transferToImageBitmap();
postResponse(null, {
image
}, [
image
]);
});
};

exports.initialize = initialize;
exports.onmessage = onmessage;

}

0 comments on commit 1ae8e83

Please sign in to comment.