Skip to content

Programmatic API ​

Besides the command line, @neosjs/release can be used as a Node.js / TypeScript library directly in scripts, build tools, or internal platforms.

Functional API: release() ​

release(options?, dependencyInjection?) is the simplest entry point and returns Promise<ReleaseResult>:

ts
import { release } from '@neosjs/release'

const result = await release({
  'ci': true,
  'dry-run': true,
  'git': {
    push: true
  },
  'npm': {
    publish: false
  }
})

// result: { name, latestVersion, version, changelog }
console.log(`${result.name}: ${result.latestVersion} -> ${result.version}`)

Class-based API: ReleaseManager ​

Use ReleaseManager when you need to access the internal container, the final merged config object, or the logger after the release has finished:

ts
import { ReleaseManager } from '@neosjs/release'

const manager = new ReleaseManager({
  configOptions: {
    ci: true,
    git: {
      requireCleanWorkingDir: true,
      push: true
    }
  }
})

try {
  const { name, latestVersion, version, changelog } = await manager.release()
  const config = manager.getConfig()
  const logger = manager.getLogger()

  logger.info(`Released: ${name} ${latestVersion} -> ${version}`)
  logger.info(`Tag name: ${config.getContext('tagName')}`)
} catch (error: any) {
  if (error?.cancelled) {
    // The user cancelled with Ctrl+C / Esc (error.cancelled === true; error.code is 0 when stdin is a terminal, 130 otherwise)
    console.log('Release cancelled')
  } else {
    console.error('Release failed:', error)
    process.exitCode = Number.isInteger(error?.code) ? error.code : 1
  }
}

manager.getContainer(), manager.getConfig(), and manager.getLogger() must be used after manager.release() has initialized the container.

Behavior When Used as a Library ​

Compared with the command line, release() / manager.release() behaves as follows when used as a library:

  • It never terminates the process: process.exit() is never called. An early exit (for example, nothing to release, or no new commits while failure is not required) rejects with an error whose error.code === 0; a real failure has error.code of 1; the exit code is up to the caller.
  • It does not take over the host process's global errors: it installs no uncaughtException / unhandledRejection handlers, so uncaught errors are handed to the host environment as usual.
  • Results are returned: with 'release-version': true / 'only-changelog': true, it no longer writes to stdout and then exits; it returns ReleaseResult as usual (take its version / changelog field) and likewise only calculates, without performing the release.
  • Ctrl+C: release() rejects with an error whose error.cancelled === true (error.code is 0 when stdin is a terminal, 130 otherwise), the host process keeps running, and hooks after the cancellation are no longer executed.
  • Rollback timing: on the command line the process exits when the release ends, and the rollback runs when the process exits; when used as a library the process does not exit, so the rollback runs immediately when release() ends (with a failure or a cancellation) instead of waiting to modify the repository until the host process exits.

After each call finishes, the process-level listeners registered by the release flow (the SIGINT handler, the rollback hooked into exit, etc.) are cleaned up: release() can be called multiple times in the same process, and it can coexist with the host's own SIGINT handling.

Programmatic-only Options ​

When passed in through configOptions, the following c12 loading control options are supported in addition to the regular configuration options:

OptionDescription
configSet to false to disable reading local config files, or pass a custom config file path
configDirThe directory in which to look for local config files (default process.cwd())
extendExtra config sources to extend from, passed to c12
defaultConfigAn additional default config object passed to c12

Dependency Injection (dependencyInjection) ​

Dependency injection is useful for customizing log output, intercepting the opening of the browser, or writing unit tests. The following internal components can be replaced:

  • config: a custom Config instance
  • log: a custom logger (it must implement log, info, success, warn, error, verbose, exec, preview, and the other methods)
  • shell: a custom shell executor
  • spinner: a custom progress indicator
  • prompt: a custom interactive prompter
  • inquirer: replaces the underlying inquirer instance
  • openUrl: a custom browser-opening function (url, options) => Promise<any> for Release-via-PR

Example (silently capturing all logs):

ts
import { ReleaseManager } from '@neosjs/release'

const logs: string[] = []
const customLog = {
  log: (...args: any[]) => logs.push(args.join(' ')),
  info: (...args: any[]) => logs.push(`[INFO] ${args.join(' ')}`),
  success: (...args: any[]) => logs.push(`[OK] ${args.join(' ')}`),
  warn: (...args: any[]) => logs.push(`[WARN] ${args.join(' ')}`),
  error: (...args: any[]) => logs.push(`[ERR] ${args.join(' ')}`),
  verbose: () => {},
  debug: () => {},
  exec: () => {},
  obtrusive: (...args: any[]) => logs.push(args.join(' ')),
  preview: () => {},
  boxenLog: () => {}
}

const manager = new ReleaseManager({
  configOptions: {
    'config': false,
    'ci': true,
    'dry-run': true
  },
  dependencyInjection: {
    log: customLog
  }
})

const result = await manager.release()
console.log({ result, logs })