|
| 1 | +/** |
| 2 | + * @jrmc/adonis-etl |
| 3 | + * |
| 4 | + * @license MIT |
| 5 | + * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr> |
| 6 | + */ |
| 7 | + |
| 8 | +// import { stubsRoot } from '../../stubs/main.js' |
| 9 | +import { args, BaseCommand } from '@adonisjs/core/ace' |
| 10 | +import string from '@adonisjs/core/helpers/string' |
| 11 | +import { stubsRoot } from '../../stubs/main.js' |
| 12 | + |
| 13 | +export default class MakeEtl extends BaseCommand { |
| 14 | + static commandName = 'make:etl' |
| 15 | + static description = 'Create a new ETL files (source, transform, destination)' |
| 16 | + |
| 17 | + @args.string({ description: 'Name of the ETL process' }) |
| 18 | + declare name: string |
| 19 | + |
| 20 | + async run() { |
| 21 | + // Ask which ETL components to create |
| 22 | + const components = await this.prompt.multiple('Which ETL components do you want to create?', [ |
| 23 | + 'Source', |
| 24 | + 'Transform', |
| 25 | + 'Destination' |
| 26 | + ], { |
| 27 | + validate: (value) => { |
| 28 | + if (!value || value.length === 0) { |
| 29 | + return 'You must select at least one component' |
| 30 | + } |
| 31 | + return true |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + // Ask for source details if source is selected |
| 36 | + let sourceDetails = null |
| 37 | + if (components.includes('Source') || components.includes('Transform')) { |
| 38 | + sourceDetails = await this.prompt.ask('What is the source type? (e.g., database, api, file)', { |
| 39 | + validate: (value) => { |
| 40 | + if (!value || value.trim().length === 0) { |
| 41 | + return 'Source type is required' |
| 42 | + } |
| 43 | + return true |
| 44 | + } |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + // Ask for destination details if destination is selected |
| 49 | + let destinationDetails = null |
| 50 | + if (components.includes('Destination') || components.includes('Transform')) { |
| 51 | + destinationDetails = await this.prompt.ask('What is the destination type? (e.g., database, api, file)', { |
| 52 | + validate: (value) => { |
| 53 | + if (!value || value.trim().length === 0) { |
| 54 | + return 'Destination type is required' |
| 55 | + } |
| 56 | + return true |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + const codemods = await this.createCodemods() |
| 62 | + let className = null |
| 63 | + |
| 64 | + // Create the selected components |
| 65 | + for (const component of components) { |
| 66 | + if (component === 'Source') { |
| 67 | + className = [ |
| 68 | + string.snakeCase(this.name), |
| 69 | + string.snakeCase(sourceDetails!), |
| 70 | + 'source', |
| 71 | + ].join('_') |
| 72 | + } else if (component === 'Destination') { |
| 73 | + className = [ |
| 74 | + string.snakeCase(this.name), |
| 75 | + string.snakeCase(destinationDetails!), |
| 76 | + 'destination', |
| 77 | + ].join('_') |
| 78 | + } else if (component === 'Transform') { |
| 79 | + className = [ |
| 80 | + string.snakeCase(this.name), |
| 81 | + string.snakeCase(sourceDetails!), |
| 82 | + 'to', |
| 83 | + string.snakeCase(destinationDetails!), |
| 84 | + 'transform', |
| 85 | + ].join('_') |
| 86 | + } |
| 87 | + |
| 88 | + const stubPath = `make/etl/${component.toLowerCase()}s/main.ts.stub` |
| 89 | + |
| 90 | + await codemods.makeUsingStub(stubsRoot, stubPath, { |
| 91 | + className |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + this.logger.success(`ETL files created successfully for: ${this.name}`) |
| 96 | + } |
| 97 | +} |
0 commit comments