-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
174 lines (164 loc) · 3.95 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import path from 'node:path';
import fs from 'node:fs';
import alfy from 'alfy';
import {camelCase, pascalCase, sentenceCase, paramCase, capitalCase} from 'change-case';
import {upperCase} from 'upper-case';
import {lowerCase} from 'lower-case';
import {titleCase} from 'title-case';
import {
filterOutput,
findFilter,
isFileActionCaseSensitive,
getClipboardContent,
findCommand,
} from './src/utils.js';
const {inputWithoutFilter, foundFilter: filter} = findFilter(alfy.input);
const {inputWithoutCommand, foundCommand} = findCommand(inputWithoutFilter);
const input = (inputWithoutCommand || getClipboardContent())
.replaceAll('\t', '\n')
.split('\n')
.map(element => element.trim());
const options = [
{
prefix: titleCase('title case'),
match: 'titleCase',
action: line => titleCase(line),
},
{
prefix: upperCase('upper case'),
match: 'upperCase',
action: line => upperCase(line),
},
{
prefix: lowerCase('lower case'),
match: 'lowerCase',
action: line => lowerCase(line),
},
{
prefix: camelCase('camel case'),
match: 'camelCase',
action: line => camelCase(line),
},
{
prefix: pascalCase('pascal case'),
match: 'pascalCase',
action: line => pascalCase(line),
},
{
prefix: sentenceCase('sentence case'),
match: 'sentenceCase',
action: line => sentenceCase(line),
},
{
prefix: paramCase('param case'),
match: 'paramCase',
action: line => paramCase(line),
},
{
prefix: capitalCase('capital case'),
match: 'capitalCase',
action: line => capitalCase(line),
},
];
function run(input) {
if (isFileActionCaseSensitive(input) && !foundCommand) {
return [
{
title: 'File or Folder Name',
valid: false,
autocomplete: `!name ${input.join('\t')}`,
},
{
title: 'Extension',
valid: false,
autocomplete: `!ext ${input.join('\t')}`,
},
{
title: 'File or Folder Name and Extension',
valid: false,
autocomplete: `!both ${input.join('\t')}`,
},
];
}
const isFileAction = isFileActionCaseSensitive(input);
return options.map(options => {
const result = input.map(line => {
if (isFileAction) {
let inputBefore;
let inputAfter;
const dir = path.dirname(line);
const stat = fs.lstatSync(line);
if (stat.isDirectory()) {
if (foundCommand === 'ext') {
// ignore directories when processing only the file extension
return false;
}
const name = path.basename(line);
inputBefore = name;
inputAfter = options.action(name);
} else {
const {name} = path.parse(line); //=> "hello"
const ext = path.parse(line)?.ext?.slice(1); //=> ".html"
inputBefore = name + ext;
switch (foundCommand) {
case 'name':
inputAfter = options.action(name) + '.' + ext;
break;
case 'ext':
inputAfter = name + '.' + options.action(ext);
break;
default:
inputAfter = options.action(name) + '.' + options.action(ext);
break;
}
}
return {
inputBefore,
inputAfter,
before: line,
after: path.resolve(dir, inputAfter),
};
}
const inputAfter = options.action(line);
return {
inputBefore: line,
inputAfter,
before: line,
after: options.action(line),
};
}).filter(element => Boolean(element));
return {
title: `${options.prefix}: ${result.map(element => element.inputAfter).join('\n')}`,
subtitle: 'Copy to clipboard',
match: options.match,
arg: result.map(element => element.after),
mods: {
cmd: isFileAction
? {
subtitle: 'Rename file(s)',
arg: JSON.stringify(result),
variables: {
action: 'rename',
},
}
: {
subtitle: 'Paste case changed into front most app',
variables: {
action: 'paste',
},
},
},
};
});
}
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?',
},
]);
}