import Debug from 'debug' import { copyTemporaryFiles, generateProjectName, usePackageName, readPackageJson, hasPackageJsonFile, checkFilesToKeep } from './directory.js' import { askCopyPlaywrightProject, askInitializeProject, askProjectDirectory, askTemplate } from './prompts.js' import { addDevDependecies, installDependencies } from '../actions/dependencies.js' import { copyTemplate } from '../actions/template.js' import { createCustomBrowserCheck } from '../actions/creates.js' import { initGit } from '../actions/git.js' import * as playwright from '../actions/playwright.js' const debug = Debug('checkly:create-cli') const templateBaseRepo = 'checkly/checkly-cli/examples' export async function getProjectDirectory ({ onCancel }: { onCancel: () => void }): Promise { debug('Ask or detect directory name') const cwd = process.cwd() // if directory has a package.json, do not ask project directory and use CWD if (hasPackageJsonFile(cwd)) { return cwd } const { projectDirectory } = await askProjectDirectory(onCancel) if (!projectDirectory) { process.stderr.write('You must provide a valid directory name. Please try again.') } return projectDirectory } export async function installWithinProject ( { projectDirectory, version, onCancel }: { projectDirectory: string, version: string, onCancel: () => void }) { debug('Existing package.json detected') const { initializeProject } = await askInitializeProject(onCancel) if (initializeProject) { const packageJson = readPackageJson(projectDirectory) const temporaryDir = generateProjectName() debug('Add dependencies to existing package.json') addDevDependecies(projectDirectory, packageJson) const FILE_TO_KEEP = ['__checks__', 'checkly.config.ts'] checkFilesToKeep(FILE_TO_KEEP, projectDirectory) debug('Copy boilerplate project to temporary folder') await copyTemplate({ template: 'boilerplate-project', templatePath: `github:${templateBaseRepo}/boilerplate-project#${version}`, targetDir: temporaryDir, }) copyTemporaryFiles(FILE_TO_KEEP, projectDirectory, temporaryDir) usePackageName(packageJson.name) debug('Create custom Browser check') await createCustomBrowserCheck({ projectDirectory, onCancel }) } else { process.exit(0) } } export async function createProject ( { projectDirectory, version, onCancel }: { projectDirectory: string, version: string, onCancel: () => void }) { const templateResponse = await askTemplate(onCancel) debug('Downloading template') await copyTemplate({ template: templateResponse.template, templatePath: `github:${templateBaseRepo}/${templateResponse.template}#${version}`, targetDir: projectDirectory, }) } export async function installDependenciesAndInitGit ( { projectDirectory }: { projectDirectory: string }): Promise<{ installOk: boolean }> { debug('Install npm dependencies') const installOk = await installDependencies(projectDirectory) debug('Init .git & .gitignore') await initGit(projectDirectory) return { installOk } } export async function copyPlaywrightConfig ({ projectDirectory, playwrightConfig, onCancel }: { projectDirectory: string, playwrightConfig: string, onCancel: () => void }) { debug('Check if playwright config exists in project') // The boilerplate template hardcodes playwrightConfigPath: './playwright.config.ts', // update it to the path the user selected. const { shouldCopyPlaywrightConfig } = await askCopyPlaywrightProject(onCancel) if (shouldCopyPlaywrightConfig) { playwright.updatePlaywrightConfigPath(projectDirectory, `./${playwrightConfig}`) await playwright.copyPlaywrightConfig(projectDirectory, playwrightConfig) } }