-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (115 loc) · 3.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const fg = require("fast-glob");
const path = require("path");
const os = require("os");
const fse = require("fs-extra");
const arvish = require("arvish");
const prettyBytes = require("pretty-bytes");
const { getIcon, getRootDir } = require("./utils");
const pluginConf = {
include: JSON.parse(process.env.include),
customInclude: JSON.parse(process.env.customInclude),
exclude: JSON.parse(process.env.exclude),
maxItem: Number(process.env.maxItem),
deep: Number(process.env.deep),
includeDotFiles: Boolean(process.env.includeDotFiles),
};
const sep = path.sep;
const getPluginItems = async ({ inputStr }) => {
if (inputStr === "")
return {
items: [],
};
const configItems = [];
return new Promise((resolve, reject) => {
const globOpts = {
absolute: true,
caseSensitiveMatch: false,
cwd: getRootDir(),
deep: pluginConf.deep,
dot: pluginConf.includeDotFiles,
markDirectories: true,
onlyDirectories: false,
onlyFiles: false,
suppressErrors: true,
concurrency: pluginConf.concurrency
? pluginConf.concurrency
: os.cpus().length,
};
let targetPaths = [
...pluginConf.customInclude.map((filePath) => `${filePath}${sep}*${inputStr}*`),
...pluginConf.customInclude.map(
(filePath) => `${filePath}${sep}**${sep}*${inputStr}*`
),
...pluginConf.include.map((platformEnvKey) => `${arvish.env.platform[platformEnvKey]}${sep}*${inputStr}*`),
...pluginConf.include.map(
(platformEnvKey) => `${arvish.env.platform[platformEnvKey]}${sep}**${sep}*${inputStr}*`
),
...pluginConf.exclude.map((filePath) => `!${filePath}`),
];
if (process.platform === "win32") {
targetPaths = targetPaths.map((targetPath) =>
targetPath.split("\\").join("/")
);
} else {
targetPaths = targetPaths.map((targetPath) => path.resolve(targetPath));
}
const getFileOrDirName = (filePath) => {
return filePath.endsWith("/")
? filePath
.substring(0, filePath.length - 1)
.split("/")
.pop()
: filePath.split("/").pop();
};
fg(targetPaths, globOpts)
.then(async (files) => {
const items = files.map((filePath) => {
const fileName = getFileOrDirName(filePath);
return {
title: fileName,
subtitle: filePath,
arg: filePath,
fileName,
filePath,
};
});
await Promise.all(
items.map(async (item) => {
item.icon = {
path: await getIcon(item.fileName, item.filePath)
};
if (!item.fileName.endsWith('pdf')) {
item.quicklook = {
type: 'markdown',
data: new Promise(async (resolve, reject) => {
const fileInfo = await fse.lstat(item.filePath);
const imgPath = path.isAbsolute(item.icon.path) ?
(item.icon.path).split(' ').join(' ') :
path.resolve(__dirname, item.icon.path);
resolve(
`## ${item.fileName}
<p align="center">
<img src="${imgPath}" width="70%" />
</p>
***
##### Full Path: \`${item.filePath}\`
##### Size: \`${prettyBytes(fileInfo.size)}\`
##### Created: \`${fileInfo.birthtime.toLocaleString()}\`
##### Edited: \`${fileInfo.mtime.toLocaleString()}\`
`);
})
}
};
})
);
resolve({
items: [...items.slice(0, pluginConf.maxItem), ...configItems],
});
})
.catch((err) => {
console.error("arvis-filesearch-plugin throws an Error", err);
resolve({ items: [] });
});
});
};
module.exports = getPluginItems;