1
0
mirror of synced 2025-12-19 18:10:59 -05:00

Create PR about orphaned features (#49827)

Co-authored-by: Robert Sese <734194+rsese@users.noreply.github.com>
This commit is contained in:
Peter Bengtsson
2024-03-29 16:13:52 -04:00
committed by GitHub
parent 9bbc99fd27
commit 4dc5cc8d87
3 changed files with 160 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
import languages from '@/languages/lib/languages.js'
type Options = {
verbose?: boolean
max: number
}
export async function deleteOrphans(filePath: string, options: Options) {
const orphans = JSON.parse(fs.readFileSync(filePath, 'utf8'))
if (!Array.isArray(orphans)) {
throw new Error(`Expected an array of orphans in ${filePath}`)
}
let count = 0
if (options.verbose) {
console.log(chalk.yellow(`${orphans.length} orphans found in ${filePath}`))
if (orphans.length > options.max) {
console.log(chalk.yellow(`Only deleting the first ${options.max} orphans`))
}
}
let countDeletions = 0
for (const orphan of orphans.slice(0, options.max)) {
count++
const absolutePath = path.join(languages.en.dir, orphan)
if (!fs.existsSync(absolutePath)) {
throw new Error(`File does not exist: ${absolutePath} (number ${count} in ${filePath})`)
}
if (options.verbose) {
console.log(chalk.green(`Deleting ${absolutePath}`))
}
fs.unlinkSync(absolutePath)
countDeletions++
}
if (countDeletions > 0) {
console.log(chalk.green(`Deleted ${countDeletions} orphans`))
} else {
console.log(chalk.yellow(`Deleted no orphans`))
}
}

View File

@@ -1,5 +1,6 @@
import { program } from 'commander'
import { find } from './find'
import { deleteOrphans } from './delete'
program
.name('find-orphaned-features')
@@ -15,4 +16,12 @@ program
.option('-v, --verbose', 'Verbose')
.action(find)
program
.command('delete')
.description('Delete features based on found orphans')
.option('-m, --max <number>', 'Maximum number of files to delete', (val) => parseInt(val), 10)
.option('-v, --verbose', 'Verbose')
.argument('<orphans-json-filepath>', 'path to the JSON file')
.action(deleteOrphans)
program.parse(process.argv)