import * as path from 'path' import axios from 'axios' import chalk from 'chalk' import prompts from 'prompts' import { Command, Flags } from '@oclif/core' import { getUserGreeting, header, footer, hint } from '../utils/messages.js' import { collectConfigPaths, getPlaywrightConfig, hasPackageJsonFile, PLAYWRIGHT_CONFIG_FILES } from '../utils/directory.js' import { copyPlaywrightConfig, createProject, getProjectDirectory, installDependenciesAndInitGit, installWithinProject, } from '../utils/installation.js' import { askPlaywrightConfigPath } from '../utils/prompts.js' /** * This code is heavily inspired by the amazing create-astro package over at * https://github.com/withastro/astro/tree/main/packages/create-astro */ export default class Bootstrap extends Command { static description = 'Bootstrap a Checkly project' static flags = { 'template': Flags.string({ char: 't', description: 'An optional template name', }), 'non-interactive': Flags.boolean({ description: 'Skip interactive prompts and use default values', default: false, }), } async run (): Promise { const { flags } = await this.parse(Bootstrap) const { template, 'non-interactive': explicitNonInteractive } = flags // Auto-detect non-interactive mode if no TTY is attached const interactive = (() => { if (explicitNonInteractive) { return false } if (process.env.CHECKLY_E2E_ISTTY === 'true') { return true } if (process.stdin.isTTY && process.stderr.isTTY) { return true } return false })() const onCancel = (): void => { this.error(chalk.dim('Bailing, hope to see you again soon!\n')) } // This overrides the template prompt and skips to the next prompt if (template) { prompts.override({ template }) } // This overrides prompts answers/selections (used on E2E tests) if (process.env.CHECKLY_E2E_PROMPTS_INJECTIONS) { try { const injections = JSON.parse(process.env.CHECKLY_E2E_PROMPTS_INJECTIONS) prompts.inject(injections) } catch { process.stderr.write('Error parsing CHECKLY_E2E_PROMPTS_INJECTIONS environment variable for injections.') } } // CHECKLY_E2E_CLI_VERSION is an internal override used by the e2e suite and // for testing prereleases. It selects the version shown in the banner and // the git tag project templates are fetched from; it does not change which // create-checkly version runs. let version = process.env.CHECKLY_E2E_CLI_VERSION ?? this.config.version // use latest version from NPM if it's running from the local environment or E2E if (version === '0.0.1-dev') { try { const { data: packageInformation } = await axios.get('https://registry.npmjs.org/checkly/latest') this.logToStderr(`\nNotice: replacing version '${version}' with latest '${packageInformation.version}'.\n`) version = packageInformation.version } catch { // No-op } } // Override prompts for non-interactive mode BEFORE any prompts are called if (interactive) { // Show interactive UI elements const greeting = await getUserGreeting() await header(version, greeting) } else { prompts.override({ initializeProject: true, template: template || 'boilerplate-project', projectDirectory: process.cwd(), createInitialBrowserCheck: false, website: 'https://checklyhq.com', installDependencies: true, initializeGit: false, shouldCopyPlaywrightConfig: true, }) } const projectDirectory = await getProjectDirectory({ onCancel }) const existingProject = hasPackageJsonFile(projectDirectory) if (existingProject) { // Init Checkly CLI for an existing project await installWithinProject({ projectDirectory, version, onCancel }) } else { await hint('Cool.', `Your project will be created in the directory "${projectDirectory}"`) await createProject({ projectDirectory, version, onCancel }) } const { installOk } = await installDependenciesAndInitGit({ projectDirectory }) if (!installOk) { // A failed dependency install does not abort the bootstrap (the // scaffolded project is usable after a manual install), but scripted // callers — non-interactive mode forces the install — must still // observe a non-zero exit. process.exitCode = 1 } let playwrightConfig: string | undefined if (interactive) { const candidates = collectConfigPaths(projectDirectory, PLAYWRIGHT_CONFIG_FILES) const { playwrightConfigPath } = await askPlaywrightConfigPath(candidates, projectDirectory, onCancel) playwrightConfig = playwrightConfigPath ? path.relative(projectDirectory, playwrightConfigPath) : undefined } else { playwrightConfig = getPlaywrightConfig(projectDirectory) } // Only prompt playwright copy when not using examples if (playwrightConfig && existingProject) { await copyPlaywrightConfig({ projectDirectory, playwrightConfig, onCancel }) } // Show appropriate footer if (interactive) { await footer(projectDirectory) } } }