import { ExitPromptError } from '@inquirer/core'; import { CommandModule } from 'yargs'; import { createSolution } from './createSolution'; import { defaultValues } from './defaultValues'; import { logger, setDebug } from './helpers/logger'; import { CliResults } from './helpers/types'; import { runQuestionaire } from './runQuestionaire'; export const createSolutionCommand: CommandModule = { command: 'create', describe: 'Creates a new Mosaic solution.', builder: (yargs) => yargs .positional('projectName', { describe: `The name of the solution. Default: ${defaultValues.projectName}`, type: 'string', }) .positional('target', { describe: `The root folder for the solution. Default: ${defaultValues.target}`, type: 'string', }) .option('default', { alias: 'y', describe: 'Bypass prompts and use default options', type: 'boolean', }) .option('noGit', { type: 'boolean', describe: 'Do not initialize a new git repository', }) .option('noInstall', { type: 'boolean', describe: 'Do not run package manager install command', }) .option('installationType', { choices: ['stable', 'experimental', 'custom'], type: 'string', }) .option('branch', { type: 'string', }) .option('debug', { type: 'boolean', describe: 'Show debug logs', }), handler: async (options): Promise => { setDebug(!!options.debug); options.projectName = options._[1] as string; options.target = options._[2] as string; logger.log('Welcome to Axinom Mosaic!'); logger.log("We're going to create a new Mosaic solution for you.\n"); try { if (options.default) { await createSolution({ projectName: options.projectName ?? defaultValues.projectName, target: options.target ?? defaultValues.target, noGit: !!options.noGit, noInstall: !!options.noInstall, debug: !!options.debug, default: true, ...(await getGitReference(options)), }); } else { logger.log( 'We just need a couple more things from you before we can get started:\n', ); const answers = await runQuestionaire(options); // new line console.log(); await createSolution({ projectName: answers.projectName ?? defaultValues.projectName, target: answers.target ?? defaultValues.target, noGit: !!answers.noGit, noInstall: !!answers.noInstall, debug: !!options.debug, default: false, ...(await getGitReference(answers)), }); } } catch (error) { if (error instanceof ExitPromptError) { logger.error('\nCreation of the solution was cancelled.'); } else { logger.error( '\nLooks like something went wrong and we failed to create a new Mosaic solution :’(\n In case you need any help, please reach out to us at https://portal.axinom.com/mosaic/support', ); } process.exit(1); } logger.log( '\nWe created your Mosaic solution 🎉\nPlease follow the instructions on the README.MD to get it configured.', ); }, }; const getGitReference = async ( options: CliResults, ): Promise<{ gitReference: string; gitReferenceType: string; }> => { if (options.branch) { return { gitReference: options.branch, gitReferenceType: 'heads' }; } if (options.installationType === 'experimental') { // TODO: remove the logic here and always pull `main` after branching model is switched const devBranchRequest = await fetch( 'https://api.github.com/repos/Axinom/mosaic-media-template/branches/dev', ); const devBranchExists = devBranchRequest.ok; const gitReference = devBranchExists ? 'dev' : 'main'; return { gitReference, gitReferenceType: 'heads' }; } const lastReleaseRequest = await fetch( 'https://api.github.com/repos/Axinom/mosaic-media-template/releases/latest', ); const latestRelease = await lastReleaseRequest.json(); return { gitReference: latestRelease.tag_name, gitReferenceType: 'tags', }; };