-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (94 loc) · 2.36 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
import fs from 'node:fs';
import path from 'node:path';
import alfy from 'alfy';
import {filterOutput, findFilter, isFileAction} from './src/utils.js';
const {inputWithoutFilter, foundFilter: filter} = findFilter(alfy.input);
const input = inputWithoutFilter
.replaceAll("\t", "\n")
.split("\n")
.map((element) => element.trim());
const options = [
{
prefix: 'Copy File Names',
match: 'copy file name names filenames filename files',
action: filepath => {
const stat = fs.lstatSync(filepath);
if (!stat.isFile()) {
return false;
}
const {name} = path.parse(filepath); //=> "hello"
const {ext} = path.parse(filepath); //=> ".html"
return `${name}${ext}`;
},
},
{
prefix: 'Copy File Names (no extension)',
match: 'copy file name names filenames filename files without no extension',
action: filepath => {
const stat = fs.lstatSync(filepath);
if (!stat.isFile()) {
return false;
}
const {name} = path.parse(filepath); //=> "hello"
return `${name}`;
},
},
{
prefix: 'Copy Folder Names',
match: 'copy folder name names foldernames foldername folders',
action: filepath => {
const stat = fs.lstatSync(filepath);
if (!stat.isDirectory()) {
return false;
}
return path.basename(filepath);
},
},
{
prefix: 'Copy All Names',
match: 'copy all name names allnames',
action: filepath => path.basename(filepath),
},
{
prefix: 'Copy All Names (no file extension)',
match: 'copy all name names allnames without no extension',
action: filepath => {
const stat = fs.lstatSync(filepath);
if (stat.isDirectory()) {
return path.basename(filepath);
}
const {name} = path.parse(filepath); //=> "hello"
return `${name}`;
},
},
];
function run(input) {
if (!isFileAction(input)) {
return [
{
title: 'Input must be valid and existing file(s) or folder(s)',
valid: false,
},
];
}
return options.map(options => {
const files = input.map(filepath => options.action(filepath)).filter(element => Boolean(element));
return {
title: `${options.prefix}: ${files}`,
subtitle: 'Copy to clipboard',
match: options.match,
arg: files.join('\n'),
};
});
}
const output = run(input);
if (output.length > 0) {
alfy.output(filter ? filterOutput(filter, output) : output);
} else {
alfy.output([
{
title: 'Nothing to process…',
subtitle: 'No files selected?',
},
]);
}