Unable to rename files #1181
-
I have a little script that I wrote and which aims to do the following :
For some reason, the Here is the code, any help appreciated as always : // Exclude: true
import "@johnlindquist/kit"
function buildSectionFilename(index: number) {
return `section-${index < 10 ? `0${index}` : index.toString()}.mdx`
}
const currentPath = cwd()
const lessonFiles = await readdir(currentPath)
if (!lessonFiles.includes("index.md")) {
console.log("❌ Not in a lesson directory")
// TODO also check MD attributes
exit(1)
}
const sections = lessonFiles.filter((file) => file.startsWith("section-"))
const index = Number(await arg("Section to remove:"))
if (!index || index < 1 || index > sections.length) {
console.log("❌ Invalid section index")
exit(1)
}
// Remove the section
const section = sections[index - 1]
const sectionPath = path.join(currentPath, section)
await rm(sectionPath)
console.log("✅ Removed section: " + section)
// Rename all sections following the index
for (let i = index; i < sections.length; i++) {
const section = sections[i]
const newSectionId = i
const newSectionFilename = buildSectionFilename(newSectionId)
mv(section, newSectionFilename)
console.log("- Renamed section: " + section + " -> " + newSectionFilename)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@Alarid So I thought I'd take a stab at the idea before reading your solution just in case the way I approach it would be helpful: // Name: Section Manager
import "@johnlindquist/kit"
let sectionPath = home("dev", "testing-section-manager")
let files = await readdir(sectionPath)
if (files.length === 0) {
let count = 20
for await (let i of _.range(count)) {
let fileName = `section-${i.toString().padStart(2, "0")}.mdx`
let filePath = path.resolve(sectionPath, fileName)
await writeFile(filePath, `# Section ${i}`)
}
files = await readdir(sectionPath)
}
let selectedFile = await arg("Select file to remove", files)
let selectedFilePath = path.resolve(sectionPath, selectedFile)
await rm(selectedFilePath)
files = await readdir(sectionPath)
// Determine which files > than the removed file need to be renamed
let filesToRename = files.filter(file => {
let fileNumber = parseInt(file.split("-")[1])
let selectedFileNumber = parseInt(selectedFile.split("-")[1])
return fileNumber > selectedFileNumber
})
// debug filesToRename
let yn = await arg(
{
placeholder: "Rename files down one?",
hint: `[y]es/[n]o`,
},
filesToRename
)
if (yn === "y") {
// Rename the files down one
for await (let file of filesToRename) {
// You could also try "rename" if mv doesn't work for you
// let { rename } = await import("fs/promises")
// await rename(
mv(
path.resolve(sectionPath, file),
path.resolve(
sectionPath,
file.replace(/\d+/, match => {
let number = parseInt(match)
return (number - 1).toString().padStart(2, "0")
})
)
)
}
files = await readdir(sectionPath)
await arg(
"Verify files have been renamed",
files.map(f => {
return {
name: f,
preview: async () => {
return md(await readFile(path.resolve(sectionPath, f), "utf8"))
},
}
})
)
} I'm not sure in your solution that You can try using I personally never use the shelljs
or the built-in node helpers. |
Beta Was this translation helpful? Give feedback.
@Alarid Does this work for you?