|
| 1 | +# How to Create Templates |
| 2 | + |
| 3 | +Here’s a brief overview of creating and setting up templates for your application projects: |
| 4 | + |
| 5 | +**Starting Point:** The main file is located at `src/index.ts`. The starting point of the code is within the child class `Generate`, which inherits from `Command`. This class utilizes the [oclif](https://oclif.io/docs/commands) dependency, which requires a specific structure in the file. |
| 6 | + |
| 7 | +## Setting Up Your Template |
| 8 | + |
| 9 | +**User Interactions:** Through prompts powered by [inquirer](https://www.npmjs.com/package/inquirer), you will define your application's name, select a project template, and specify database connection preferences, these configurations will be saved in |
| 10 | + `configObject` and sent to `generateDatabaseApp(configObject)` to kickstart the application creation process. |
| 11 | + |
| 12 | +```typescript |
| 13 | +// Ask the user to choose the template. |
| 14 | +const templateChoice = template === '' ? await select( |
| 15 | + { |
| 16 | + message: 'Which template would you like to use for your project?', |
| 17 | + choices: [ |
| 18 | + // Template choices... |
| 19 | + ], |
| 20 | + default: 'node-vanilla' |
| 21 | + }, |
| 22 | +) : template; |
| 23 | +``` |
| 24 | + |
| 25 | +The `templateChoice` constant allows the user to select a template. You can add your template choices here. |
| 26 | + |
| 27 | + |
| 28 | +```typescript |
| 29 | +// Ask the user for the database connection type. |
| 30 | +const databaseConnectionType = await select( |
| 31 | + { |
| 32 | + message: 'Which database connection type would you like to choose?', |
| 33 | + choices: [ |
| 34 | + // Database connection options... |
| 35 | + ], |
| 36 | + default: 'walletPath' |
| 37 | + } |
| 38 | +); |
| 39 | +``` |
| 40 | + |
| 41 | +The `databaseConnectionType` variable prompts the user to choose a database connection type. You can add your connection types here. |
| 42 | + |
| 43 | +```typescript |
| 44 | +// This represents the config object that will hold all the user-inputted information. |
| 45 | +let configObject; |
| 46 | + |
| 47 | +if (databaseConnectionType === 'basic') { |
| 48 | + // Prompt for protocol, hostname, port, and service details... |
| 49 | + |
| 50 | + // Configure for basic connection type. |
| 51 | + configObject = { |
| 52 | + appName, |
| 53 | + templateChoice: path.resolve(__dirname, '..', '..', 'templates', templateChoice), |
| 54 | + connectionString: generateConnectionString(protocol, hostname, port, serviceValue) |
| 55 | + }; |
| 56 | +} else { |
| 57 | + // Prompt for cloud wallet path and password... |
| 58 | + |
| 59 | + // Configure for wallet connection type. |
| 60 | + configObject = { |
| 61 | + appName, |
| 62 | + templateChoice: path.resolve(__dirname, '..', '..', 'templates', templateChoice), |
| 63 | + walletPath, |
| 64 | + walletPassword, |
| 65 | + }; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Generate Your Template |
| 70 | + |
| 71 | +The second main file is located at `generators/index.ts` The starting point of the code is within the anonymous class, which inherits from `Generator`. This class utilizes the [Yeoman](https://yeoman.io/authoring/) dependency, which requires a specific structure in the file. |
| 72 | + |
| 73 | +When creating templates, it is important to adhere to the following directory convention: |
| 74 | + |
| 75 | +- Your template should reside within a folder named `templates`. |
| 76 | +- The specific template directory should have the name that corresponds to the value stored in `templateChoice`. |
| 77 | +- Inside your folder template you must have a file named `.gitignore.template` that will genarate a `.gitignore` file in your template |
| 78 | + |
| 79 | +Ensure your template includes a `package.json` file with all necessary dependencies. When project generation, `npm install` will be run automatically to install these dependencies. |
| 80 | + |
| 81 | +### Copied Files |
| 82 | + |
| 83 | +- The `.env.example` is a template for creating a .env file. Copy it, replace placeholders with actual values. |
| 84 | + |
| 85 | +- The `.env` is used to to configure the connection to the database and other services |
| 86 | + |
| 87 | +- The `utils/db/config.cjs` loads environment variables from a `.env` file and saves the database configuration in a constant |
| 88 | + |
| 89 | +- The `utils/db/index.cjs` sets up a database connection pool using OracleDB with configurations from `utils/db/config.cjs` |
| 90 | + |
| 91 | +- The `utils/rest-serv/connection.cjs` exports a function that verifies database connectivity with the database configuration from `utils/db/index.cjs` |
| 92 | + |
| 93 | +- The `routes/connection.cjs` sets up an Express router to serve an endpoint that reports the database connection status using the function from `utils/rest-serv/connection.cjs`. |
| 94 | + |
| 95 | +- The `index.cjs` serves API requests and static files, with logging and CORS support, on port 3000. |
| 96 | + |
| 97 | + |
| 98 | +## Adding New Files |
| 99 | + |
| 100 | +- Use the `this.fs.copy()` method for straightforward file copying from your template directory to the destination project. |
| 101 | + |
| 102 | +- Use the `this.fs.copyTpl()` for files needing customization or dynamic content based on user input. |
0 commit comments