2025-05-26 18:23:16 +00:00
import { select } from '@clack/prompts'
2025-05-21 12:18:39 +00:00
import { Command } from 'commander'
2025-05-26 18:23:16 +00:00
import { oraPromise } from 'ora'
import semver from 'semver'
2025-05-26 17:35:19 +00:00
import type { Context } from '../types'
2025-05-26 18:23:16 +00:00
import { AuthStore } from '../lib/auth-store'
2025-05-26 17:35:19 +00:00
import { resolveDeployment } from '../lib/resolve-deployment'
export function registerPublishCommand ( { client , program , logger } : Context ) {
const command = new Command ( 'publish' )
2025-05-26 18:23:16 +00:00
. description (
'Publishes a deployment. Defaults to the most recent deployment for the project in the target directory. If a deployment identifier is provided, it will be used instead.'
)
. argument ( '[deploymentIdentifier]' , 'Optional deployment identifier' )
2025-05-26 17:35:19 +00:00
. option (
'-c, --cwd <dir>' ,
'The directory to load the Agentic project config from (defaults to cwd). This directory must contain an "agentic.config.{ts,js,json}" project file.'
)
. action ( async ( deploymentIdentifier , opts ) = > {
AuthStore . requireAuth ( )
if ( deploymentIdentifier ) {
// TODO: parseFaasIdentifier
}
2025-05-26 18:23:16 +00:00
const deployment = await oraPromise (
resolveDeployment ( {
client ,
deploymentIdentifier ,
fuzzyDeploymentIdentifierVersion : 'dev' ,
cwd : opts.cwd ,
populate : [ 'project' ]
} ) ,
{
text : 'Resolving deployment...' ,
successText : 'Resolved deployment' ,
failText : 'Failed to resolve deployment'
}
)
const { project } = deployment
2025-05-26 17:35:19 +00:00
if ( deployment . published ) {
logger . error (
deploymentIdentifier
? ` Deployment " ${ deploymentIdentifier } " is already published `
: ` Latest deployment " ${ deployment . identifier } " is already published `
)
return
}
2025-05-26 18:23:16 +00:00
if ( ! project ) {
logger . error (
deploymentIdentifier
? ` Deployment " ${ deploymentIdentifier } " failed to fetch project " ${ deployment . projectId } " `
: ` Latest deployment " ${ deployment . identifier } " failed to fetch project " ${ deployment . projectId } " `
)
return
}
const initialVersion = deployment . version
const baseVersion =
initialVersion || project . lastPublishedDeploymentVersion || '0.0.0'
2025-05-26 17:35:19 +00:00
2025-05-26 18:23:16 +00:00
const options = [
initialVersion
? { value : initialVersion , label : initialVersion }
: null ,
{
value : semver.inc ( baseVersion , 'patch' ) ,
label : ` ${ semver . inc ( baseVersion , 'patch' ) } (patch) `
} ,
{
value : semver.inc ( baseVersion , 'minor' ) ,
label : ` ${ semver . inc ( baseVersion , 'minor' ) } (minor) `
} ,
{
value : semver.inc ( baseVersion , 'major' ) ,
label : ` ${ semver . inc ( baseVersion , 'major' ) } (major) `
}
] . filter ( Boolean )
if ( project . lastPublishedDeploymentVersion ) {
logger . info (
` Project " ${ project . identifier } " latest published version is " ${ project . lastPublishedDeploymentVersion } ". \ n `
)
} else {
logger . info ( ` Project " ${ project . identifier } " is not published yet. \ n ` )
}
const version = await select ( {
message : ` Select version of deployment " ${ deployment . identifier } " to publish: ` ,
options
} )
if ( ! version || typeof version !== 'string' ) {
logger . error ( 'No version selected' )
return
}
2025-05-26 17:35:19 +00:00
2025-05-26 18:23:16 +00:00
const publishedDeployment = await client . publishDeployment (
{
version
} ,
{
deploymentId : deployment.id
}
)
2025-05-26 17:35:19 +00:00
2025-05-26 18:23:16 +00:00
logger . info (
` Deployment " ${ publishedDeployment . identifier } " published with version " ${ publishedDeployment . version } " `
)
logger . log ( publishedDeployment )
2025-05-26 17:35:19 +00:00
} )
program . addCommand ( command )
}