1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-25 19:34:48 +01:00
blog/scripts/move-downloads.ts
Matthias Kretschmann 05ad0470c2
migrate to biome (#944)
* migrate to biome

* cleanup

* fix

* use tsx

* script tweaks

* fix test runs

* path tweaks
2024-07-27 21:15:05 +01:00

73 lines
1.9 KiB
TypeScript

//
// Find all zip files in all article folders
// and copy them to public/get/ folder.
//
import fs from 'node:fs'
import path from 'node:path'
import chalk from 'chalk'
import { globby } from 'globby'
import ora, { type Ora } from 'ora'
const sourceFolder = './content/articles/'
const destinationFolder = './public/get/'
const filesGlob = '**/*.zip'
const spinner = ora(
`${chalk.bold('[move-downloads]')} Finding and moving zip files`
).start()
function removeFolderContents(folderPath: string) {
if (fs.existsSync(folderPath)) {
for (const file of fs.readdirSync(folderPath)) {
const filePath = path.join(folderPath, file)
if (fs.lstatSync(filePath).isDirectory()) {
removeFolderContents(filePath)
fs.rmSync(filePath)
} else {
fs.unlinkSync(filePath)
}
}
}
}
export async function copyZipFiles(
source: string,
destination: string,
spinner: Ora
) {
// Clean out the destination folder
removeFolderContents(destination)
// Create the destination folder if it doesn't exist
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination, { recursive: true })
}
// Find all files recursively in the source folder
const zipFiles = await globby(filesGlob, { cwd: source })
for (const zipFile of zipFiles) {
const sourcePath = path.join(source, zipFile)
const destinationPath = path.join(destination, path.basename(zipFile))
try {
// Copy the file to the destination folder
fs.copyFileSync(sourcePath, destinationPath)
} catch (error: unknown) {
spinner.fail(
`${chalk.bold('[move-downloads]')} Error copying ${zipFile}: ${
(error as Error).message
}`
)
}
}
spinner.succeed(
`${chalk.bold('[move-downloads]')} Copied ${
zipFiles.length
} .zip files to ${destination}`
)
}
await copyZipFiles(sourceFolder, destinationFolder, spinner)