Skip to content

Commit 9efefae

Browse files
Introduce the connector bundle logic to create the minified JS output
1 parent 2822f16 commit 9efefae

11 files changed

Lines changed: 1627 additions & 31 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"standard": "^17.0.0",
3333
"ts-node": "^10.9.1",
3434
"typescript": "~4.7.4",
35-
"yarn-audit-fix": "^10.1.1"
35+
"yarn-audit-fix": "^10.1.1",
36+
"@types/sinon-chai": "^4.0.0"
3637
},
3738
"engines": {
3839
"node": ">=20.17.0"

packages/zcli-connectors/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
"chalk": "^4.1.2",
2424
"fs-extra": "^10.0.0",
2525
"rimraf": "^3.0.2",
26-
"tslib": "^2.4.0"
26+
"tslib": "^2.4.0",
27+
"ora": "^5.4.1",
28+
"@rollup/plugin-babel": "^6.0.0",
29+
"@rollup/plugin-commonjs": "^25.0.0",
30+
"@rollup/plugin-node-resolve": "^15.0.0",
31+
"vite": "7.1.3"
2732
},
2833
"devDependencies": {
2934
"@oclif/test": "=2.1.0",
@@ -39,7 +44,8 @@
3944
"eslint-config-oclif-typescript": "^1.0.2",
4045
"lerna": "^5.6.2",
4146
"mocha": "^10.8.2",
42-
"sinon": "^14.0.0"
47+
"sinon": "^14.0.0",
48+
"sinon-chai": "^4.0.1"
4349
},
4450
"files": [
4551
"/bin",
Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,125 @@
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'
34
import * as chalk from 'chalk'
5+
import { ViteConfigBuilder, ViteRunner } from '../../lib/vite'
6+
import * as ora from 'ora'
47

58
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+
}
737

838
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+
}
1043
]
1144

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!'))
16108

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+
})
20113

21-
const connectorPath = path.resolve(connectorDirectory)
114+
throw new Error('Connector build failed')
115+
}
22116

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+
}
26124
}
27125
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export {
2+
ViteConfigBuilder,
3+
ViteConfigOptions,
4+
createConnectorViteConfig
5+
} from './vite-config'
6+
export { ViteRunner } from './vite-runner'

0 commit comments

Comments
 (0)