-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move-downloads.ts
70 lines (60 loc) · 1.75 KB
/
move-downloads.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Finds all zip files in all article folders,
// and then copies them to public/get/.
//
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.rmSync(filePath)
} else {
fs.unlinkSync(filePath)
}
})
}
}
export async function copyZipFiles(
source: string,
destination: string,
spinner: Ora
) {
removeFolderContents(destination)
// Creates the destination folder (if it doesn't exist).
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination, { recursive: true })
}
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 {
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)