|
| 1 | +import { Command, Flags } from '@oclif/core' |
| 2 | +import * as chalk from 'chalk' |
| 3 | +import * as semver from 'semver' |
| 4 | +import { existsSync, readFileSync, writeFileSync } from 'fs' |
| 5 | +import { join, resolve } from 'path' |
| 6 | + |
| 7 | +export default class Bump extends Command { |
| 8 | + static description = 'bumps the version of your connector in the manifest. Accepts major, minor and patch; defaults to patch.' |
| 9 | + |
| 10 | + static args = [ |
| 11 | + { name: 'path', description: 'relative path to connector root directory (optional, defaults to current directory)' } |
| 12 | + ] |
| 13 | + |
| 14 | + static examples = [ |
| 15 | + '<%= config.bin %> <%= command.id %>', |
| 16 | + '<%= config.bin %> <%= command.id %> ./my-connector', |
| 17 | + '<%= config.bin %> <%= command.id %> -M ./my-connector', |
| 18 | + '<%= config.bin %> <%= command.id %> -m ./my-connector', |
| 19 | + '<%= config.bin %> <%= command.id %> -p ./my-connector' |
| 20 | + ] |
| 21 | + |
| 22 | + static flags = { |
| 23 | + major: Flags.boolean({ char: 'M', description: 'Increments the major version by 1' }), |
| 24 | + minor: Flags.boolean({ char: 'm', description: 'Increments the minor version by 1' }), |
| 25 | + patch: Flags.boolean({ char: 'p', description: 'Increments the patch version by 1' }) |
| 26 | + } |
| 27 | + |
| 28 | + async run (): Promise<void> { |
| 29 | + const { args, flags } = await this.parse(Bump) |
| 30 | + const { major, minor } = flags |
| 31 | + const connectorPath = resolve(args.path || '.') |
| 32 | + |
| 33 | + // Validate connector directory exists |
| 34 | + if (!existsSync(connectorPath)) { |
| 35 | + this.error(chalk.red(`Error: Directory ${connectorPath} does not exist`)) |
| 36 | + } |
| 37 | + |
| 38 | + const indexTsPath = join(connectorPath, 'src/index.ts') |
| 39 | + |
| 40 | + // Validate index.ts exists |
| 41 | + if (!existsSync(indexTsPath)) { |
| 42 | + this.error(chalk.red(`Error: Could not find src/index.ts in ${connectorPath}`)) |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + let content = readFileSync(indexTsPath, 'utf8') |
| 47 | + |
| 48 | + // Extract current version using regex |
| 49 | + const versionRegex = /version:\s*['"]([^'"]+)['"]/ |
| 50 | + const match = content.match(versionRegex) |
| 51 | + |
| 52 | + if (!match) { |
| 53 | + this.error(chalk.red('Error: Could not find version field in src/index.ts. Make sure your connector manifest includes a version field.')) |
| 54 | + } |
| 55 | + |
| 56 | + const currentVersion = match[1] |
| 57 | + |
| 58 | + // Validate current version is valid semver |
| 59 | + if (!semver.valid(currentVersion)) { |
| 60 | + this.error(chalk.red(`Error: Current version '${currentVersion}' is not a valid semantic version`)) |
| 61 | + } |
| 62 | + |
| 63 | + // Calculate new version |
| 64 | + let newVersion: string | null |
| 65 | + if (major) { |
| 66 | + newVersion = semver.inc(currentVersion, 'major') |
| 67 | + } else if (minor) { |
| 68 | + newVersion = semver.inc(currentVersion, 'minor') |
| 69 | + } else { |
| 70 | + newVersion = semver.inc(currentVersion, 'patch') |
| 71 | + } |
| 72 | + |
| 73 | + if (!newVersion) { |
| 74 | + this.error(chalk.red('Error: Failed to increment version')) |
| 75 | + } |
| 76 | + |
| 77 | + // Replace version in content |
| 78 | + const oldVersionString = `version: '${currentVersion}'` |
| 79 | + const newVersionString = `version: '${newVersion}'` |
| 80 | + content = content.replace(oldVersionString, newVersionString) |
| 81 | + |
| 82 | + // Write updated content back to file |
| 83 | + writeFileSync(indexTsPath, content, 'utf8') |
| 84 | + |
| 85 | + this.log(chalk.green(`✅ Successfully bumped connector version from ${currentVersion} to ${newVersion}`)) |
| 86 | + } catch (error) { |
| 87 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 88 | + this.error(chalk.red(`Error: ${errorMessage}`)) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments