@jamen/new-common
v1.0.0
Published
Base functions for project generators.
Downloads
11
Readme
@jamen/new-common
Base functions for project generators.
Install
npm i @jamen/new-common
Usage
create(output, questions, templates)
Prompts for the questions
then writes to the output
using templates
.
output
is the directory where templates created.questions
is a function ofquestions(output) -> Promise<Array<question>>
(see propmts)templates
is a function oftemplates(answers) -> Promise<Array<template>>
(see Templates)
create(
process.cwd(),
questions,
templates
)
preview(output, questions, templates)
Like create()
, but doesn't create any files. Just runs the prompts and displays a file tree at the end. This is helpful, because answers can often affect the structure of the output.
Templates
Each template is:
templates : {
input: String,
create: Function<answers> -> Promise<Array<template>>
}
template : {
from: String,
to: String,
use: Function<from, to, answers>
}
The function in use
is called a "directive" or simply a "use", and its job is to copy or create the source (if there is one) to the destination, using prompt answers to add variance to the output.
Note that from
can be absent, since the files wont always have static contents (i.e. empty contents, or completely dynamic contents).
Uses
The package has common uses for templating, merging, and creating files.
createTemplateNormal
: Templates with lodash.templatecreateTemplateJson
: Templates and then merges into destination (e.g. apackage.json
)createTemplateUniq
: Templates and enforces unique lines (e.g. a.gitignore
)createLicense
: Creates alicense
file given a license type answer.createFile
: Creates any file.
An example of how two variations of these functions would look:
const templates = answers => [
{
from: 'package.json',
to: 'package.json',
use: createTemplateJson
},
{
from: 'lib/project.js',
to: `lib/${basename(answers.name)}.js`,
use: createTemplateNormal
},
{
to: 'license',
use: createLicense
},
]
Utils
The package has other utility functions for common tasks
exactTemplates(input, templates)
is used for composing templates from a different generator.installNpmPackages(dir, packages, dev)
is to install dependencies (withyarn
,pnpm
, ornpm
).templateFile(from, answers)
is used for reading and templating a file without writing it yet.message
are functions to create any message logged by the core functions.prompts
is prompts.
Other utils are functions from Node core that have been promisified.