|
| 1 | +import process from 'node:process'; |
| 2 | +import path from 'node:path'; |
| 3 | +import alfy from 'alfy'; |
| 4 | +import pupa from 'pupa'; |
| 5 | +import {findFilter, isFileAction, slugifyLine} from './src/utils.js'; |
| 6 | + |
| 7 | +const {inputWithoutFilter, foundFilter: filter} = findFilter(alfy.input); |
| 8 | +const input = inputWithoutFilter.replaceAll('\t', '\n'); |
| 9 | +const clipboard = process.env.text_to_add.trim(); |
| 10 | + |
| 11 | +const options = [ |
| 12 | + { |
| 13 | + prefix: 'Prefix with Clipboard', |
| 14 | + action: (line, text) => { |
| 15 | + let slug = slugifyLine(text); |
| 16 | + slug = (slug) ? slug + '-' : ''; |
| 17 | + return `${slug}${line}`; |
| 18 | + }, |
| 19 | + }, |
| 20 | + { |
| 21 | + prefix: 'Postfix with Clipboard', |
| 22 | + action: (line, text) => { |
| 23 | + const {name} = path.parse(line); //=> "hello" |
| 24 | + const {ext} = path.parse(line); //=> ".html" |
| 25 | + let slug = slugifyLine(text); |
| 26 | + slug = (slug) ? '-' + slug : ''; |
| 27 | + return `${name}${slug}${ext}`; |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + prefix: 'Replace Variable with Clipboard', |
| 32 | + action: (line, text) => pupa(line, {replace: text}), |
| 33 | + }, |
| 34 | + { |
| 35 | + prefix: 'Remove Clipboard Content from Filename', |
| 36 | + action: (line, text) => line.replace(text, ''), |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +function run(input) { |
| 41 | + if (isFileAction(input)) { |
| 42 | + return options.map(option => { |
| 43 | + const files = input.split('\n').map(filepath => { |
| 44 | + filepath = path.resolve(filepath); |
| 45 | + const filenameBefore = path.basename(filepath); |
| 46 | + const filenameAfter = option.action(filenameBefore, clipboard); |
| 47 | + const filepathBefore = filepath; |
| 48 | + const filepathAfter = path.resolve(path.dirname(filepath), filenameAfter); |
| 49 | + return { |
| 50 | + filenameBefore, |
| 51 | + filenameAfter, |
| 52 | + filepathBefore, |
| 53 | + filepathAfter, |
| 54 | + }; |
| 55 | + }).filter(element => element.filenameBefore !== element.filenameAfter); |
| 56 | + return files.length > 0 |
| 57 | + ? { |
| 58 | + title: `${option.prefix}: ${files[0].filenameAfter}`, |
| 59 | + subtitle: 'Copy to clipboard… Hold CMD Key to actually rename files.', |
| 60 | + match: option.prefix, |
| 61 | + valid: true, |
| 62 | + arg: files.map(element => element.filenameAfter), |
| 63 | + |
| 64 | + variables: { |
| 65 | + action: 'copy', |
| 66 | + }, |
| 67 | + mods: { |
| 68 | + cmd: { |
| 69 | + subtitle: 'Actually rename files!', |
| 70 | + arg: JSON.stringify(files), |
| 71 | + variables: { |
| 72 | + action: 'rename', |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + : false; |
| 78 | + }).filter(element => Boolean(element)); |
| 79 | + } |
| 80 | + |
| 81 | + return [ |
| 82 | + { |
| 83 | + title: 'Input must be valid and existing file(s) or folder(s)', |
| 84 | + valid: false, |
| 85 | + }, |
| 86 | + ]; |
| 87 | +} |
| 88 | + |
| 89 | +function filterOutput(filter, output) { |
| 90 | + const filterSplit = filter.split(' '); |
| 91 | + for (const filter of filterSplit) { |
| 92 | + output = alfy.matches(filter, output, 'match'); |
| 93 | + } |
| 94 | + |
| 95 | + return output; |
| 96 | +} |
| 97 | + |
| 98 | +const output = run(input); |
| 99 | +if (output.length > 0) { |
| 100 | + alfy.output(filter ? filterOutput(filter, output) : output); |
| 101 | +} else { |
| 102 | + alfy.output([{ |
| 103 | + title: 'Nothing to process…', |
| 104 | + subtitle: 'Maybe the clipboard is empty?', |
| 105 | + }]); |
| 106 | +} |
0 commit comments