-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add biome and github action (#102)
- Loading branch information
Showing
17 changed files
with
362 additions
and
382 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,22 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
- run: npm ci | ||
- run: npm run lint | ||
- run: npm run coverage | ||
- uses: codecov/codecov-action@v4 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} |
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 |
---|---|---|
|
@@ -20,3 +20,4 @@ node_modules/ | |
.settings | ||
.yml | ||
package-lock.json | ||
coverage |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,76 +1,85 @@ | ||
#!/usr/bin/env node | ||
|
||
const program = require("commander"); | ||
const chalk = require("chalk"); | ||
const updateNotifier = require("update-notifier"); | ||
const pkg = require("../package.json"); | ||
const config = require("../lib/config"); | ||
const { searchList } = require("../lib/searchHistory"); | ||
const { program } = require('commander'); | ||
const chalk = require('chalk'); | ||
const updateNotifier = require('update-notifier'); | ||
const pkg = require('../package.json'); | ||
const config = require('../lib/config'); | ||
const { searchList } = require('../lib/searchHistory'); | ||
|
||
updateNotifier({ pkg }).notify(); | ||
|
||
program.version(pkg.version); | ||
program | ||
.name(pkg.name) | ||
.description(pkg.description) | ||
.version(pkg.version) | ||
.action(() => { | ||
// If the input is "fanyi", no parameters, ignore. | ||
if (process.argv.length > 2) { | ||
return runFY(); | ||
} | ||
}); | ||
|
||
program | ||
.command("config") | ||
.description("Set the global options") | ||
.option("-c, --color", "Output with color") | ||
.option("-C, --no-color", "Output without color") | ||
.option("-i, --iciba", "Enable the iciba translation engine") | ||
.option("-I, --no-iciba", "Disable the iciba translation engine") | ||
.action((args) => { | ||
// hack | ||
// If the input is "fanyi config", then translate the word config. | ||
if (process.argv.length === 3) { | ||
return runFY(); | ||
} | ||
const { color, iciba } = args; | ||
const options = resolveOptions({ color, iciba }); | ||
return config.write(options); | ||
}); | ||
.command('config') | ||
.description('Set the global options') | ||
.option('-c, --color', 'Output with color') | ||
.option('-C, --no-color', 'Output without color') | ||
.option('-i, --iciba', 'Enable the iciba translation engine') | ||
.option('-I, --no-iciba', 'Disable the iciba translation engine') | ||
.action((args) => { | ||
// hack | ||
// If the input is "fanyi config", then translate the word config. | ||
if (process.argv.length === 3) { | ||
return runFY(); | ||
} | ||
const { color, iciba } = args; | ||
const options = resolveOptions({ color, iciba }); | ||
return config.write(options); | ||
}); | ||
|
||
program | ||
.command("list") | ||
.option("-d, --someDay <char>", "查看指定某天的查询记录") | ||
.option("-r, --recentDays [number]", "查看最近几天内的数据", 0) | ||
.option("-all --show-file [boolean]", "查看全部数据,即单词存放的位置", false) | ||
.action((args) => { | ||
searchList(args); | ||
}); | ||
.command('list') | ||
.option('-d, --someDay <char>', '查看指定某天的查询记录') | ||
.option('-r, --recentDays [number]', '查看最近几天内的数据', 0) | ||
.option('-all --show-file [boolean]', '查看全部数据,即单词存放的位置', false) | ||
.action((args) => { | ||
searchList(args); | ||
}); | ||
|
||
program.on("--help", () => { | ||
console.log(""); | ||
console.log(chalk.gray("Examples:")); | ||
console.log(`${chalk.cyan(" $ ")}fanyi word`); | ||
console.log(`${chalk.cyan(" $ ")}fanyi world peace`); | ||
console.log(`${chalk.cyan(" $ ")}fanyi chinglish`); | ||
console.log(""); | ||
program.on('--help', () => { | ||
console.log(''); | ||
console.log(chalk.gray('Examples:')); | ||
console.log(`${chalk.cyan(' $ ')}fanyi word`); | ||
console.log(`${chalk.cyan(' $ ')}fanyi world peace`); | ||
console.log(`${chalk.cyan(' $ ')}fanyi chinglish`); | ||
console.log(''); | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
if (!process.argv.slice(2).length) { | ||
program.outputHelp(); | ||
program.help(); | ||
} | ||
|
||
async function runFY(options = {}) { | ||
const defaultOptions = await config.load(); | ||
const mergedOptions = { ...defaultOptions, ...options }; | ||
const fanyi = require(".."); | ||
fanyi(program.args.join(" "), mergedOptions); | ||
const defaultOptions = await config.load(); | ||
const mergedOptions = { ...defaultOptions, ...options }; | ||
const fanyi = require('..'); | ||
fanyi(program.args.join(' '), mergedOptions); | ||
} | ||
|
||
function resolveOptions(options) { | ||
const opts = {}; | ||
const filteredKeys = Object.keys(options).filter( | ||
(key) => isBoolean(options[key]) || typeof options[key] === "string", | ||
); | ||
for (const key of filteredKeys) { | ||
opts[key] = options[key]; | ||
} | ||
return opts; | ||
const opts = {}; | ||
const filteredKeys = Object.keys(options).filter( | ||
(key) => isBoolean(options[key]) || typeof options[key] === 'string', | ||
); | ||
for (const key of filteredKeys) { | ||
opts[key] = options[key]; | ||
} | ||
return opts; | ||
} | ||
|
||
function isBoolean(val) { | ||
return typeof val === "boolean"; | ||
return typeof val === 'boolean'; | ||
} |
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,23 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "space", | ||
"lineWidth": 100, | ||
"indentWidth": 2 | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"quoteStyle": "single" | ||
} | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,49 +1,49 @@ | ||
const needle = require("needle"); | ||
const chalk = require("chalk"); | ||
const SOURCE = require("./lib/source"); | ||
const print = require("./lib/print"); | ||
const parseString = require("xml2js").parseString; | ||
const ora = require("ora"); | ||
const needle = require('needle'); | ||
const chalk = require('chalk'); | ||
const SOURCE = require('./lib/source'); | ||
const print = require('./lib/print'); | ||
const parseString = require('xml2js').parseString; | ||
const ora = require('ora'); | ||
|
||
module.exports = (word, options, callback) => { | ||
console.log(""); | ||
const { iciba } = options; | ||
const requestCounts = [iciba].filter(isTrueOrUndefined).length; | ||
const spinner = ora().start(); | ||
console.log(''); | ||
const { iciba } = options; | ||
const requestCounts = [iciba].filter(isTrueOrUndefined).length; | ||
const spinner = ora().start(); | ||
|
||
let count = 0; | ||
const callbackAll = () => { | ||
count += 1; | ||
if (count >= requestCounts) { | ||
spinner.stop(); | ||
spinner.clear(); | ||
callback?.(); | ||
} | ||
}; | ||
let count = 0; | ||
const callbackAll = () => { | ||
count += 1; | ||
if (count >= requestCounts) { | ||
spinner.stop(); | ||
spinner.clear(); | ||
callback?.(); | ||
} | ||
}; | ||
|
||
const endcodedWord = encodeURIComponent(word); | ||
const endcodedWord = encodeURIComponent(word); | ||
|
||
// iciba | ||
isTrueOrUndefined(iciba) && | ||
needle.get( | ||
SOURCE.iciba.replace("${word}", endcodedWord), | ||
{ parse: false }, | ||
(error, response) => { | ||
if (error) { | ||
console.log(chalk.yellow("访问 iciba 失败,请检查网络")); | ||
} else if (response.statusCode === 200) { | ||
parseString(response.body, (err, result) => { | ||
if (err) { | ||
return; | ||
} | ||
print.iciba(result.dict, options); | ||
}); | ||
} | ||
callbackAll(); | ||
}, | ||
); | ||
// iciba | ||
isTrueOrUndefined(iciba) && | ||
needle.get( | ||
SOURCE.iciba.replace('${word}', endcodedWord), | ||
{ parse: false }, | ||
(error, response) => { | ||
if (error) { | ||
console.log(chalk.yellow('访问 iciba 失败,请检查网络')); | ||
} else if (response.statusCode === 200) { | ||
parseString(response.body, (err, result) => { | ||
if (err) { | ||
return; | ||
} | ||
print.iciba(result.dict, options); | ||
}); | ||
} | ||
callbackAll(); | ||
}, | ||
); | ||
}; | ||
|
||
function isTrueOrUndefined(val) { | ||
return val === true || val === undefined; | ||
return val === true || val === undefined; | ||
} |
Oops, something went wrong.