Skip to content

Commit

Permalink
chore: add biome and github action (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Sep 11, 2024
1 parent 7618f0a commit f0b519f
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 382 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

version: 2
updates:
- package-ecosystem: "" # See documentation for possible values
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
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 }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ node_modules/
.settings
.yml
package-lock.json
coverage
14 changes: 0 additions & 14 deletions .prettierrc

This file was deleted.

4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

111 changes: 60 additions & 51 deletions bin/fanyi.js
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';
}
23 changes: 23 additions & 0 deletions biome.json
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
}
}
}
Binary file added bun.lockb
Binary file not shown.
80 changes: 40 additions & 40 deletions index.js
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;
}
Loading

0 comments on commit f0b519f

Please sign in to comment.