|
1 | | -import { Command } from '@oclif/core' |
2 | | -import * as path from 'path' |
| 1 | +import { Command, Flags } from '@oclif/core' |
| 2 | +import { existsSync, mkdirSync } from 'fs' |
| 3 | +import { join, resolve } from 'path' |
3 | 4 | import * as chalk from 'chalk' |
| 5 | +import { ViteConfigBuilder, ViteRunner } from '../../lib/vite' |
| 6 | +import * as ora from 'ora' |
4 | 7 |
|
5 | 8 | export default class Bundle extends Command { |
6 | | - static description = 'bundles your connector package (Note: This command is not yet available for customers)' |
| 9 | + static examples = [ |
| 10 | + '<%= config.bin %> <%= command.id %> ./example-connector', |
| 11 | + '<%= config.bin %> <%= command.id %> ./example-connector --output ./bundled', |
| 12 | + '<%= config.bin %> <%= command.id %> --input ./dist --output ./bundle' |
| 13 | + ] |
| 14 | + |
| 15 | + static flags = { |
| 16 | + help: Flags.help({ char: 'h' }), |
| 17 | + input: Flags.string({ |
| 18 | + char: 'i', |
| 19 | + description: 'input directory containing built connector files', |
| 20 | + default: '.' |
| 21 | + }), |
| 22 | + output: Flags.string({ |
| 23 | + char: 'o', |
| 24 | + description: 'output directory for bundled files' |
| 25 | + }), |
| 26 | + verbose: Flags.boolean({ |
| 27 | + char: 'v', |
| 28 | + description: 'verbose output', |
| 29 | + default: false |
| 30 | + }), |
| 31 | + watch: Flags.boolean({ |
| 32 | + char: 'w', |
| 33 | + description: 'watch for changes and rebuild', |
| 34 | + default: false |
| 35 | + }) |
| 36 | + } |
7 | 37 |
|
8 | 38 | static args = [ |
9 | | - { name: 'connectorDirectory', default: '.', description: 'connector path where configuration exists' } |
| 39 | + { |
| 40 | + name: 'path', |
| 41 | + description: 'path to connector directory (will use dist/ folder inside)' |
| 42 | + } |
10 | 43 | ] |
11 | 44 |
|
12 | | - static examples = [ |
13 | | - '$ zcli connectors:bundle .', |
14 | | - '$ zcli connectors:bundle ./connector1' |
15 | | - ] |
| 45 | + async run (): Promise<void> { |
| 46 | + const { args, flags } = await this.parse(Bundle) |
| 47 | + |
| 48 | + let inputPath: string |
| 49 | + if (args.path) { |
| 50 | + inputPath = resolve(join(args.path, 'src')) |
| 51 | + } else { |
| 52 | + inputPath = resolve(flags.input) |
| 53 | + } |
| 54 | + |
| 55 | + const outputPath = flags.output ? resolve(flags.output) : resolve('dist') |
| 56 | + if (!existsSync(outputPath)) { |
| 57 | + mkdirSync(outputPath, { recursive: true }) |
| 58 | + } |
| 59 | + |
| 60 | + const spinner = ora( |
| 61 | + `Bundling connector from ${inputPath} to ${outputPath}...` |
| 62 | + ).start() |
| 63 | + |
| 64 | + try { |
| 65 | + await this.generateViteBundle(inputPath, outputPath, flags, spinner) |
| 66 | + |
| 67 | + if (flags.watch) { |
| 68 | + spinner.succeed( |
| 69 | + chalk.green('Watching for changes... (Press Ctrl+C to stop)') |
| 70 | + ) |
| 71 | + } else { |
| 72 | + spinner.succeed(chalk.green('Bundle created successfully!')) |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + spinner.fail(chalk.red('Failed to bundle the connector')) |
| 76 | + |
| 77 | + if (error instanceof Error) { |
| 78 | + this.log('\n' + chalk.red('Error Details:')) |
| 79 | + this.log(error.message) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private async generateViteBundle ( |
| 85 | + inputPath: string, |
| 86 | + outputPath: string, |
| 87 | + flags: { watch: boolean }, |
| 88 | + spinner: ora.Ora |
| 89 | + ): Promise<void> { |
| 90 | + const { watch } = flags |
| 91 | + const viteConfig = ViteConfigBuilder.createConfig( |
| 92 | + { |
| 93 | + inputPath, |
| 94 | + outputPath, |
| 95 | + useLocalWorkspace: false, |
| 96 | + watch |
| 97 | + }, |
| 98 | + this |
| 99 | + ) |
| 100 | + |
| 101 | + spinner.text = watch |
| 102 | + ? 'Building connector and watching for changes...' |
| 103 | + : 'Building connector...' |
| 104 | + const stats = await ViteRunner.run(viteConfig) |
| 105 | + |
| 106 | + if (stats.hasErrors()) { |
| 107 | + spinner.fail(chalk.red('Bundle failed with errors!')) |
16 | 108 |
|
17 | | - async run () { |
18 | | - const { args } = await this.parse(Bundle) |
19 | | - const { connectorDirectory } = args |
| 109 | + const errors = stats.toJson().errors || [] |
| 110 | + errors.forEach((error: any) => { |
| 111 | + this.log(chalk.red(`Error: ${error.message}`)) |
| 112 | + }) |
20 | 113 |
|
21 | | - const connectorPath = path.resolve(connectorDirectory) |
| 114 | + throw new Error('Connector build failed') |
| 115 | + } |
22 | 116 |
|
23 | | - this.log(chalk.yellow(`Bundling connector from: ${connectorPath}`)) |
24 | | - // Placeholder for actual bundling logic |
25 | | - this.log(chalk.green('Connector bundle created successfully!')) |
| 117 | + if (stats.hasWarnings()) { |
| 118 | + const warnings = stats.toJson().warnings || [] |
| 119 | + this.log(chalk.yellow('\nWarnings:')) |
| 120 | + warnings.forEach((warning: any) => { |
| 121 | + this.log(chalk.yellow(` - ${warning.message}`)) |
| 122 | + }) |
| 123 | + } |
26 | 124 | } |
27 | 125 | } |
0 commit comments