-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (99 loc) · 2.54 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
import process from 'node:process';
import path from 'node:path';
import alfy from 'alfy';
import pupa from 'pupa';
import {
filterOutput,
findFilter,
isFileActionCaseSensitive,
slugifyLine,
} from './src/utils.js';
const {inputWithoutFilter, foundFilter: filter} = findFilter(alfy.input);
const input = inputWithoutFilter
.replaceAll('\t', '\n')
.split('\n')
.map(element => element.trim());
const clipboard = process.env.text_to_add.trim();
const options = [
{
prefix: 'Prefix with Clipboard',
action: (line, text) => {
let slug = slugifyLine(text);
slug = (slug) ? slug + '-' : '';
return `${slug}${line}`;
},
},
{
prefix: 'Postfix with Clipboard',
action: (line, text) => {
const {name} = path.parse(line); //=> "hello"
const {ext} = path.parse(line); //=> ".html"
let slug = slugifyLine(text);
slug = (slug) ? '-' + slug : '';
return `${name}${slug}${ext}`;
},
},
{
prefix: 'Replace Variable with Clipboard',
action: (line, text) => pupa(line, {replace: text}),
},
{
prefix: 'Remove Clipboard Content from Filename',
action: (line, text) => line.replace(text, ''),
},
];
function run(input) {
if (!isFileActionCaseSensitive(input)) {
return [
{
title: 'Input must be valid and existing file(s) or folder(s)',
valid: false,
},
];
}
return options.map(option => {
const files = input.map(filepath => {
filepath = path.resolve(filepath);
const filenameBefore = path.basename(filepath);
const filenameAfter = option.action(filenameBefore, clipboard);
const filepathBefore = filepath;
const filepathAfter = path.resolve(path.dirname(filepath), filenameAfter);
return {
filenameBefore,
filenameAfter,
filepathBefore,
filepathAfter,
};
}).filter(element => element.filenameBefore !== element.filenameAfter);
return files.length > 0
? {
title: `${option.prefix}: ${files[0].filenameAfter}`,
subtitle: 'Copy to clipboard… Hold CMD Key to actually rename files.',
match: option.prefix,
valid: true,
arg: files.map(element => element.filenameAfter),
variables: {
action: 'copy',
},
mods: {
cmd: {
subtitle: 'Actually rename files!',
arg: JSON.stringify(files),
variables: {
action: 'rename',
},
},
},
}
: false;
}).filter(element => Boolean(element));
}
const output = run(input);
if (output.length > 0) {
alfy.output(filter ? filterOutput(filter, output) : output);
} else {
alfy.output([{
title: 'Nothing to process…',
subtitle: 'Maybe the clipboard is empty?',
}]);
}