|
| 1 | +import { Command } from 'commander'; |
| 2 | +import Z from 'zod'; |
| 3 | +import Ora from 'ora'; |
| 4 | +import { createLockfileProcessor } from '../workers/lockfile'; |
| 5 | +import { bucketTypeSchema } from '@replexica/spec'; |
| 6 | +import { loadConfig } from '../workers/config'; |
| 7 | +import { createBucketLoader, expandPlaceholderedGlob } from '../workers/bucket/v2'; |
| 8 | + |
| 9 | +export default new Command() |
| 10 | + .command('lockfile') |
| 11 | + .description('Create a lockfile if it does not exist') |
| 12 | + .helpOption('-h, --help', 'Show help') |
| 13 | + .option('-f, --force', 'Force create a lockfile') |
| 14 | + .action(async (options) => { |
| 15 | + const flags = flagsSchema.parse(options); |
| 16 | + const ora = Ora().start('Creating lockfile'); |
| 17 | + |
| 18 | + const lockfileProcessor = createLockfileProcessor(); |
| 19 | + const lockfileExists = await lockfileProcessor.exists(); |
| 20 | + if (lockfileExists && !flags.force) { |
| 21 | + ora.warn(`Lockfile won't be created because it already exists. Use --force to overwrite.`); |
| 22 | + return; |
| 23 | + } else { |
| 24 | + await lockfileProcessor.delete(); |
| 25 | + } |
| 26 | + |
| 27 | + const i18nConfig = await loadConfig(); |
| 28 | + |
| 29 | + const placeholderedPathsTuples: [Z.infer<typeof bucketTypeSchema>, string][] = []; |
| 30 | + for (const [bucketType, bucketTypeParams] of Object.entries(i18nConfig?.buckets || {})) { |
| 31 | + const includedPlaceholderedPaths = bucketTypeParams.include |
| 32 | + .map((placeholderedGlob) => expandPlaceholderedGlob(placeholderedGlob, i18nConfig!.locale.source)) |
| 33 | + .flat(); |
| 34 | + const excludedPlaceholderedPaths = bucketTypeParams.exclude |
| 35 | + ?.map((placeholderedGlob) => expandPlaceholderedGlob(placeholderedGlob, i18nConfig!.locale.source)) |
| 36 | + .flat() || []; |
| 37 | + const finalPlaceholderedPaths = includedPlaceholderedPaths.filter((path) => !excludedPlaceholderedPaths.includes(path)); |
| 38 | + for (const placeholderedPath of finalPlaceholderedPaths) { |
| 39 | + placeholderedPathsTuples.push([ |
| 40 | + bucketType as Z.infer<typeof bucketTypeSchema>, |
| 41 | + placeholderedPath |
| 42 | + ]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + for (const [bucketType, placeholderedPath] of placeholderedPathsTuples) { |
| 47 | + const sourcePayload = await createBucketLoader({ |
| 48 | + bucketType, |
| 49 | + placeholderedPath, |
| 50 | + locale: i18nConfig!.locale.source, |
| 51 | + }).load(); |
| 52 | + |
| 53 | + const currentChecksums = await lockfileProcessor.createChecksums(sourcePayload); |
| 54 | + await lockfileProcessor.saveChecksums(placeholderedPath, currentChecksums); |
| 55 | + } |
| 56 | + |
| 57 | + ora.succeed('Lockfile created'); |
| 58 | + }); |
| 59 | + |
| 60 | +const flagsSchema = Z.object({ |
| 61 | + force: Z.boolean().default(false), |
| 62 | +}); |
0 commit comments