Skip to content

Commit

Permalink
Display combined file size in output (#47696)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Dec 12, 2023
1 parent b9a49e6 commit 858d723
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/workflows/delete-orphan-translation-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,45 @@ main(program.args[0], {
})

function main(root: string, options: Options) {
let deleted = 0
let countInSync = 0
let countOrphan = 0
const deleted: number[] = []
const inSync: number[] = []
const orphan: number[] = []
for (const filePath of getContentAndDataFiles(root)) {
const relPath = path.relative(root, filePath)
const size = fs.statSync(filePath).size
if (!fs.existsSync(path.join(ROOT, relPath))) {
countOrphan++
if (deleted < options.max) {
orphan.push(size)
if (deleted.length < options.max) {
if (options.dryRun) {
console.log('DELETE', filePath)
} else {
fs.rmSync(filePath)
console.log('DELETED', filePath)
}
deleted++
deleted.push(size)

if (deleted >= options.max) {
if (deleted.length >= options.max) {
console.log(`Max. number (${options.max}) of files deleted`)
}
}
} else {
countInSync++
inSync.push(size)
}
}
console.log(`In conclusion, deleted ${deleted.toLocaleString()} files.`)
const sumDeleted = deleted.reduce((a, b) => a + b, 0)
console.log(
`There are ${countInSync.toLocaleString()} files in sync and ${countOrphan.toLocaleString()} orphan files in ${root}`,
`In conclusion, deleted ${deleted.length.toLocaleString()} files (${formatFileSize(
sumDeleted,
)}).`,
)
const sumInSync = inSync.reduce((a, b) => a + b, 0)
const sumOrphan = orphan.reduce((a, b) => a + b, 0)
console.log(
`There are ${inSync.length.toLocaleString()} files (${formatFileSize(
sumInSync,
)}) in sync and ${orphan.length.toLocaleString()} orphan files (${formatFileSize(
sumOrphan,
)}) in ${root}`,
)
}

Expand All @@ -82,3 +94,13 @@ function getContentAndDataFiles(root: string) {
...walkFiles(path.join(root, 'data'), ['.md', '.yml']),
]
}

function formatFileSize(bytes: number) {
if (bytes < 1024) {
return `${bytes} B`
}
if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(1)} kB`
}
return `${(bytes / 1024 / 1024).toFixed(1)} MB`
}

0 comments on commit 858d723

Please sign in to comment.