-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
2,872 additions
and
1,393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Place your settings in this file to overwrite default and user settings. | ||
{ | ||
"files.exclude": { | ||
"out": false | ||
}, | ||
"search.exclude": { | ||
"out": true, | ||
"server": true | ||
}, | ||
"files.trimTrailingWhitespace": true, | ||
"editor.insertSpaces": false, | ||
"editor.tabSize": 4, | ||
"typescript.tsdk": "./node_modules/typescript/lib", | ||
"typescript.tsc.autoDetect": "off", | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true | ||
}, | ||
"eslint.trace.server": "off", | ||
"eslint.lintTask.enable": false, | ||
"eslint.format.enable": false, | ||
"editor.detectIndentation": false, | ||
"javascript.format.insertSpaceBeforeAndAfterBinaryOperators": true, | ||
"javascript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false, | ||
"javascript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": true, | ||
"css.lint.important": "warning", | ||
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": false, | ||
"typescript.format.placeOpenBraceOnNewLineForFunctions": false, | ||
"javascript.format.placeOpenBraceOnNewLineForControlBlocks": false, | ||
"javascript.format.placeOpenBraceOnNewLineForFunctions": false, | ||
"eslint.validate": [ | ||
"javascript", | ||
"javascriptreact", | ||
"typescript", | ||
"typescriptreact" | ||
], | ||
"editor.formatOnSave": true, | ||
"editor.defaultFormatter": "vscode.typescript-language-features", // https://github.com/prettier/prettier-vscode#default-formatter | ||
"[typescript]": { | ||
"editor.defaultFormatter": "vscode.typescript-language-features" | ||
}, | ||
"[typescriptreact]": { | ||
"editor.defaultFormatter": "vscode.typescript-language-features" | ||
}, | ||
"editor.wordWrap": "on", | ||
"editor.renderWhitespace": "all", | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/usr/bin/env node | ||
import chalk from 'chalk' | ||
import os from 'os' | ||
import path from 'path' | ||
const commander = require('commander') | ||
import logSymbols from 'log-symbols'; | ||
import _sqlite, { Database } from 'sqlite3' | ||
|
||
import whats from '../index' | ||
import record from '../lib/db'; | ||
import { config } from '../lib/util/config'; | ||
import { checkVers } from '../lib/util/checkVers'; | ||
import { dropDB, tableCreated } from '../lib/db/handlers'; | ||
import dropCLI from '../lib/db/dropCLI'; | ||
import pkg from '../package.json'; | ||
|
||
|
||
(() => { | ||
const dbName = '.whats.sqlite'; | ||
const dbPath = path.join(os.homedir(), dbName); | ||
const sqlite = _sqlite.verbose() | ||
const program = new commander.Command(); | ||
const version = pkg.version; | ||
|
||
program.version(version, '-v, --vers', 'output the current version'); | ||
|
||
program | ||
.name('whats') | ||
.usage('<query> [options]') | ||
.option('-n, --normal', 'normalize text color of your terminal') | ||
.option('-f, --from <source>', 'the source language to translate') | ||
.option('-t, --to <target>', 'the target language') | ||
.option('-s, --say', 'use default system voice and speak') | ||
.option('-r, --record [limit: number | clear]', 'show the query record'); | ||
|
||
program.on('--help', () => { | ||
console.log(''); | ||
console.log('Examples:'); | ||
console.log(' $ whats love'); | ||
console.log(' $ whats bonjour -f fr'); | ||
console.log(' $ whats こんにちは -f ja -t en'); | ||
console.log(' $ whats I love you very much'); | ||
console.log(' $ whats -r clear'); | ||
console.log(' $ whats -r 10'); | ||
console.log(''); | ||
}); | ||
|
||
if (!process.argv.slice(2).length) { | ||
console.log(''); | ||
return program.help(); | ||
} | ||
if (!checkVers()) { | ||
return console.log(logSymbols.warning + ' 您的 Node.js 版本过低'); | ||
} | ||
|
||
program.parse(process.argv); | ||
|
||
config.say = !!program.say; | ||
|
||
// use global chalk instead of config.normalize | ||
// because we not need getChalk() every time | ||
config.chalk = program.normal | ||
? (str: string) => str | ||
: (str: string, color: string) => (chalk as any)[color](str); | ||
|
||
// deal with record commander | ||
let limit; | ||
const rawRecord = program.record; | ||
const recordConfig = config.recordConfig; | ||
const invaild = logSymbols.error + ' ' + `无效参数 (${rawRecord}),使用 -h 查看帮助`; | ||
if (rawRecord && typeof rawRecord === 'string') { | ||
if (rawRecord === 'clear') { | ||
// use dropDB as closure to avoid path issue | ||
return dropCLI(() => dropDB(dbPath)); | ||
} | ||
if (!(limit = Number(rawRecord))) { | ||
return console.log(invaild); | ||
} | ||
recordConfig.limit = limit; | ||
} | ||
|
||
// For now, it's easily to fail to find the database file by spreading the database creation, | ||
// so we'll have to create it in the first place | ||
const db: Database = sqlite.cached.Database(dbPath); | ||
|
||
db.on('open', () => { | ||
tableCreated(dbPath).then((created: boolean) => { | ||
const dbOpts = { db, created }; | ||
config.dbOpts = dbOpts; | ||
program.record | ||
? record() | ||
: whats(program.from, program.to); | ||
}) | ||
.catch((e: Error) => { | ||
console.log(logSymbols.error + ' ' + e.message); | ||
db.close(); | ||
}); | ||
}) | ||
|
||
db.on('error', (e: Error) => { | ||
console.log(logSymbols.error + ' ' + e); | ||
db.close(); | ||
}) | ||
|
||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
declare module 'is-chinese'; | ||
|
||
declare module 'xml2js' | ||
|
||
declare module 'blueimp-md5' | ||
|
||
declare module 'tty-table' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import say from 'say'; | ||
import iciba from './src/iciba'; | ||
import youdao from './src/youdao'; | ||
import logSymbols from 'log-symbols'; | ||
import { config } from './lib/util/config'; | ||
import { checkLang } from './lib/util/checkLang'; | ||
import { formatQuery } from './lib/util/formatQuery'; | ||
|
||
export default async (from: string, to: string) => { | ||
let useIciba; | ||
|
||
try { | ||
const { | ||
query, | ||
isSent, | ||
isChinese, | ||
} = formatQuery(); | ||
config.isChinese = isChinese; | ||
|
||
useIciba = checkLang(from, to) && !isSent; | ||
config.useIciba = useIciba; | ||
if (useIciba) { | ||
await iciba(query); | ||
} else { | ||
await youdao(query, from, to); | ||
} | ||
if (config.say) say.speak(query); | ||
|
||
} catch (e: any) { | ||
const msg = e.message || '出现了一个错误...'; | ||
console.error(logSymbols.error + ' ' + msg); | ||
} | ||
}; |
Oops, something went wrong.