erector-set
v1.0.0-beta.3
Published
A tool for creating project generators
Downloads
26
Readme
erector-set
A tool for creating project generators
Usage
Install this package
npm i -S erector-set
And use it in a file
const erectorSet = require('erector-set');
const questions = [
{ name: 'animal', question: 'Enter an animal:' },
{ name: 'unused', question: 'What\'s your age?' },
{ name: 'animalId', transform: convertSpaces, useAnswer: 'animal' }
];
const templates = [
{ destination: './animal-story.txt', template: 'My favorite animal is a(n) {{ animal }} (id: {{ animalId }})' },
{ destination: './sub/{{ animalId }}-file' }
];
const convertSpaces = value => {
return value.replace(/\s/g, '_');
};
erectorSet.build(questions, templates);
When run, this will prompt the user similar to the following:
Enter an animal: Tiger Shark
What's your age? 24
Given the above answers, it would produce the following file structure:
|_sub/
| |_TigerShark-file
|_animal-story.txt
The file in sub/TigerShark-file
would be empty since there was no template
provided while the contents
of animal-story.txt
would be:
My favorite animal is a(n) Tiger Shark (id: TigerShark)
Methods
build(questions, templates)
: asks the providedquestions
, performs string replacements ontemplates
based on those answers, then outputs those files; returns a Promiseconstruct(answers, templates)
: function to perform string replacement on templatetemplates
based on theanswers
inquire(questions)
: tool used bybuild
to askquestions
; returns a Promise
Data Structures
Questions
The questions to ask a user should be provided as an Array of Objects with the following structure:
allowBlank
: if truthy, will allow blank answersdefaultAnswer
: provide a default value to use; if provided,allowBlank
istrue
name
(required): an ID, unique to the set of questionsquestion
: the String prompt to show the usertransform
: a Function to transform the user's input; method signature istransformMethod(value, allAnswers)
whereallAnswers
is the Array of answers up until that point.useAnswer
: if a question is not provided, this attribute can be provided to use another question's answer. Make sure these questions are at the end of the list of questions!
Templates
The templates should be provided as an Array of Objects with the following structure:
check
: a function to see if the file should be created; if omitted or not a Function, it defaults to true.destination
(required): the file path to create. Note thatdestination
values are run through the same template variable-replacement as thetemplate
values. This means your can output variable file names.template
: the string to do replacements on; if this is omitted, a blank file will be created. This can also be a file path to a template; if it is, the file will be read and used for replacements.
Template Format
The template.template
value is a String which can either point to a file location or be the template
itself. The format of templates uses a variable scheme similar to Angular or
Vue, as shown in the following example:
<html>
<body>
<h1>{{ myHeader }}</h1>
</body>
</html>
In this template, the question named myHeader
would be replaced with the answer to that question. It is
important to remember any {{ }}
-wrapped strings that DO NOT have a matching answer will not be replaced.
This was done purposefully to allow accurate generation of libraries which use such a syntax.