Skip to content

Repository files navigation

CI

moldable

A CLI tool for code generation under the influence of plop and scaffdog. It has the best of each tool, yet it is faster and more flexible. See the benchmarks for more information.

Installation

npm install -g moldable

Quick Start

Create a .moldable folder in the root directory of your application, and create Markdown file within it shown below. Name the file quick-start.md.

---name: "quick-start"description: "Quick Start"prompts:
- type: "base"name: "name"message: "Type your name"validate: "{{if eq (len .input) 0}}Name must have more than - characters{{end}}"
- type: "select"name: "favoriteColor"message: "Select your favorite color"choices:
- "Red"
- "Green"
- "Blue"actions:
- type: "add"path: "src/quick-start.json"template: "quick-start.json"---# quick-start.json```json
{
"name": "{{.name}}",
"favoriteColor": "{{.favoriteColor}}"
}
```

Execute moldable in the root directory of your application, and select the quick-start generator.

# Answer the questions? Select Generator:
▸ quick-start - Quick Start
✔ Type your name : John Doe█
? Select your favorite color :
▸ Red
Green
Blue
# Results
🖨 Added:
src/quick-start.json

Answer the questions that appear, and the code will be generated in the quick-start.json file.

{
"name": "John Doe",
"favoriteColor": "Red"
}

Advanced Usage

You can describe a generator for generating any code. Create a /.moldable folder in the root directory of your application, and create Markdown files within it.

.
└── .moldable
├── foo.md
└── bar.md

The name of the Markdown file corresponds to the generator name displayed when you launch moldable. When you run moldable, an interactive question starts according to the selected generator, and when all are answered, code is generated. This generator is divided into metadata (in YAML format) and content (in Markdown format). The structure of the metadata is as follows. You can use the syntax of Go's text/template in some properties.

NameDescriptionRequiredTypeExample
nameTemplate name (must match the Markdown file name)String"pages"
descriptionTemplate descriptionString"Page Generator"
promptsQuestions to be asked sequentially after selecting a templateArray of mapping
type: "base"
name: "name"
message: "Type a page name"
actionsSettings for generating code using the values of the answers to the questionsArray of mapping
type: "add"
path: "src/app/{{.name}}/page.tsx"
template: "{{.name | pascal}}/page.tsx"

The structure of each element of the prompts value is as follows.

NameDescriptionRequiredType
typeFormat of the question
・base: Text input
・select: Single selection
・multiselect: Multiple selection
・confirm: Y/N binary choice
"base" | "select" | "multiselect" | "confirm"
nameVariable name to store the result of the question. In places where text/template is valid, it can be referenced with a . start.String
messageContent of the questionString
prefixSupplement to the question, displayed before the questionString
suffixSupplement to the question, displayed after the questionString
whenPlace to skip the question. The results of the questions up to the current question can be referenced.String(text/template)
validatePlace to validate the question. The value of the question being entered can be referenced with .input.String(text/template)
defaultValuesDefault value of the result of the question
Data type
・base: String
・select: String
・multiselect: Array of String
・confirm: Boolean
Any
choisesChoices for select and multiselectRequired if type is select or multiselectArray of String

The structure of each element of the actions value is as follows.

NameDescriptionRequiredType
typeType of code generation"add" | "append"
pathDestination of the code outputString(text/template)
templateTemplate name for code generation (must match the heading in the content)String(text/template)
skipPlace to skip code generationString(text/template)
patternDestination to add code in appendRequired if type is appendString

In addition, for variables referenced within text/template, you can convert to the following cases using the format .var | case.

NameCase
camelcamelCase
snakesnake_case
kebabkebab-case
dashdash-case
dotdot.case
properProperCase
pascalPascalCase
lowerlower case
sentenceSentence case
constantCONSTANT_CASE
titleTitle Case

Note

By attaching OnlyAlphanumeric to the end of each case modifier (e.g. pascalOnlyAlphanumeric), if the first character of the string is not alphanumeric, it is excluded and the case is converted. For example, [projectId] is converted to ProjectId with pascalOnlyAlphanumeric.

The content consists of headings and code blocks as follows.

---actions:
- type: "add"template: "page.tsx"---# page.tsx```tsxexportconst Page = (children: { children:React.ReactNode }) => <div>{children}</div>;
```

As mentioned earlier, the template of any element contained in actions needs to match the heading in the content. The syntax of text/template can also be used for headings.

Benchmarks

Environment

  • OS: macOS Monterey 12.6.3
  • CPU: Apple M1 Max
  • Memory: 64GB
  • Node.js: 20.11.1
  • npm: 10.5.1

We measured the time it took to generate a simple React component with each package. The benchmark project is available here.

importReactfrom"react";exportconst{{ name }}: React.FC=(children: {children: React.ReactNode})=><div>{children}</div>;

Result

PackageVersionAverage time of 10 times(ms)
plop6.2.110.233
scaffdog3.0.00.385
moldable0.0.40.182

Moldable is the fastest. This is because it is written in Go and executed by node after compiling it into a binary.

Third Party License

Parts of the code from the following projects have been borrowed and modified for use.

The list of licenses is available at THIRD_PARTY_LICENSE.

License

MIT License

Developers

Copyright

CyberAgent, Inc. All rights reserved.

About

A CLI NPM Package for code generation

Topics

Resources

Stars

23 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages