-
Notifications
You must be signed in to change notification settings - Fork 28
feat(VEG-3430): Add the Connector bundle logic into the cli #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2822f16
16d1587
dea5d1c
5c56bb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,6 @@ | |
|
|
||
| # Shared | ||
| yarn.lock @zendesk/wattle @zendesk/vikings | ||
|
|
||
| # Connectors | ||
| /packages/zcli-connectors/ @zendesk/vegemite | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,164 @@ | ||
| import { Command } from '@oclif/core' | ||
| import * as path from 'path' | ||
| import { Command, Flags } from '@oclif/core' | ||
| import { existsSync, mkdirSync } from 'fs' | ||
| import { join, resolve } from 'path' | ||
| import * as chalk from 'chalk' | ||
| import { ViteConfigBuilder, ViteRunner } from '../../lib/vite' | ||
| import * as ora from 'ora' | ||
|
|
||
| export default class Bundle extends Command { | ||
| static description = 'bundles your connector package (Note: This command is not yet available for customers)' | ||
| static examples = [ | ||
| '<%= config.bin %> <%= command.id %> ./example-connector', | ||
| '<%= config.bin %> <%= command.id %> ./example-connector --output ./bundled', | ||
| '<%= config.bin %> <%= command.id %> --input ./src --output ./bundle' | ||
| ] | ||
|
|
||
saikambaiyyagari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static flags = { | ||
| help: Flags.help({ char: 'h' }), | ||
| input: Flags.string({ | ||
| char: 'i', | ||
| description: 'input directory containing connector source files', | ||
| default: '.' | ||
| }), | ||
| output: Flags.string({ | ||
| char: 'o', | ||
| description: 'output directory for bundled files' | ||
| }), | ||
| verbose: Flags.boolean({ | ||
| char: 'v', | ||
| description: 'verbose output', | ||
| default: false | ||
| }), | ||
saikambaiyyagari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| watch: Flags.boolean({ | ||
| char: 'w', | ||
| description: 'watch for changes and rebuild', | ||
| default: false | ||
| }) | ||
| } | ||
|
|
||
| static args = [ | ||
| { name: 'connectorDirectory', default: '.', description: 'connector path where configuration exists' } | ||
| { | ||
| name: 'path', | ||
| description: 'path to connector directory (will use src/ folder inside)' | ||
| } | ||
| ] | ||
|
|
||
| static examples = [ | ||
| '$ zcli connectors:bundle .', | ||
| '$ zcli connectors:bundle ./connector1' | ||
| ] | ||
| async run (): Promise<void> { | ||
| const { args, flags } = await this.parse(Bundle) | ||
|
|
||
| let inputPath: string | ||
| if (args.path) { | ||
| inputPath = resolve(join(args.path, 'src')) | ||
saikambaiyyagari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| inputPath = resolve(flags.input) | ||
| } | ||
|
|
||
| const outputPath = flags.output ? resolve(flags.output) : resolve('dist') | ||
| if (!existsSync(outputPath)) { | ||
| mkdirSync(outputPath, { recursive: true }) | ||
| if (flags.verbose) { | ||
| this.log(chalk.cyan(`Created output directory: ${outputPath}`)) | ||
| } | ||
| } | ||
|
|
||
| if (flags.verbose) { | ||
| this.log(chalk.cyan('Verbose mode enabled')) | ||
| this.log(chalk.cyan(`Resolved Input path: ${inputPath}`)) | ||
| this.log(chalk.cyan(`Resolved Output path: ${outputPath}`)) | ||
| this.log(chalk.cyan(`Watch mode: ${flags.watch ? 'enabled' : 'disabled'}`)) | ||
| } | ||
|
Comment on lines
+63
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be before the output directory creation?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are printing the resolved absolute paths for output and the input based on the flags or default values. The logic code path above these print statements. Can we leave it here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, not a blocker on my end. Just wanted to call out the output order would be: Which seems a bit odd. As a user I would expect:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed the labels like this for now: |
||
|
|
||
| const spinner = ora( | ||
| `Bundling connector from ${inputPath} to ${outputPath}...` | ||
| ).start() | ||
|
|
||
| try { | ||
| await this.generateViteBundle(inputPath, outputPath, flags, spinner) | ||
|
|
||
| if (flags.watch) { | ||
| spinner.succeed( | ||
| chalk.green('Watching for changes... (Press Ctrl+C to stop)') | ||
| ) | ||
| } else { | ||
| spinner.succeed(chalk.green('Bundle created successfully!')) | ||
| } | ||
| } catch (error) { | ||
| spinner.fail(chalk.red('Failed to bundle the connector')) | ||
|
|
||
| const errorMessage = (error instanceof Error) ? error.message : String(error) | ||
| if (flags.verbose) { | ||
| this.log('\n' + chalk.red('Error Details:')) | ||
| this.log(errorMessage) | ||
| } | ||
saikambaiyyagari marked this conversation as resolved.
Show resolved
Hide resolved
saikambaiyyagari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| this.error(errorMessage, { exit: 1 }) | ||
| } | ||
| } | ||
|
|
||
| private async generateViteBundle ( | ||
| inputPath: string, | ||
| outputPath: string, | ||
| flags: { watch: boolean; verbose: boolean }, | ||
| spinner: ora.Ora | ||
| ): Promise<void> { | ||
| const { watch, verbose } = flags | ||
|
|
||
| if (verbose) { | ||
| this.log(chalk.cyan('Creating Vite configuration...')) | ||
| } | ||
|
|
||
| const viteConfig = ViteConfigBuilder.createConfig( | ||
| { | ||
| inputPath, | ||
| outputPath, | ||
| watch | ||
| } | ||
| ) | ||
|
|
||
| if (verbose) { | ||
| spinner.stop() | ||
| this.log(chalk.cyan('Vite configuration created successfully')) | ||
| this.log(chalk.cyan('Starting build process...')) | ||
| spinner.start() | ||
| } | ||
|
|
||
| spinner.text = watch | ||
| ? 'Building connector and watching for changes...' | ||
| : 'Building connector...' | ||
| const stats = await ViteRunner.run(viteConfig) | ||
|
|
||
| if (stats.hasErrors()) { | ||
| spinner.fail(chalk.red('Bundle failed with errors!')) | ||
|
|
||
| const errors = stats.toJson().errors || [] | ||
| this.log(chalk.cyan(`Found ${errors.length} error(s)`)) | ||
| errors.forEach((error: any) => { | ||
| this.log(chalk.red(`Error: ${error.message}`)) | ||
| }) | ||
saikambaiyyagari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async run () { | ||
| const { args } = await this.parse(Bundle) | ||
| const { connectorDirectory } = args | ||
| throw new Error('Connector build failed') | ||
| } | ||
|
|
||
| const connectorPath = path.resolve(connectorDirectory) | ||
| if (verbose) { | ||
| const buildInfo = stats.toJson() | ||
| if (buildInfo.assets && buildInfo.assets.length > 0) { | ||
| this.log(chalk.cyan(`Generated ${buildInfo.assets.length} asset(s)`)) | ||
| buildInfo.assets.forEach((asset: any) => { | ||
| this.log(chalk.cyan(` - ${asset.name} (${(asset.size / 1024).toFixed(2)} KB)`)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| this.log(chalk.yellow(`Bundling connector from: ${connectorPath}`)) | ||
| // Placeholder for actual bundling logic | ||
| this.log(chalk.green('Connector bundle created successfully!')) | ||
| if (stats.hasWarnings()) { | ||
| const warnings = stats.toJson().warnings || [] | ||
| if (verbose) { | ||
| this.log(chalk.cyan(`Found ${warnings.length} warning(s)`)) | ||
| } | ||
| this.log(chalk.yellow('\nWarnings:')) | ||
| warnings.forEach((warning: any) => { | ||
| this.log(chalk.yellow(` - ${warning.message}`)) | ||
| }) | ||
| } else if (verbose) { | ||
| this.log(chalk.cyan('No warnings found')) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.