Created
November 24, 2024 12:06
-
-
Save Strajk/fb52135b855faeaaa6aba9f34d1ef54f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Name: Rename script files to match their name | |
| // Description: Renames script file names to match their name (// Name: Foo Bar -> foo-bar.ts) | |
| import "@johnlindquist/kit" | |
| import { stripName } from "@johnlindquist/kit" | |
| import * as fs from "fs/promises" | |
| import dedent from "dedent" | |
| // just for documentation purposes | |
| let exampleScript = { | |
| "command": "rename-script-files-to-match-their-name", | |
| "filePath": "/Users/strajk/.kenv/scripts/rename-script-files-to-match-their-name.ts", | |
| "id": "/Users/strajk/.kenv/scripts/rename-script-files-to-match-their-name.ts", | |
| "name": "Rename script files to match their name", | |
| "timestamp": 1732375034783, | |
| "type": "Prompt" | |
| // ... | |
| } | |
| let scripts = await getScripts() | |
| for (let script of scripts) { | |
| let normalizedName = stripName(script.name) | |
| let extname = path.extname(script.filePath) | |
| let basename = path.basename(script.filePath, extname) | |
| if (basename !== script.command) { | |
| console.log(dedent` | |
| This should never happen, basename and command should be the same: | |
| basename: ${basename} | |
| command: ${script.command} | |
| `) | |
| notify(`basename: ${basename} command: ${script.command}`) | |
| exit() | |
| } | |
| if (normalizedName !== basename) { | |
| console.log(dedent` | |
| "${script.name}": | |
| current: ${basename} | |
| normalized: ${normalizedName} | |
| `) | |
| let confirmed = await arg({ | |
| placeholder: `Rename "${basename}" to "${normalizedName}"`, | |
| choices: [ | |
| { name: "Yes", value: true }, | |
| { name: "No", value: false } | |
| ] | |
| }) | |
| if (!confirmed) { | |
| continue | |
| } | |
| console.log(`Renaming "${basename}" to: ${normalizedName}`) | |
| let newPath = path.join(path.dirname(script.filePath), normalizedName + extname) | |
| await fs.rename(script.filePath, newPath) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment