-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Cansiny0320/feat-cli
feat: cli
- Loading branch information
Showing
7 changed files
with
109 additions
and
46 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env node | ||
'use strict' | ||
import '../dist/index.mjs' | ||
import '../dist/cli.mjs' |
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,13 +1,14 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: ['src/index'], | ||
entries: ['src/cli'], | ||
// declaration: true, | ||
clean: true, | ||
failOnWarn: false, | ||
rollup: { | ||
// emitCJS: true, | ||
esbuild: { | ||
minify: true, | ||
// minify: 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,62 @@ | ||
import { cac } from 'cac' | ||
import pkg from '../package.json' | ||
import { main } from './index' | ||
|
||
const cli = cac('book-spider') | ||
|
||
cli | ||
.command('[...names]', '小说名', { | ||
allowUnknownOptions: true, | ||
}) | ||
.option('-s, --source <source>', '设置书源') | ||
.option('-t, --turn', '顺序下载') | ||
.option('-l, --limit <number>', '限制并发数', { | ||
default: 64, | ||
}) | ||
.example(' $ bs 铁血残明') | ||
.example(' $ bs 铁血残明 -s https://www.xbiquge.la') | ||
.example(' $ bs list') | ||
.action((names, options) => { | ||
main({ | ||
bookNames: names, | ||
...options, | ||
}) | ||
}) | ||
|
||
cli | ||
.command('download', '批量下载小说') | ||
.option('-s, --source <source>', '设置书源') | ||
.option('-t, --turn', '顺序下载') | ||
.option('-l, --limit <number>', '限制并发数', { | ||
default: 64, | ||
}) | ||
.action(options => { | ||
main({ | ||
download: true, | ||
...options, | ||
}) | ||
}) | ||
cli.command('list', '显示所有书源').action(() => { | ||
console.log( | ||
[ | ||
'https://www.biquge.com.cn', | ||
'https://www.xbiquge.la', | ||
'https://www.xbiquwx.la', | ||
'https://www.qu-la.com', | ||
'https://www.shuquge.com', | ||
].join('\n'), | ||
) | ||
}) | ||
|
||
cli | ||
.help(sections => { | ||
const [versionSection, usageSection] = sections | ||
versionSection.body = `book-spider v${pkg.version}` | ||
usageSection.body = ` $ bs <小说名 小说名...> [参数] | ||
$ book-spider <小说名 小说名...> [参数]` | ||
sections[2].title = 'Other Commands' | ||
const [, ...otherCommands] = sections[2].body.split('\n') | ||
sections[2].body = otherCommands.join('\n') | ||
}) | ||
.version(pkg.version) | ||
.parse() |
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,35 +1,34 @@ | ||
import fs from 'fs' | ||
import mri from 'mri' | ||
|
||
import type { IOptions } from './interface' | ||
import { Spider } from './spider' | ||
import { getSpecSource } from './utils' | ||
|
||
const args = mri(process.argv.splice(2)) | ||
export const main = async (args: any) => { | ||
const bookNames: string[] = args.bookNames ?? [] | ||
const url = args.source || args.s | ||
const limit = args.limit || args.l | ||
const inTurn = args.turn || args.t | ||
|
||
const bookNames = args._ | ||
const url = args.source || args.s | ||
const limit = args.limit || args.l | ||
const inTurn = args.t | ||
const options: IOptions = { | ||
limit: parseInt(limit) || 64, | ||
mode: inTurn ? 1 : 0, | ||
} | ||
|
||
const options: IOptions = { | ||
limit: parseInt(limit) || 64, | ||
mode: inTurn ? 1 : 0, | ||
} | ||
|
||
if (url) { | ||
options.source = getSpecSource(url) | ||
} | ||
if (url) { | ||
options.source = getSpecSource(url) | ||
} | ||
|
||
const spider = new Spider(options) | ||
const spider = new Spider(options) | ||
|
||
if (bookNames[0] === 'download') { | ||
const download = fs.readFileSync('./download.txt', 'utf-8').split('\r\n') | ||
download.forEach(item => { | ||
spider.run(item) | ||
}) | ||
} else { | ||
bookNames.forEach(item => { | ||
spider.run(item) | ||
}) | ||
if (args.download) { | ||
const download = fs.readFileSync('./download.txt', 'utf-8').split('\r\n') | ||
download.forEach(item => { | ||
spider.run(item) | ||
}) | ||
} else { | ||
bookNames.forEach(item => { | ||
spider.run(item) | ||
}) | ||
} | ||
} |