Skip to content

Commit

Permalink
Merge pull request #5 from rubickCenter/feature/new
Browse files Browse the repository at this point in the history
♻️ 修改 simulation 实现方式,添加单测.
  • Loading branch information
SOVLOOKUP authored Oct 24, 2023
2 parents 4e50f33 + ee5c6f9 commit 448f813
Show file tree
Hide file tree
Showing 15 changed files with 2,621 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

### Node ###
# Logs
.idea
logs
*.log
npm-debug.log*
Expand Down
7 changes: 7 additions & 0 deletions __test__/clipboard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import test from 'ava';
import { getClipboardContent, ClipboardContent } from '../src'

test('get clipboard content', (t) => {
const result: ClipboardContent = getClipboardContent()
t.true((result.type === 'text' && typeof result.content === 'string') || (result.type === 'file' && typeof result.content === 'object') || result.type === null)
});
21 changes: 21 additions & 0 deletions __test__/keyboard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava';
import { keyboard, Key, mouse, left, right, up, down } from '../src'

test('simulateKeyboardTap one key', (t) => {
t.notThrows(async () => await keyboard.type('a'))
});

test('simulateKeyboardTap multi key', (t) => {
t.notThrows(async () => await keyboard.type('calculator'))
});

test('simulateKeyboardTap key with modifier', (t) => {
t.notThrows(async () => await keyboard.type(Key.LeftControl, Key.LeftSuper, Key.A))
});

test('mouse move', (t) => {
t.notThrows(async () => await mouse.move(left(100)))
t.notThrows(async () => await mouse.move(up(50)))
t.notThrows(async () => await mouse.move(right(100)))
t.notThrows(async () => await mouse.move(down(50)))
});
28 changes: 14 additions & 14 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.android-arm64.node')
} else {
nativeBinding = require('rubick-native-addon-android-arm64')
nativeBinding = require('@node-rs/rubick-native-addon-android-arm64')
}
} catch (e) {
loadError = e
Expand All @@ -49,7 +49,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.android-arm-eabi.node')
} else {
nativeBinding = require('rubick-native-addon-android-arm-eabi')
nativeBinding = require('@node-rs/rubick-native-addon-android-arm-eabi')
}
} catch (e) {
loadError = e
Expand All @@ -69,7 +69,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.win32-x64-msvc.node')
} else {
nativeBinding = require('rubick-native-addon-win32-x64-msvc')
nativeBinding = require('@node-rs/rubick-native-addon-win32-x64-msvc')
}
} catch (e) {
loadError = e
Expand All @@ -83,7 +83,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.win32-ia32-msvc.node')
} else {
nativeBinding = require('rubick-native-addon-win32-ia32-msvc')
nativeBinding = require('@node-rs/rubick-native-addon-win32-ia32-msvc')
}
} catch (e) {
loadError = e
Expand All @@ -97,7 +97,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.win32-arm64-msvc.node')
} else {
nativeBinding = require('rubick-native-addon-win32-arm64-msvc')
nativeBinding = require('@node-rs/rubick-native-addon-win32-arm64-msvc')
}
} catch (e) {
loadError = e
Expand All @@ -113,7 +113,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.darwin-universal.node')
} else {
nativeBinding = require('rubick-native-addon-darwin-universal')
nativeBinding = require('@node-rs/rubick-native-addon-darwin-universal')
}
break
} catch {}
Expand All @@ -124,7 +124,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.darwin-x64.node')
} else {
nativeBinding = require('rubick-native-addon-darwin-x64')
nativeBinding = require('@node-rs/rubick-native-addon-darwin-x64')
}
} catch (e) {
loadError = e
Expand All @@ -138,7 +138,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.darwin-arm64.node')
} else {
nativeBinding = require('rubick-native-addon-darwin-arm64')
nativeBinding = require('@node-rs/rubick-native-addon-darwin-arm64')
}
} catch (e) {
loadError = e
Expand All @@ -157,7 +157,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.freebsd-x64.node')
} else {
nativeBinding = require('rubick-native-addon-freebsd-x64')
nativeBinding = require('@node-rs/rubick-native-addon-freebsd-x64')
}
} catch (e) {
loadError = e
Expand All @@ -174,7 +174,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.linux-x64-musl.node')
} else {
nativeBinding = require('rubick-native-addon-linux-x64-musl')
nativeBinding = require('@node-rs/rubick-native-addon-linux-x64-musl')
}
} catch (e) {
loadError = e
Expand All @@ -187,7 +187,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.linux-x64-gnu.node')
} else {
nativeBinding = require('rubick-native-addon-linux-x64-gnu')
nativeBinding = require('@node-rs/rubick-native-addon-linux-x64-gnu')
}
} catch (e) {
loadError = e
Expand All @@ -203,7 +203,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.linux-arm64-musl.node')
} else {
nativeBinding = require('rubick-native-addon-linux-arm64-musl')
nativeBinding = require('@node-rs/rubick-native-addon-linux-arm64-musl')
}
} catch (e) {
loadError = e
Expand All @@ -216,7 +216,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.linux-arm64-gnu.node')
} else {
nativeBinding = require('rubick-native-addon-linux-arm64-gnu')
nativeBinding = require('@node-rs/rubick-native-addon-linux-arm64-gnu')
}
} catch (e) {
loadError = e
Expand All @@ -231,7 +231,7 @@ switch (platform) {
if (localFileExisted) {
nativeBinding = require('./rubick-native-addon.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('rubick-native-addon-linux-arm-gnueabihf')
nativeBinding = require('@node-rs/rubick-native-addon-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
Expand Down
2 changes: 1 addition & 1 deletion addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"artifacts": "napi artifacts",
"build": "napi build --platform --release",
"build:debug": "napi build --platform",
"build:debug": "CARGO_HTTP_MULTIPLEXING=false napi build --platform",
"prepublishOnly": "node ./prepub.js && napi prepublish -t npm",
"universal": "napi universal",
"version": "napi version"
Expand Down
8 changes: 8 additions & 0 deletions addon/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@napi-rs/cli@^2.16.3":
version "2.16.3"
resolved "https://registry.npmmirror.com/@napi-rs/cli/-/cli-2.16.3.tgz#6943d0614bbb4900a97b18ae074f735864f78c4b"
integrity sha512-3mLNPlbbOhpbIUKicLrJtIearlHXUuXL3UeueYyRRplpVMNkdn8xCyzY6PcYZi3JXR8bmCOiWgkVmLnrSL7DKw==
10 changes: 10 additions & 0 deletions ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
extensions: ['ts'],
workerThreads: false,
require: ['@swc-node/register'],
files: ['__test__/**/*.spec.ts'],
timeout: '3m',
environmentVariables: {
TS_NODE_PROJECT: './tsconfig.test.json',
},
}
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
{
"name": "rubick-native",
"version": "0.0.15-beta",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup --cjsInterop"
"build": "tsup --cjsInterop",
"test": "ava",
"dev": "tsup --cjsInterop --watch"
},
"devDependencies": {
"@swc-node/register": "^1.6.8",
"@swc/core": "^1.3.94",
"@types/node": "^20.8.7",
"picomatch": "^2.3.1",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
},
"dependencies": {
"@miniben90/x-win": "^1.3.1",
"@nut-tree/nut-js": "^3.1.2",
"ava": "^5.3.1",
"execa": "^8.0.1",
"fdir": "^6.1.0"
},
Expand All @@ -37,4 +42,4 @@
"treeshake": true,
"shims": true
}
}
}
2 changes: 1 addition & 1 deletion src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getClipboardContent as gcbc } from "../addon"

