Rename script files to match their script name (from metadata)

Open rename-script-files-to-match-their-name in Script Kit

// 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)
}
}