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 c123dd9d68
Switch to astro-redirect-from (#830)
* astro-redirect-from prototype

* move out plugin

* switch to astro-redirect-from

* fixes

* new post: astro-redirect-from

* fix dependency

* test fixes

* downgrade and lock astro

* until fix for https://github.com/withastro/astro/issues/8649 has been released

* mention debug json file

* fix e2e test
2023-09-23 21:32:18 +01:00

74 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 globby from 'globby'
import ora, { type Ora } from 'ora'
import chalk from 'chalk'
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)) {
fs.readdirSync(folderPath).forEach((file) => {
const filePath = path.join(folderPath, file)
if (fs.lstatSync(filePath).isDirectory()) {
removeFolderContents(filePath)
fs.rmdirSync(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 })
zipFiles.forEach((zipFile: string) => {
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: any) {
spinner.fail(
`${chalk.bold('[move-downloads]')} Error copying ${zipFile}: ${
(error as Error).message
}`
)
return
}
})
spinner.succeed(
`${chalk.bold('[move-downloads]')} Copied ${
zipFiles.length
} .zip files to ${destination}`
)
}
await copyZipFiles(sourceFolder, destinationFolder, spinner)