-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
65 lines (57 loc) · 1.43 KB
/
cli.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
const fs = require('fs/promises');
const {
lstatSync
} = require('fs');
const yargs = require('yargs');
const path = require('path');
const inquirer = require('inquirer');
const options = yargs
.usage('Использование: -p <путь> -s <строка или паттерн>')
.option('p', {
alias: 'path',
default: process.cwd(),
describe: 'Путь до файла',
type: 'string',
})
.option('s', {
alias: 'search',
default: '',
describe: 'Найти строку или паттерн',
type: 'string',
})
.argv;
let currentDir = options.p ? options.p : process.cwd();
const regExp = new RegExp(options.s, 'gi');
const isFile = fileName => lstatSync(path.join(currentDir, fileName)).isFile();
const run = async () => {
const list = await fs.readdir(currentDir);
const item = await inquirer
.prompt([{
name: "fileName",
type: "list",
message: "Выберите файл или папку: ",
choices: list,
}, ])
.then(({
fileName
}) => {
return {
name: fileName,
path: path.join(currentDir, fileName)
}
});
if (isFile(item.name)) {
const data = await fs.readFile(item.path, 'utf-8');
if (!options.s) {
console.log(`Содержимое файла:
${data}`);
} else {
const arr = data.match(regExp);
console.log(`Количество найденных совпадений: ${arr.length}`)
}
} else {
currentDir = item.path;
return await run();
}
}
run();