|
| 1 | +# create-gen-app |
| 2 | + |
| 3 | +<p align="center" width="100%"> |
| 4 | + <img height="90" src="https://user-images.githubusercontent.com/545047/190171475-b416f99e-2831-4786-9ba3-a7ff4d95b0d3.svg" /> |
| 5 | +</p> |
| 6 | + |
| 7 | +<p align="center" width="100%"> |
| 8 | + |
| 9 | + <a href="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml"> |
| 10 | + <img height="20" src="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml/badge.svg" /> |
| 11 | + </a> |
| 12 | + <a href="https://github.com/hyperweb-io/dev-utils/blob/main/LICENSE"> |
| 13 | + <img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/> |
| 14 | + </a> |
| 15 | + <a href="https://www.npmjs.com/package/create-gen-app"><img height="20" src="https://img.shields.io/npm/dt/create-gen-app"></a> |
| 16 | + <a href="https://www.npmjs.com/package/create-gen-app"><img height="20" src="https://img.shields.io/github/package-json/v/hyperweb-io/dev-utils?filename=packages%2Fcreate-gen-app%2Fpackage.json"></a> |
| 17 | +</p> |
| 18 | + |
| 19 | +A TypeScript library for cloning and customizing template repositories with variable replacement. |
| 20 | + |
| 21 | +## Features |
| 22 | + |
| 23 | +- Clone GitHub repositories or any git URL |
| 24 | +- Extract template variables from filenames and file contents using `__VARIABLE__` syntax |
| 25 | +- Load custom questions from `.questions.json` or `.questions.js` files |
| 26 | +- Interactive prompts using inquirerer with CLI argument support |
| 27 | +- Stream-based file processing for efficient variable replacement |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +```bash |
| 32 | +npm install create-gen-app |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +### Basic Usage |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { createGen } from 'create-gen-app'; |
| 41 | + |
| 42 | +await createGen({ |
| 43 | + templateUrl: 'https://github.com/user/template-repo', |
| 44 | + outputDir: './my-new-project', |
| 45 | + argv: { |
| 46 | + PROJECT_NAME: 'my-project', |
| 47 | + AUTHOR: 'John Doe' |
| 48 | + } |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +### Template Variables |
| 53 | + |
| 54 | +Variables in your template should be wrapped in double underscores: |
| 55 | + |
| 56 | +**Filename variables:** |
| 57 | +``` |
| 58 | +__PROJECT_NAME__/ |
| 59 | + __MODULE_NAME__.ts |
| 60 | +``` |
| 61 | + |
| 62 | +**Content variables:** |
| 63 | +```typescript |
| 64 | +// __MODULE_NAME__.ts |
| 65 | +export const projectName = "__PROJECT_NAME__"; |
| 66 | +export const author = "__AUTHOR__"; |
| 67 | +``` |
| 68 | + |
| 69 | +### Custom Questions |
| 70 | + |
| 71 | +Create a `.questions.json` file in your template repository: |
| 72 | + |
| 73 | +```json |
| 74 | +{ |
| 75 | + "questions": [ |
| 76 | + { |
| 77 | + "name": "PROJECT_NAME", |
| 78 | + "type": "text", |
| 79 | + "message": "What is your project name?", |
| 80 | + "required": true |
| 81 | + }, |
| 82 | + { |
| 83 | + "name": "AUTHOR", |
| 84 | + "type": "text", |
| 85 | + "message": "Who is the author?" |
| 86 | + } |
| 87 | + ] |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +Or use `.questions.js` for dynamic questions: |
| 92 | + |
| 93 | +```javascript |
| 94 | +/** |
| 95 | + * @typedef {Object} Questions |
| 96 | + * @property {Array} questions - Array of question objects |
| 97 | + */ |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + questions: [ |
| 101 | + { |
| 102 | + name: 'PROJECT_NAME', |
| 103 | + type: 'text', |
| 104 | + message: 'What is your project name?', |
| 105 | + required: true |
| 106 | + } |
| 107 | + ] |
| 108 | +}; |
| 109 | +``` |
| 110 | + |
| 111 | +## API |
| 112 | + |
| 113 | +### `createGen(options: CreateGenOptions): Promise<string>` |
| 114 | + |
| 115 | +Main function to create a project from a template. |
| 116 | + |
| 117 | +**Options:** |
| 118 | +- `templateUrl` (string): URL or path to the template repository |
| 119 | +- `outputDir` (string): Destination directory for the generated project |
| 120 | +- `argv` (Record<string, any>): Command-line arguments to pre-populate answers |
| 121 | +- `noTty` (boolean): Whether to disable TTY mode for non-interactive usage |
| 122 | + |
| 123 | +### `extractVariables(templateDir: string): Promise<ExtractedVariables>` |
| 124 | + |
| 125 | +Extract all variables from a template directory. |
| 126 | + |
| 127 | +### `promptUser(extractedVariables: ExtractedVariables, argv?: Record<string, any>, noTty?: boolean): Promise<Record<string, any>>` |
| 128 | + |
| 129 | +Prompt the user for variable values using inquirerer. |
| 130 | + |
| 131 | +### `replaceVariables(templateDir: string, outputDir: string, extractedVariables: ExtractedVariables, answers: Record<string, any>): Promise<void>` |
| 132 | + |
| 133 | +Replace variables in all files and filenames. |
| 134 | + |
| 135 | +## Variable Naming Rules |
| 136 | + |
| 137 | +Variables can contain: |
| 138 | +- Letters (a-z, A-Z) |
| 139 | +- Numbers (0-9) |
| 140 | +- Underscores (_) |
| 141 | +- Must start with a letter or underscore |
| 142 | + |
| 143 | +Examples of valid variables: |
| 144 | +- `__PROJECT_NAME__` |
| 145 | +- `__author__` |
| 146 | +- `__CamelCase__` |
| 147 | +- `__snake_case__` |
| 148 | +- `__VERSION_1__` |
| 149 | + |
| 150 | +## License |
| 151 | + |
| 152 | +MIT |
0 commit comments