interface ClipboardContentText {
type: "text",
content: string
content: string | undefined
}

interface ClipboardContentFile {
Expand Down
2 changes: 1 addition & 1 deletion src/folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { join } from "path"
// 获取活动的文件夹路径
export const getFolderOpenPath = async () => {
if (process.platform === 'darwin') {
const { execa } = await import("execa")
const { execa } = require("execa")
const res = await execa('osascript', ['-e', `
tell app "Finder"
try
Expand Down
64 changes: 4 additions & 60 deletions src/simulation.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,6 @@
import {
Position,
sendKeyboardSimulation as ks,
sendMouseSimulation as ms,
} from "../addon";
import { keyboard, Key, mouse, left, right, up, down, straightTo } from "@nut-tree/nut-js";

export type MouseBtn =
| "Left"
| "Middle"
| "Right"
| "Back"
| "Forward"
keyboard.config.autoDelayMs = 10;
mouse.config.autoDelayMs = 10;

export interface MoveMoveInput {
type: "relative" | "absolute";
data: Position;
}

// example: {+CTRL}a{-CTRL}{+SHIFT}Hello World{-SHIFT}
// 所有可用键 https://github.com/enigo-rs/enigo/blob/master/src/keycodes.rs
export const sendKeyboardSimulation = (cmd: string) => ks(cmd);

export const mouseScrollX = (len: number) => {
ms({ action: 3, data: { x: len, y: 0 } });
}

export const mouseScrollY = (len: number) => {
ms({ action: 4, data: { x: 0, y: len } });
};

export const mouseMove = (input: MoveMoveInput) => {
ms({ action: input.type === "absolute" ? 2 : 1, data: input.data });
};

export const mouseLocaion = () => ms({ action: 0 })

const mouseDUC = (btn: MouseBtn, action: 5 | 6 | 7) => {
let button = 0;
switch (btn) {
case "Left":
break;
case "Middle":
button = 1;
break;
case "Right":
button = 2;
break;
case "Back":
button = 3;
break;
case "Forward":
button = 4;
break;
default:
break;
}
ms({ action, button });
};

export const mouseDown = (btn: MouseBtn) => mouseDUC(btn, 6)
export const mouseUp = (btn: MouseBtn) => mouseDUC(btn, 5)
export const mouseClick = (btn: MouseBtn) => mouseDUC(btn, 7)
export { keyboard, mouse, Key, left, right, up, down, straightTo}
10 changes: 0 additions & 10 deletions test.ts

This file was deleted.

38 changes: 28 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
{
"compilerOptions": {
"outDir": "dist",
"allowSyntheticDefaultImports": true,
"declaration": true,
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": false,
"module": "CommonJS",
"moduleResolution": "node",
"newLine": "LF",
"noEmitHelpers": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"target": "ES2018",
"inlineSourceMap": true,
"esModuleInterop": true,
"module": "ESNext",
"moduleResolution": "Node",
"target": "ESNext",
"lib": [
"ESNext",
"DOM"
]
"stripInternal": true,
"importsNotUsedAsValues": "remove",
"paths": {
"@node-rs/*": ["./addon"]
},
"outDir": "dist",
"lib": ["dom", "ES5", "ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "esnext"]
},
"include": [
"./src/**/*"
]
"./src/**/*",
],
"exclude": ["node_modules", "addon"]
}
7 changes: 7 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"include": [
"__test__",
"./src/**/*"
]
}
Loading

0 comments on commit 448f813

Please sign in to comment.