Skip to content

Generate

Generate common files, folders, and workspaces from your own standard templates. Since every organization has its own needs and standards, providing a one-size-fits-all solution for scaffolding workspaces and other common files is out of scope for oneRepo. However, the generate core command provides a helpful way for creating your own template generators.

First, set the location for oneRepo to look for your templates. This can be anywhere within your repository, but must be a relative path from the root configuration file.

./onerepo.config.js
export default {
templateDir: './templates',
};

In your templateDir directory, create folders for different template types. Most use-cases will include separate templates for app and module. Ensure each template directory includes a .onegen.js or .onegen.cjs file:

  • Directoryrepo
    • onerepo.config.js
    • package.json
    • Directorytemplates
      • Directoryapp
        • .onegen.js // This is a oneRepo generation config file
        • package.json.ejs // Render files with EJS
      • Directorymodule
        • .onegen.js
        • package.json.ejs

The .onegen.cjs file is a configuration file for each template directory that tells oneRepo how to handle and format the output of your workspace:

templates/module/.onegen.js
import { fileURLToPath } from 'node:url';
import path from 'node:path';
export default {
outDir: () => path.join(fileURLToPath(import.meta.url), '../../../path/to/output'),
};

The above will result in a set of prompts that may look like the following:

Terminal window
? Choose a template…
app
module

All other files in the template directory and any sub-directories will be included in the resulting generated output. Any file that uses the extension .ejs will be read and parsed using Embedded JavaScript templating and the generated files will have that extension stripped, leaving the rendered file with the correct extension in place.

By default, templates will be rendered with the variables name and fullName. These are determined by the nameFormat function provided in your configuration. If not provided, the values of these will be identical.

Filenames will be rendered using EJS as well. If a file includes something like <%- name %>.ts.ejs, the resulting output file will have the name variable replaced inline.

You will likely use inquirer prompts provided by each .onegen.cjs configuration to add input variables to your templates.

templates/module/.onegen.js
import { fileURLToPath } from 'node:url';
import path from 'node:path';
export default {
name: 'Module',
description: 'A shared, publishable workspace',
outDir: () => path.join(fileURLToPath(import.meta.url), '../../../path/to/modules'),
prompts: [
{
name: 'name',
question: 'What is the name of the module?',
prefix: '@scope/',
filter: (name) => name.replace(/[^a-zA-Z0-9-]/g, '').toLowerCase(),
transformer: (name) => name.replace(/[^a-zA-Z0-9-]/g, '').toLowerCase(),
},
{
type: 'input',
name: 'description',
message: 'Please enter a description for the module',
},
],
};

The above will result in a set of prompts that may look like the following:

Terminal window
? Choose a template…
App
Module - A shared, publishable workspace
? What is the name of the module? @scope/ ▓
? Please anter a description for the module ▓

Please see the inquirer documentation for more options and usage instructions.

Aliases: one gen

Generate files, folders, and Workspaces from templates.

Terminal window
one generate [options...]

To create new templates add a new folder to config/templates and create a .onegen.cjs configuration file. Follow the instructions online for more: https://onerepo.tools/core/generate/

OptionTypeDescription
--type, -tstringTemplate type to generate. If not provided, a list will be provided to choose from.
Advanced options
OptionTypeDescriptionRequired
--show-advancedbooleanPair with --help to show advanced options.
--template-dir, --templates-dirstring, default: "config/templates"Path to the templates