UNPKG

1.4 kBJavaScriptView Raw
1'use strict';
2
3
4/**
5 * 用于获取目标路径的文件或目录相关信息
6 * @param {string} fileOrDirPath 目标路径
7 */
8let pathInfo = (fileOrDirPath)=>{
9 const {fs,path,tip} = {
10 fs:require('fs'),
11 path:require('path'),
12 tip:require('./tip')
13 };
14
15 let info = {
16 'type':undefined, //类型 'dir'|'file'
17 'extension':undefined, //扩展名 'undefined|空|.xxx'
18 'name':undefined //文件名 不包含扩展名部分
19 };
20
21 try {
22 let stat = fs.statSync(fileOrDirPath);
23
24 //如果路径是一个目录,则返回目录信息
25 if(stat.isDirectory()){
26 info.type = 'dir';
27
28 let backPath = path.resolve(fileOrDirPath,'../'), //跳到路径上一级目录
29 dirName = fileOrDirPath.replace(backPath,''), //去除上级目录路径
30 re = /[/]|[\\]/g;
31
32 info.name = dirName.replace(re,''); //去除处理路径后的/\符号
33 };
34
35 //如果是文件则返回文件信息
36 if(stat.isFile()){
37 info.type = 'file';
38 info.extension = path.extname(fileOrDirPath);
39 info.name = path.basename(fileOrDirPath,info.extension);
40 };
41 } catch (error) {
42 //tip.error(error);
43 };
44
45 return info;
46};
47
48module.exports = pathInfo;
49
50
51