@jdpnielsen/assemble
v1.1.0
Published
CLI to generate files based on templates
Downloads
696
Maintainers
Readme
@jdpnielsen/assemble
Command-line tool to generate files based on templates
Install
npm install @jdpnielsen/assemble
Blueprints
Blueprints are the core of Assemble. They are the templates that Assemble uses to generate files.
A blueprint is a directory that contains a index.ts
file and any .eta templates that are needed.
The index.ts file is where the blueprint is defined. Its executes a runner function that is passed a AssembleContext
object.
// ./blueprints/component/index.ts
import { prompt, changeCase, AssembleContext, runner, assembleTemplate } from '@jdpnielsen/assemble';
runner(async (context: AssembleContext) => {
// Enquirer is bundled with Assemble and can be used to prompt the user for input
const { name } = await prompt([
{
type: 'text',
name: 'name',
message: 'What is the name of the component?',
required: true,
},
]);
const name = changeCase(answers.name);
await assembleTemplate({
// Assemble uses eta for templating
input: path.join(__dirname, './blueprint.tsx.eta'),
output: path.join(context.cwd, `./src/components/${name.kebabCase}/${name.kebabCase}.tsx`),
templateVariables: {
componentName: name.pascalCase,
},
context,
});
}).catch((error) => {
console.error(error);
process.exit(1);
})
See example folder for examples.
To make a blueprint available to Assemble, you need to add it to the config file.
// assemble.config.ts
import { defineConfig } from '@jdpnielsen/assemble';
export default defineConfig({
blueprints: [
{
name: 'component',
recipe: './blueprints/component/index.ts',
},
],
});
Once a blueprint is added to the config file, it can be used by Assemble.
$ npx assemble --blueprint component
Motivation
Assemble was created to make it easier to generate files based on templates. The core idea is that you can create a blueprint that defines how a file should be generated using bundled helpers from the Assemble library. Assemble bundles Enquirer, Eta and ts-morph to make it easier to create blueprints.
Assemble cli then makes it easy to generate files using the Assemble command-line tool.