Skip to content

Commit

Permalink
feat: prune 支持反向检测
Browse files Browse the repository at this point in the history
  • Loading branch information
likun7981 committed May 24, 2022
1 parent 535237e commit 6576d9c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
4 changes: 2 additions & 2 deletions hlink.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default {
* 1. 配置了excludeExtname,则链接文件为排除后的其他文件
* 2. 未配置excludeExtname,则链接文件为目录下的所有文件
*/
includeExtname: ['js'],
includeExtname: [],
/**
* 需要排除的后缀名, 如果配置了includeExtname则该配置无效
*/
Expand All @@ -38,7 +38,7 @@ export default {
* 如果保存模式为1 生成的硬链地址为:/d/y/mv.mkv
*/
saveMode: 0,
openCache: false,
openCache: true,
mkdirIfSingle: false,
delete: true
}
18 changes: 13 additions & 5 deletions src/bins/prune/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ QQ反馈群号:${chalk.cyanBright('807101297')}
$ hlink prune -w sourceDir1,sourceDir2 destDir1,destDir2
可配置选项:
--pruneDir,-p 是否删除硬链文件及所在目录。
如果给了这个选项则会 否则只会删除 硬链文件
--pruneDir,-p 是否删除文件及所在目录,默认只会删除文件
${chalk.gray('如果你指定了该选项则会删除文件及所在目录')}
--withoutConfirm,-w 删除前是否需确认? 默认需要确认。
如果你使用计划任务,建议设置为无需确认
--includeExtname,-i 检测包含的后缀名,同hlink --help中的includeExtname一样
--excludeExtname,-e 检测排除的后缀名,同hlink --help中的excludeExtname一样
${chalk.gray('如果你使用计划任务,建议设置为无需确认')}
${chalk.gray('Windows Git Bash不支持提示,所以会直接删除,执行前确认好')}
--includeExtname,-i 检测包含的后缀名,同hlink --help中的includeExtname一样
--excludeExtname,-e 检测排除的后缀名,同hlink --help中的excludeExtname一样
--reverse,-r 检测方向,默认是正向检测,如果你指定了该选项则会是反向检测
${chalk.gray(`1. 正向检测:删除的是硬链目录的文件,修剪硬链目录比源目录多的文件。
${chalk.cyan('注意:正向检测一定要列全所有的源目录')}
2. 反向检测:删除的是源目录目录的文件,修剪源目录比硬链目录多的文件。
${chalk.cyan('注意:反向检测一定要列全所有的硬链目录,hlink会帮你排除缓存的文件')}`)}
`
35 changes: 30 additions & 5 deletions src/bins/prune/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import { createTimeLog, log, makeOnly, rmFiles, warning } from '../../utils.js'
import helpText from './help.js'
import defaultInclude from '../defaultInclude.js'
import deleteEmptyDir from './deleteEmptyDir.js'
import { cacheRecord } from '../../paths.js'

const timeLog = createTimeLog()
export type Flags = Pick<
IHlink.Flags,
'help' | 'pruneDir' | 'withoutConfirm' | 'includeExtname' | 'excludeExtname'
| 'help'
| 'pruneDir'
| 'withoutConfirm'
| 'includeExtname'
| 'excludeExtname'
| 'reverse'
>

async function prune(sourceStr: string, destStr: string, flags: Flags) {
Expand All @@ -19,13 +25,15 @@ async function prune(sourceStr: string, destStr: string, flags: Flags) {
pruneDir,
withoutConfirm,
includeExtname,
excludeExtname
excludeExtname,
reverse
} = flags
if (help) {
console.log(helpText)
process.exit(0)
}
warning(!sourceStr || !destStr, '必须指定要检测的源目录和硬链目录集合')
const cached = (reverse && cacheRecord.read()) || []
const exts = (includeExtname || excludeExtname ? '' : defaultInclude)
.split(',')
.filter(Boolean)
Expand All @@ -36,8 +44,8 @@ async function prune(sourceStr: string, destStr: string, flags: Flags) {
.map((s: string) => s.toLowerCase())
const isWhiteList = !!exts.length
timeLog.start()
const sourceArr = sourceStr.split(',').map(s => path.resolve(s))
const destArr = destStr.split(',').map(d => path.resolve(d))
let sourceArr = sourceStr.split(',').map(s => path.resolve(s))
let destArr = destStr.split(',').map(d => path.resolve(d))
log.info('开始执行...')
log.info('指定的源目录有:')
sourceArr.forEach(s => {
Expand All @@ -49,6 +57,14 @@ async function prune(sourceStr: string, destStr: string, flags: Flags) {
console.log('', chalk.gray(d))
})
console.log()
log.info(
'检测模式:',
chalk.magenta(
reverse
? '反向检测,删除源目录比硬链目录多的文件,hlink会帮你排除缓存的文件'
: '正向检测,删除硬链目录比源目录多的文件'
)
)
log.info(
'删除模式:',
chalk.magenta(pruneDir ? '删除硬链所在目录' : '仅仅删除硬链文件')
Expand All @@ -61,6 +77,11 @@ async function prune(sourceStr: string, destStr: string, flags: Flags) {
isWhiteList ? '包含的后缀有:' : '排除的后缀有:',
chalk.magenta(isWhiteList ? exts.join(',') : excludeExts.join(','))
)
if (reverse) {
const tmp = sourceArr
sourceArr = destArr
destArr = tmp
}
log.info('开始分析目录集合...')
const inodes = makeOnly(
sourceArr.reduce<string[]>(
Expand All @@ -80,9 +101,13 @@ async function prune(sourceStr: string, destStr: string, flags: Flags) {
.extname(item.fullPath)
.replace('.', '')
.toLowerCase()
const isSupported = isWhiteList
let isSupported = isWhiteList
? exts.indexOf(extname) > -1
: excludeExts.indexOf(extname) === -1
// 反向检测,则需要关注cache的处理
if (reverse && isSupported) {
isSupported = !cached.includes(item.fullPath)
}
return isSupported
})
.map(item =>
Expand Down
17 changes: 11 additions & 6 deletions src/types/IHlink.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ declare namespace IHlink {
excludeExtname: string
openCache?: boolean
mkdirIfSingle?: boolean
/**
* @deprecated 废弃
*/
del?: boolean
generateConfig: string
removeConfig: boolean
configPath: string
help?: boolean

/**
* @description prune命令专用
*/
Expand All @@ -14,12 +23,8 @@ declare namespace IHlink {
*/
withoutConfirm?: boolean
/**
* @deprecated 废弃
* @description prune命令专用
*/
del?: boolean
generateConfig: string
removeConfig: boolean
configPath: string
help?: boolean
reverse?: boolean
}
}

0 comments on commit 6576d9c

Please sign in to comment.