Skip to content

Commit cb476c5

Browse files
committed
initial commit
0 parents  commit cb476c5

File tree

18 files changed

+2947
-0
lines changed

18 files changed

+2947
-0
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
release

.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
root: true,
3+
env: { es6: true, node: true },
4+
parser: '@typescript-eslint/parser',
5+
parserOptions: {
6+
ecmaVersion: 2020,
7+
sourceType: 'module',
8+
project: './tsconfig.eslint.json',
9+
},
10+
plugins: ['@typescript-eslint', 'prettier'],
11+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
12+
globals: {
13+
$log: false,
14+
$info: false,
15+
$option: false,
16+
$http: false,
17+
$file: false,
18+
$data: false,
19+
},
20+
rules: {
21+
'@typescript-eslint/ban-ts-comment': 'off',
22+
'@typescript-eslint/no-extra-semi': 'off',
23+
'@typescript-eslint/no-var-requires': 'off',
24+
},
25+
}

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
.idea
4+
5+
# dependencies
6+
/node_modules
7+
/.pnp
8+
.pnp.js
9+
10+
# testing
11+
/coverage
12+
13+
# production
14+
/build
15+
/release
16+
17+
# misc
18+
.DS_Store
19+
.env.local
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
28+
.vercel

.prettierrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
semi: false,
3+
singleQuote: true,
4+
trailingComma: 'all',
5+
jsxBracketSameLine: true,
6+
};

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Roy Li
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# bob-plugin-deepl-translate
2+
3+
TBD

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "bob-plugin-deepl-translate",
3+
"version": "0.1.0",
4+
"author": "Roy Li <me@dada.li>",
5+
"license": "MIT",
6+
"private": true,
7+
"scripts": {
8+
"clean": "rimraf build && rimraf release",
9+
"build": "run-s clean && cross-env NODE_ENV=production rollup -c rollup.config.js",
10+
"dev": "run-s clean && cross-env NODE_ENV=development rollup -c rollup.config.js --watch",
11+
"type-check": "tsc --noEmit",
12+
"bundle": "node scripts/bundle.js",
13+
"bundle:watch": "nodemon --watch build scripts/bundle.js"
14+
},
15+
"dependencies": {
16+
"@rollup/plugin-commonjs": "^16.0.0",
17+
"@rollup/plugin-json": "^4.1.0",
18+
"@rollup/plugin-node-resolve": "^10.0.0",
19+
"@rollup/plugin-typescript": "^6.1.0",
20+
"@types/adm-zip": "^0.4.33",
21+
"@types/jsonfile": "^6.0.0",
22+
"@types/node": "^12",
23+
"@typescript-eslint/eslint-plugin": "^4.8.0",
24+
"@typescript-eslint/parser": "^4.8.0",
25+
"adm-zip": "^0.4.16",
26+
"cross-env": "^7.0.2",
27+
"esbuild": "^0.7.22",
28+
"eslint": "^7.13.0",
29+
"eslint-plugin-prettier": "^3.1.4",
30+
"form-data": "^3.0.0",
31+
"fs-extra": "^9.0.1",
32+
"nodemon": "^2.0.6",
33+
"npm-run-all": "^4.1.5",
34+
"prettier": "^2.1.2",
35+
"rollup": "^2.28.2",
36+
"rollup-plugin-copy": "^3.3.0",
37+
"rollup-plugin-node-polyfills": "^0.2.1",
38+
"ts-node": "^9.0.0",
39+
"tslib": "^2.0.3",
40+
"type-fest": "^0.19.0",
41+
"typescript": "^4.0.5"
42+
},
43+
"nodemonConfig": {
44+
"ignore": [
45+
"build/info.json"
46+
]
47+
}
48+
}

