|
| 1 | +import { Command } from "interactive-commander"; |
| 2 | +import _ from "lodash"; |
| 3 | +import Ora from "ora"; |
| 4 | +import { getConfig } from "../utils/config"; |
| 5 | +import { getBuckets } from "../utils/buckets"; |
| 6 | +import { resolveOverriddenLocale } from "@lingo.dev/_spec"; |
| 7 | +import createBucketLoader from "../loaders"; |
| 8 | +import { minimatch } from "minimatch"; |
| 9 | +import { confirm } from "@inquirer/prompts"; |
| 10 | + |
| 11 | +interface PurgeOptions { |
| 12 | + bucket?: string[]; |
| 13 | + file?: string[]; |
| 14 | + key?: string; |
| 15 | + locale?: string[]; |
| 16 | + yesReally?: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +export default new Command() |
| 20 | + .command("purge") |
| 21 | + .description( |
| 22 | + "Remove translations for given --bucket, --file, --key, --locale", |
| 23 | + ) |
| 24 | + .helpOption("-h, --help", "Show help") |
| 25 | + .option( |
| 26 | + "--bucket <bucket>", |
| 27 | + "Bucket to process", |
| 28 | + (val: string, prev: string[]) => (prev ? [...prev, val] : [val]), |
| 29 | + ) |
| 30 | + .option( |
| 31 | + "--file [files...]", |
| 32 | + "File(s) to process. Only process files that match the given glob pattern(s).", |
| 33 | + ) |
| 34 | + .option( |
| 35 | + "--key <key>", |
| 36 | + "Key to remove. Remove all translation keys matching the given glob pattern.", |
| 37 | + ) |
| 38 | + .option( |
| 39 | + "--locale <locale>", |
| 40 | + "Locale to process", |
| 41 | + (val: string, prev: string[]) => (prev ? [...prev, val] : [val]), |
| 42 | + ) |
| 43 | + .option( |
| 44 | + "--yes-really", |
| 45 | + "Skip interactive confirmation and delete without asking.", |
| 46 | + ) |
| 47 | + .action(async function (options: PurgeOptions) { |
| 48 | + const ora = Ora(); |
| 49 | + try { |
| 50 | + ora.start("Loading configuration..."); |
| 51 | + const i18nConfig = getConfig(); |
| 52 | + if (!i18nConfig) { |
| 53 | + throw new Error("i18n.json not found. Please run `lingo.dev init`."); |
| 54 | + } |
| 55 | + ora.succeed("Configuration loaded"); |
| 56 | + |
| 57 | + let buckets = getBuckets(i18nConfig); |
| 58 | + if (options.bucket && options.bucket.length) { |
| 59 | + buckets = buckets.filter((bucket) => |
| 60 | + options.bucket!.includes(bucket.type), |
| 61 | + ); |
| 62 | + } |
| 63 | + if (options.file && options.file.length) { |
| 64 | + buckets = buckets |
| 65 | + .map((bucket) => { |
| 66 | + const paths = bucket.paths.filter((bucketPath) => |
| 67 | + options.file?.some((f) => bucketPath.pathPattern.includes(f)), |
| 68 | + ); |
| 69 | + return { ...bucket, paths }; |
| 70 | + }) |
| 71 | + .filter((bucket) => bucket.paths.length > 0); |
| 72 | + if (buckets.length === 0) { |
| 73 | + ora.fail("All files were filtered out by --file option."); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + } |
| 77 | + const sourceLocale = i18nConfig.locale.source; |
| 78 | + const targetLocales = |
| 79 | + options.locale && options.locale.length |
| 80 | + ? options.locale |
| 81 | + : i18nConfig.locale.targets; |
| 82 | + let removedAny = false; |
| 83 | + for (const bucket of buckets) { |
| 84 | + console.log(); |
| 85 | + ora.info(`Processing bucket: ${bucket.type}`); |
| 86 | + for (const bucketPath of bucket.paths) { |
| 87 | + for (const _targetLocale of targetLocales) { |
| 88 | + const targetLocale = resolveOverriddenLocale( |
| 89 | + _targetLocale, |
| 90 | + bucketPath.delimiter, |
| 91 | + ); |
| 92 | + const bucketOra = Ora({ indent: 2 }).start( |
| 93 | + `Processing path: ${bucketPath.pathPattern} [${targetLocale}]`, |
| 94 | + ); |
| 95 | + try { |
| 96 | + const bucketLoader = createBucketLoader( |
| 97 | + bucket.type, |
| 98 | + bucketPath.pathPattern, |
| 99 | + { |
| 100 | + defaultLocale: sourceLocale, |
| 101 | + injectLocale: bucket.injectLocale, |
| 102 | + }, |
| 103 | + bucket.lockedKeys, |
| 104 | + bucket.lockedPatterns, |
| 105 | + bucket.ignoredKeys, |
| 106 | + ); |
| 107 | + await bucketLoader.init(); |
| 108 | + bucketLoader.setDefaultLocale(sourceLocale); |
| 109 | + await bucketLoader.pull(sourceLocale); |
| 110 | + let targetData = await bucketLoader.pull(targetLocale); |
| 111 | + if (!targetData || Object.keys(targetData).length === 0) { |
| 112 | + bucketOra.info( |
| 113 | + `No translations found for ${bucketPath.pathPattern} [${targetLocale}]`, |
| 114 | + ); |
| 115 | + continue; |
| 116 | + } |
| 117 | + let newData = { ...targetData }; |
| 118 | + let keysToRemove: string[] = []; |
| 119 | + if (options.key) { |
| 120 | + // minimatch for key patterns |
| 121 | + keysToRemove = Object.keys(newData).filter((k) => |
| 122 | + minimatch(k, options.key!), |
| 123 | + ); |
| 124 | + } else { |
| 125 | + // No key specified: remove all keys |
| 126 | + keysToRemove = Object.keys(newData); |
| 127 | + } |
| 128 | + if (keysToRemove.length > 0) { |
| 129 | + // Show what will be deleted |
| 130 | + if (options.key) { |
| 131 | + bucketOra.info( |
| 132 | + `About to delete ${keysToRemove.length} key(s) matching '${options.key}' from ${bucketPath.pathPattern} [${targetLocale}]:\n ${keysToRemove.slice(0, 10).join(", ")}${keysToRemove.length > 10 ? ", ..." : ""}`, |
| 133 | + ); |
| 134 | + } else { |
| 135 | + bucketOra.info( |
| 136 | + `About to delete all (${keysToRemove.length}) keys from ${bucketPath.pathPattern} [${targetLocale}]`, |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + if (!options.yesReally) { |
| 141 | + bucketOra.warn( |
| 142 | + "This is a destructive operation. If you are sure, type 'y' to continue. (Use --yes-really to skip this check.)", |
| 143 | + ); |
| 144 | + const confirmed = await confirm({ |
| 145 | + message: `Delete these keys from ${bucketPath.pathPattern} [${targetLocale}]?`, |
| 146 | + default: false, |
| 147 | + }); |
| 148 | + if (!confirmed) { |
| 149 | + bucketOra.info("Skipped by user."); |
| 150 | + continue; |
| 151 | + } |
| 152 | + } |
| 153 | + for (const key of keysToRemove) { |
| 154 | + delete newData[key]; |
| 155 | + } |
| 156 | + removedAny = true; |
| 157 | + await bucketLoader.push(targetLocale, newData); |
| 158 | + if (options.key) { |
| 159 | + bucketOra.succeed( |
| 160 | + `Removed ${keysToRemove.length} key(s) matching '${options.key}' from ${bucketPath.pathPattern} [${targetLocale}]`, |
| 161 | + ); |
| 162 | + } else { |
| 163 | + bucketOra.succeed( |
| 164 | + `Removed all keys (${keysToRemove.length}) from ${bucketPath.pathPattern} [${targetLocale}]`, |
| 165 | + ); |
| 166 | + } |
| 167 | + } else if (options.key) { |
| 168 | + bucketOra.info( |
| 169 | + `No keys matching '${options.key}' found in ${bucketPath.pathPattern} [${targetLocale}]`, |
| 170 | + ); |
| 171 | + } else { |
| 172 | + bucketOra.info("No keys to remove."); |
| 173 | + } |
| 174 | + } catch (error) { |
| 175 | + const err = error as Error; |
| 176 | + bucketOra.fail(`Failed: ${err.message}`); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + if (!removedAny) { |
| 182 | + ora.info("No keys were removed."); |
| 183 | + } else { |
| 184 | + ora.succeed("Purge completed."); |
| 185 | + } |
| 186 | + } catch (error) { |
| 187 | + const err = error as Error; |
| 188 | + ora.fail(err.message); |
| 189 | + process.exit(1); |
| 190 | + } |
| 191 | + }); |
0 commit comments