Skip to content

Commit

Permalink
small cleanup to index.js, started trying to figure out what a test w…
Browse files Browse the repository at this point in the history
…ould be lol
  • Loading branch information
elliotberry committed Jun 17, 2024
1 parent dc200b7 commit aa9fb3d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 43 deletions.
95 changes: 52 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,69 @@ import filterInputs from "./lib/filters.js"
import getContent from "./lib/get-content.js"

const argv = yargs(hideBin(process.argv))
.option("dryrun", {
alias: "d",
default: false,
description: "Dry run: get new filenames, but don't rename files",
type: "boolean",
})

.option("dryrun", {
alias: "d",
default: false,
description: "Dry run: get new filenames, but don't rename files",
type: "boolean"
})
.help().argv
.help().argv

const main = async (input) => {
if (!process.env.CF_ENDPOINT || !process.env.CF_AI_TOKEN) {
console.error("Please set the CF_ENDPOINT and CF_AI_TOKEN environment variables.")
process.exit(1)
}
if (!process.env.CF_ENDPOINT || !process.env.CF_AI_TOKEN) {
console.error("Please set the CF_ENDPOINT and CF_AI_TOKEN environment variables, or else, shit, we've got nothing to query.")
process.exit(1)
}

const inputs = await validateAndFormatInput(input)

const initialLength = inputs.length

const inputs = await validateAndFormatInput(input)
const filteredInputs = await filterInputs(inputs)

const initialLength = inputs.length
const finalLength = filteredInputs.length
const filteredAmount = initialLength - filteredInputs.length
filteredAmount > 0 &&
console.log(`Filtered ${filteredAmount} non-img files for a total of ${finalLength} files.`)

const filteredInputs = await filterInputs(inputs)
if (argv.dryrun) {
console.log("Querying API for new names...")
} else {
console.log("Renaming files...")
}
let index = 0

const finalLength = filteredInputs.length
const filteredAmount = initialLength - filteredInputs.length
filteredAmount > 0 && console.log(`Filtered ${filteredAmount} non-img files for a total of ${finalLength} files.`)

if (argv.dryrun) {
console.log("Querying API for new names...");
} else {
console.log("Renaming files...");
}
let index = 0
for await (const file of filteredInputs) {
try {
const content = await getContent(file)

for await (const file of filteredInputs) {
try {
const content = await getContent(file)
const { filename, time } = await ask(content, file)

const { filename, time } = await ask(content, file)
const newFilename = path.join(path.dirname(file), `${filename}${path.extname(file)}`)
const finalFilename = await returnSafeFilePath(newFilename)
if (await exists(finalFilename)) {
//normally wouldn't happen, but just to be safe.
console.warn(
chalk.red(
`File ${finalFilename} already exists, and we screwed up renaming it, skipping`
)
)
} else {
//simply rename
!argv.dryrun && (await fs.rename(file, finalFilename))

const newFilename = path.join(path.dirname(file), `${filename}${path.extname(file)}`)
const finalFilename = await returnSafeFilePath(newFilename)
if (await exists(finalFilename)) {
console.warn(chalk.red(`File ${finalFilename} already exists, and we screwed up renaming it, skipping`))
} else {
!argv.dryrun && await fs.rename(file, finalFilename)
console.log(
chalk.green(`${argv.dryrun ? "This would rename " : "Renamed "}${truncateFilename(path.basename(file))} to ${path.basename(finalFilename)} (${time})`)
)
}
} catch (error) {
console.error(chalk.red(`Error processing file: ${error.toString()}`))
console.log(
chalk.green(
`${argv.dryrun === true ? "This would rename " : "Renamed "}${truncateFilename(path.basename(file))} to ${path.basename(finalFilename)} (${time})`
)
)
}
} catch (error) {
console.error(chalk.red(`Error processing file: ${error.toString()}`))
}
index++
}
index++
}
}

console.log(argv)
main(argv._[0])
41 changes: 41 additions & 0 deletions test.index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, it, beforeEach, afterEach, test } from "node:test"
import assert from "node:assert"

import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import exec from "elliotisms/lib/exec.js"

const originalImagePath = "./test.jpg"

describe("tests", async () => {
beforeEach(async (t) => {
t.tempFilePath = path.join(os.tmpdir(), "test.jpg")
await fs.promises.copyFile(originalImagePath, t.tempFilePath)
})

afterEach(async (t) => {
// await fs.promises.unlink(tempFilePath);
// if (newFileName) {
// await fs.promises.unlink(newFileName); // Delete the renamed file
// await fs.promises.copyFile(tempFilePath, originalImagePath); // Restore the original
// }
})

it("should run index.js, rename the image, and return no errors", async (t) => {

await exec(`node ./index.js "${t.tempFilePath}"`)

let filesInTMP = await fs.promises.readdir(os.tmpdir())

let hasTheRenamedFile =
filesInTMP.find(
(file) => file.endsWith(".jpg") && file !== "test.jpg" && file.indexOf("watermelon") > -1
) !== undefined
assert.ok(
hasTheRenamedFile,
"correct new JPG file was found after running index.js: " + filesInTMP
)

})
})

0 comments on commit aa9fb3d

Please sign in to comment.