|
| 1 | +import { promises as fPromises } from 'fs'; |
| 2 | +import Command from '../../core/base'; |
| 3 | +import { resolve, join } from 'path'; |
| 4 | +import { load } from '../../core/models/SpecificationFile'; |
| 5 | +import fs from 'fs-extra'; |
| 6 | +import { templateFlags } from '../../core/flags/new/template.flags'; |
| 7 | +import { cyan, gray } from 'picocolors'; |
| 8 | +import jsonfile from 'jsonfile'; |
| 9 | +import path from 'path'; |
| 10 | + |
| 11 | +export const successMessage = (projectName: string) => |
| 12 | + `🎉 Your template is succesfully created |
| 13 | +⏩ Next steps: follow the instructions ${cyan('below')} to manage your project: |
| 14 | +
|
| 15 | + cd ${projectName}\t\t ${gray('# Navigate to the project directory')} |
| 16 | + npm install\t\t ${gray('# Install the project dependencies')} |
| 17 | + asyncapi generate fromTemplate <templateName> ../${projectName} \t\t ${gray('# Execute the template from anasyncapi document')} |
| 18 | +
|
| 19 | +You can also open the project in your favourite editor and start tweaking it. |
| 20 | +`; |
| 21 | + |
| 22 | +const errorMessages = { |
| 23 | + alreadyExists: (projectName: string) => |
| 24 | + `Unable to create the project because the directory "${cyan(projectName)}" already exists at "${process.cwd()}/${projectName}". |
| 25 | +To specify a different name for the new project, please run the command below with a unique project name: |
| 26 | +
|
| 27 | + ${gray('asyncapi new template --name ') + gray(projectName) + gray('-1')}`, |
| 28 | +}; |
| 29 | + |
| 30 | +export default class template extends Command { |
| 31 | + static description = 'Creates a new template'; |
| 32 | + protected commandName = 'template'; |
| 33 | + static readonly successMessage = successMessage; |
| 34 | + static readonly errorMessages = errorMessages; |
| 35 | + static flags = templateFlags(); |
| 36 | + |
| 37 | + async run() { |
| 38 | + const { flags } = await this.parse(template); // NOSONAR |
| 39 | + |
| 40 | + const { |
| 41 | + name: projectName, |
| 42 | + template: templateName, |
| 43 | + renderer: rendererName |
| 44 | + } = flags; |
| 45 | + |
| 46 | + const PROJECT_DIRECTORY = join(process.cwd(), projectName); |
| 47 | + |
| 48 | + if (rendererName!=='nunjucks' && rendererName!=='react') { |
| 49 | + this.error('Invalid flag check the flag name of renderer'); |
| 50 | + } |
| 51 | + |
| 52 | + const templateDirectory = resolve( |
| 53 | + __dirname, |
| 54 | + '../../../assets/create-template/templates/', |
| 55 | + templateName |
| 56 | + ); |
| 57 | + |
| 58 | + { |
| 59 | + try { |
| 60 | + await fPromises.mkdir(PROJECT_DIRECTORY); |
| 61 | + } catch (err: any) { |
| 62 | + switch (err.code) { |
| 63 | + case 'EEXIST': |
| 64 | + this.error(errorMessages.alreadyExists(projectName)); |
| 65 | + break; |
| 66 | + case 'EACCES': |
| 67 | + this.error( |
| 68 | + `Unable to create the project. We tried to access the "${PROJECT_DIRECTORY}" directory but it was not possible due to file access permissions. Please check the write permissions of your current working directory ("${process.cwd()}").` |
| 69 | + ); |
| 70 | + break; |
| 71 | + case 'EPERM': |
| 72 | + this.error( |
| 73 | + `Unable to create the project. We tried to create the "${PROJECT_DIRECTORY}" directory but the operation requires elevated privileges. Please check the privileges for your current user.` |
| 74 | + ); |
| 75 | + break; |
| 76 | + default: |
| 77 | + this.error( |
| 78 | + `Unable to create the project. Please check the following message for further info about the error:\n\n${err}` |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + await copyAndModify(templateDirectory, PROJECT_DIRECTORY,rendererName, projectName); |
| 85 | + this.log(successMessage(projectName)); |
| 86 | + } catch (err) { |
| 87 | + this.error( |
| 88 | + `Unable to create the project. Please check the following message for further info about the error:\n\n${err}` |
| 89 | + ); |
| 90 | + } |
| 91 | + this.specFile = await load(`${templateDirectory}/asyncapi.yaml`); |
| 92 | + this.metricsMetadata.template = flags.template; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +async function copyAndModify(templateDirectory:string, PROJECT_DIRECTORY:string, rendererName:string, projectName:string) { |
| 98 | + const packageJsonPath = path.join(templateDirectory, 'package.json'); |
| 99 | + try { |
| 100 | + await fs.copy(templateDirectory, PROJECT_DIRECTORY, { |
| 101 | + filter: (src) => { |
| 102 | + return !src.endsWith('package.json'); |
| 103 | + } |
| 104 | + }); |
| 105 | + const packageData = await jsonfile.readFile(packageJsonPath); |
| 106 | + if ((packageData.generator && 'renderer' in packageData.generator)) { |
| 107 | + packageData.generator.renderer = rendererName; |
| 108 | + } |
| 109 | + if (packageData.name) { |
| 110 | + packageData.name = projectName; |
| 111 | + } |
| 112 | + |
| 113 | + await fs.writeJSON(`${PROJECT_DIRECTORY}/package.json`, packageData, { spaces: 2 }); |
| 114 | + } catch (err) { |
| 115 | + console.error('Error:', err); |
| 116 | + } |
| 117 | +} |
0 commit comments