Skip to content

Commit

Permalink
feat: when not tty use SimpleProgressBar
Browse files Browse the repository at this point in the history
  • Loading branch information
likun7981 committed May 23, 2022
1 parent 3f17ebc commit de71440
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 14 deletions.
4 changes: 1 addition & 3 deletions hlink.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export default {
* 1. 配置了excludeExtname,则链接文件为排除后的其他文件
* 2. 未配置excludeExtname,则链接文件为目录下的所有文件
*/
includeExtname: [
// 'js'
],
includeExtname: ['js'],
/**
* 需要排除的后缀名, 如果配置了includeExtname则该配置无效
*/
Expand Down
2 changes: 1 addition & 1 deletion src/bins/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function doctor() {
log.info('如何添加环境变量请自行搜索~')
let full = currentDir
log.info(
'或者你可以全局路劲使用hlink, 路劲为',
'或者你可以全局路径使用hlink, 路径为',
chalk.cyan(path.resolve(full, '../../../../bin/hlink'))
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/bins/main/hlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import link from './link.js'
import chalk from 'chalk'
import { saveCache } from '../../config/cacheHelp.js'
import { Flags } from './index.js'
import ProgressBar from '../../progress.js'
import ProgressBar, { SimpleProgressBar } from '../../progress.js'

const green = '\u001b[42m \u001b[0m'
const red = '\u001b[47m \u001b[0m'
Expand Down Expand Up @@ -58,7 +58,7 @@ async function hardLink(input: string[], options: Flags): Promise<void> {
log.info('开始执行...')
const count = 21
let c = 0
const bar = new ProgressBar(
const bar = process.stdout.isTTY ? new ProgressBar(
`\n ${chalk.green('● hlink')} :bar :percent :etas ${chalk.gray(
':current/:total'
)} \n :file \n \n`,
Expand All @@ -68,7 +68,7 @@ async function hardLink(input: string[], options: Flags): Promise<void> {
total: waitLinkFiles.length,
clear: true
}
)
) : new SimpleProgressBar(waitLinkFiles.length)
for (let i = 0, len = waitLinkFiles.length / count; i < len; i++) {
const start = c * count
const end = (c + 1) * count
Expand Down
4 changes: 2 additions & 2 deletions src/bins/main/parseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ async function parseConfig(configPath: string) {
!path.isAbsolute(source),
'配置文件',
chalk.cyan(configPath),
'源地址必须指定绝对路劲'
'源地址必须指定绝对路径'
)
}
if (dest) {
warning(
!path.isAbsolute(dest),
'配置文件',
chalk.cyan(configPath),
'目标必须指定绝对路劲'
'目标必须指定绝对路径'
)
}
return {
Expand Down
2 changes: 1 addition & 1 deletion src/bins/prune/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ QQ反馈群号:${chalk.cyanBright('807101297')}
注意:
因为我们采用 ${chalk.cyan(',(英文逗号)')} 来进行多项输入。
所以你的路劲一定不要包含 ${chalk.cyan(',(英文逗号)')}
所以你的路径一定不要包含 ${chalk.cyan(',(英文逗号)')}
否则会导致未知错误,后果自负~
用法:
Expand Down
2 changes: 1 addition & 1 deletion src/bins/prune/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async function prune(sourceStr: string, destStr: string, flags: Flags) {
)
}
log.info(
`共计找到 ${chalk.cyan(pathsNeedDelete.length)} 个路劲需要删除,列表如下`
`共计找到 ${chalk.cyan(pathsNeedDelete.length)} 个路径需要删除,列表如下`
)
pathsNeedDelete.forEach(file => {
console.log('', chalk.gray(file))
Expand Down
16 changes: 16 additions & 0 deletions src/progress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ansiEscapes from 'ansi-escapes'
import wrapAnsi from 'wrap-ansi'
import { log } from './utils.js'

type TokenType = Record<string, any>
type OptionsType = {
Expand Down Expand Up @@ -214,4 +215,19 @@ class ProgressBar {
}
}


export class SimpleProgressBar {
private total: number
private current: number;
constructor(total: number) {
log.info('如果你看到这个消息,说明你的bash不支持格式化输入')
this.total = total
this.current = 0
}
tick = (count: number) => {
this.current += count
log.info(`执行中,当前进度${this.current}/${this.total}`)
}
}

export default ProgressBar
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export async function rmFiles(files: string[]) {

/**
*
* @param _paths 路劲集合
* @param _paths 路径集合
* @returns 返回_paths的公共父目录
*
* _paths = ['/a/c/d/e', '/a/b/c/d/e']
Expand All @@ -204,14 +204,14 @@ export function findParent(_paths: string[]) {
let paths = [..._paths]
if (!paths.length) return ''
/**
* 排序,把最短的路劲排到最前面
* 排序,把最短的路径排到最前面
*/
paths = paths.sort(
(a, b) => a.split(path.sep).length - b.split(path.sep).length
)
const firstItem = paths.shift() as string // 这里必有
let dirname = path.join(path.dirname(firstItem), '/')
// 如果paths里面每个都包含了最短路劲,说明最短路劲就算所有路劲的目录了
// 如果paths里面每个都包含了最短路径,说明最短路径就算所有路径的目录了
while (!paths.every(p => p.includes(dirname))) {
dirname = path.join(path.dirname(dirname), '/')
}
Expand Down

0 comments on commit de71440

Please sign in to comment.