Skip to content

Commit

Permalink
refactor: convert to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Styx11 committed Nov 12, 2021
1 parent 22a6fa9 commit e542006
Show file tree
Hide file tree
Showing 57 changed files with 2,872 additions and 1,393 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
package-lock.json*
dist

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
Expand Down
46 changes: 46 additions & 0 deletions .vscode/settings.json
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",
}
103 changes: 0 additions & 103 deletions bin/whats.js

This file was deleted.

105 changes: 105 additions & 0 deletions bin/whats.ts
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();
})

})()
7 changes: 7 additions & 0 deletions index.d.ts
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'
33 changes: 0 additions & 33 deletions index.js

This file was deleted.

33 changes: 33 additions & 0 deletions index.ts
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);
}
};
Loading

0 comments on commit e542006

Please sign in to comment.