Comparing version 0.1.0 to 0.2.0
40
cli.js
#!/usr/bin/env node | ||
"use strict"; | ||
const chalk = require("chalk"); | ||
const meow = require("meow"); | ||
const hardLink = require("./lib/index"); | ||
const hardLink = require("./lib/index"); | ||
const cli = meow( | ||
` | ||
Usage | ||
$ hlink [source] [dist] | ||
用法: | ||
$ hlink [--Options] [sourceDir] distDir | ||
Options | ||
--saveLevel,-l [Default: 1] | ||
说明:如果源视频文件目录结构是 /share/download/movie/a.mkv,硬链接目的地目录是/share/media | ||
saveLevel=2 时 结果就是 "/share/media/download/movie/a.mkv" | ||
saveLevel=1 时 结果就是 "/share/media/movie/a.mkv" | ||
可配置选项: | ||
--saveLevel,-l [Default: 0] | ||
${chalk.gray(`saveLevel=2 只保存文件 | ||
saveLevel=1 保存一级目录 | ||
saveLevel=0 保存原有的相对源地址的路径`)} | ||
--ext,-e [Default: mkv,mp4,rmvb] | ||
--maxFindLevel,-m [Default: 4] 删除硬链 | ||
--delete,-d 删除目标地址所有硬链 | ||
${chalk.gray(`delete=1 表示删除目录 | ||
delete=0 表示只删除文件`)} | ||
例子: | ||
${chalk.grey(`# 创建 /share/download 下面文件到目标地址 /share/movie`)} | ||
$ hlink /share/download /share/movie | ||
--ext,-e [Default: mkv,mp4,rmvb] | ||
--maxFindLevel [Default: 4] | ||
${chalk.grey(`# 删除 /share/download 下面文件在 /share/movie 下面的对应硬链的文件夹`)} | ||
$ hlink -d=1 /share/download /share/movie | ||
说明: | ||
1. 创建硬链时,会自动检测硬链接是否存在,硬链改名同样能检测到 | ||
2. sourceDir 不填,则表示 sourceDir 为当前 允许目录 | ||
`, | ||
@@ -24,3 +38,3 @@ { | ||
type: "string", | ||
default: "1", | ||
default: "0", | ||
alias: "s" | ||
@@ -37,2 +51,6 @@ }, | ||
alias: "m" | ||
}, | ||
delete: { | ||
type: 'string', | ||
alias: 'd' | ||
} | ||
@@ -39,0 +57,0 @@ } |
@@ -7,24 +7,16 @@ const fs = require("fs-extra"); | ||
const chalk = require("chalk"); | ||
const { | ||
checkLinkExist, | ||
checkDirectory, | ||
checkFindLevels, | ||
checkLevels, | ||
getLinkPath, | ||
getDirBasePath | ||
} = require('./utils'); | ||
const resolvePath = path.resolve; | ||
function checkLevels(levels) { | ||
warning(Number.isNaN(levels), "保存的最大层级saveDirLevel必须设置为数字"); | ||
warning( | ||
levels > 2 || levels < 0, | ||
"保存的最大层级saveDirLevel只能设置为0/1/2" | ||
); | ||
} | ||
function checkFindLevels(levels) { | ||
warning(Number.isNaN(levels), "查找的最大层级maxFindLevel必须设置为数字"); | ||
warning(levels > 6 || levels < 1, "保存的最大层级maxFindLevel不能小于1大于6"); | ||
} | ||
function checkDirectory(source, dest) { | ||
fs.ensureDirSync(dest); | ||
warning(source === dest, "起始地址和目标地址不能相同"); | ||
} | ||
function getRealDestPath(sourcePath, destPath, saveLevels, startPath, s) { | ||
if (saveLevels > 0) { | ||
if (saveLevels !== 2) { | ||
const relativePath = path | ||
@@ -54,3 +46,3 @@ .relative(startPath, path.resolve(sourcePath)) || s.replace(path.extname(s), ''); | ||
checkDirectory(source, dest); | ||
const { s, e, m } = options; | ||
const { s, e, m, d } = options; | ||
const exts = e.split(","); | ||
@@ -64,9 +56,14 @@ const saveLevels = +s; | ||
m: " 源地址最大查找层级为:", | ||
s: " 硬链保存源地址的目录层级数为:" | ||
s: " 硬链保存模式:" | ||
}; | ||
log.info("开始创建硬链..."); | ||
log.info("当前配置为:"); | ||
Object.keys(messageMap).forEach(k => { | ||
log.info(`${messageMap[k]}${chalk.cyanBright(options[k])}`); | ||
}); | ||
if (!d) { | ||
log.info("开始创建硬链..."); | ||
log.info("当前配置为:"); | ||
Object.keys(messageMap).forEach(k => { | ||
log.info(`${messageMap[k]}${chalk.cyanBright(options[k])}`); | ||
}); | ||
} else { | ||
log.info("开始删除硬链...") | ||
log.info(`删除模式为: ${chalk.cyan(d === '0' ? '仅删除文件' : '删除对应目录')}`) | ||
} | ||
function start(sourcePath, currentLevel = 1) { | ||
@@ -84,3 +81,2 @@ if (currentLevel > maxFindLevels) { | ||
} else if (exts.indexOf(extname) > -1) { | ||
// 做硬链接 | ||
const realDestPath = getRealDestPath( | ||
@@ -93,15 +89,42 @@ sourcePath, | ||
); | ||
const sourceNameForMessage = chalk.yellow( | ||
path.relative(path.join(source, ".."), filePath) | ||
); | ||
const destNameForMessage = chalk.cyan( | ||
path.relative(path.join(dest, ".."), path.join(realDestPath, s)) | ||
); | ||
if (!!d) { | ||
// 删除硬链接 | ||
try { | ||
const linkPaths = getLinkPath(filePath, realDestPath, d === '1' && dest) | ||
if (!linkPaths.length) { | ||
log.warn(`没有找到 ${chalk.cyan(getDirBasePath(source, filePath))} 硬链接`); | ||
} | ||
linkPaths.map((removePath) => { | ||
execa.sync('rm', ['-r', removePath]) | ||
const deletePathMessage = chalk.cyan(getDirBasePath(dest, removePath)); | ||
if (d === '0') { | ||
log.info(`删除硬链文件成功 ${deletePathMessage} `) | ||
} else { | ||
log.info(`目录 ${deletePathMessage} 已删除`) | ||
} | ||
}) | ||
} catch (e) { | ||
if (e.message === 'ALREADY_DELETE') { | ||
log.warn(`目录 ${chalk.cyan(getDirBasePath(dest, realDestPath))} 已删除`) | ||
} | ||
} | ||
return | ||
} | ||
// 做硬链接 | ||
const sourceNameForMessage = chalk.yellow(getDirBasePath(source, filePath)); | ||
const destNameForMessage = chalk.cyan(getDirBasePath(dest, path.join(realDestPath, s))); | ||
try { | ||
fs.ensureDirSync(realDestPath); | ||
if (checkLinkExist(filePath, realDestPath)) { | ||
throw { stderr: 'File exists' } | ||
} | ||
execa.sync("ln", [filePath, realDestPath]); | ||
log.success( | ||
`源地址 "${sourceNameForMessage}" 硬链成功, 硬链地址为 "${destNameForMessage}" ` | ||
`源地址 ${sourceNameForMessage} 硬链成功, 硬链地址为 ${destNameForMessage}` | ||
); | ||
} catch (e) { | ||
if (!e.stderr) { | ||
console.log(e); | ||
process.exit(0); | ||
} | ||
if (e.stderr.indexOf("File exists") === -1) { | ||
@@ -111,3 +134,3 @@ console.log(e); | ||
log.warn( | ||
`目标地址 "${destNameForMessage}" 硬链已存在, 跳过源地址 "${sourceNameForMessage}" 硬链接创建` | ||
`源地址"${sourceNameForMessage}"硬链已存在, 跳过创建` | ||
); | ||
@@ -114,0 +137,0 @@ } |
{ | ||
"name": "hlink", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "hlink", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10052
8
273
3