rollup.config.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { join } from 'path'
2+
import copy from 'rollup-plugin-copy'
3+
import json from '@rollup/plugin-json'
4+
import commonjs from '@rollup/plugin-commonjs'
5+
import resolve from '@rollup/plugin-node-resolve'
6+
import nodePolyfills from 'rollup-plugin-node-polyfills'
7+
import typescript from '@rollup/plugin-typescript'
8+
9+
export default {
10+
input: join(__dirname, './src/main.ts'),
11+
output: {
12+
format: 'cjs',
13+
exports: 'auto',
14+
dir: join(__dirname, `./build`),
15+
preserveModules: true,
16+
},
17+
plugins: [
18+
copy({
19+
targets: [
20+
// { src: './src/info.json', dest: `build` },
21+
],
22+
}),
23+
json({ namedExports: false }),
24+
resolve({
25+
extensions: ['.js', '.ts'],
26+
preferBuiltins: true,
27+
}),
28+
commonjs({
29+
include: ['node_modules/**'],
30+
}),
31+
nodePolyfills(),
32+
typescript({}),
33+
],
34+
external: [
35+
'crypto-js',
36+
'$util',
37+
'$http',
38+
'$info',
39+
'$option',
40+
'$log',
41+
'$data',
42+
'$file',
43+
],
44+
}

scripts/bundle.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const { join } = require('path')
2+
const AdmZip = require('adm-zip')
3+
const fs = require('fs-extra')
4+
const { promisify } = require('util')
5+
6+
function generateInfo() {
7+
return {
8+
identifier: 'dev.royli.bob-plugin-deepl-translate',
9+
category: 'translate',
10+
name: 'DeepL Translate',
11+
summary: '',
12+
icon: '115',
13+
author: 'Roy Li',
14+
homepage: 'https://royli.dev',
15+
minBobVersion: '0.5.0',
16+
options: [
17+
{
18+
identifier: 'token',
19+
type: 'text',
20+
title: 'Token',
21+
},
22+
{
23+
identifier: 'provider',
24+
type: 'menu',
25+
title: 'API',
26+
defaultValue: 'deepl',
27+
menuValues: [
28+
{
29+
title: 'DeepL',
30+
value: 'deepl',
31+
},
32+
{
33+
title: 'Sub DeepL',
34+
value: 'sub-deepl',
35+
},
36+
],
37+
},
38+
{
39+
identifier: 'formality',
40+
type: 'menu',
41+
title: 'Formality',
42+
defaultValue: 'default',
43+
menuValues: [
44+
{
45+
title: 'Default',
46+
value: 'default',
47+
},
48+
{
49+
title: 'More formal',
50+
value: 'more',
51+
},
52+
{
53+
title: 'More informal',
54+
value: 'less',
55+
},
56+
],
57+
},
58+
],
59+
}
60+
}
61+
62+
async function main() {
63+
const pkgName = 'bob-plugin-deepl-translate'
64+
const version = require('../package.json').version
65+
const buildDir = join(__dirname, '../build')
66+
const releaseDir = join(__dirname, '../release')
67+
const pkg = join(releaseDir, `${pkgName}-v${version}.bobplugin`)
68+
const info = {
69+
...generateInfo(),
70+
version,
71+
appcast: '',
72+
}
73+
74+
await fs.writeJson(join(buildDir, 'info.json'), info)
75+
76+
const zip = new AdmZip()
77+
zip.addLocalFolder(buildDir)
78+
await promisify(zip.writeZip)(pkg)
79+
}
80+
81+
main().catch((err) => {
82+
console.error(err)
83+
process.exit(1)
84+
})

src/api.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { RequestCallbackResponse, RequestObject } from '../types/global'
2+
3+
export class Api {
4+
constructor(private provider: 'deepl' | 'sub-deepl', private token: string) {}
5+
6+
private get baseUrl(): string {
7+
switch (this.provider) {
8+
case 'deepl':
9+
return 'https://api.deepl.com'
10+
case 'sub-deepl':
11+
return 'https://sub-deepl-api.nerdynerd.org'
12+
}
13+
}
14+
15+
async request<T = Record<string, unknown>>(
16+
requestObject: Omit<RequestObject, 'handler' | 'header'>,
17+
): Promise<RequestCallbackResponse<T>> {
18+
try {
19+
return await $http.request({
20+
...requestObject,
21+
url: `${this.baseUrl}${requestObject.url}`,
22+
header: {
23+
'Content-Type': 'application/x-www-form-urlencoded',
24+
},
25+
body: {
26+
...requestObject.body,
27+
auth_key: this.token,
28+
},
29+
})
30+
} catch (e) {
31+
Object.assign(e, {
32+
_type: 'network',
33+
_message: '接口请求错误',
34+
})
35+
36+
throw e
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)