Socket
Socket
Sign inDemoInstall

moldable

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moldable

Moldable is a fast code generator with markdown templates


Version published
Maintainers
1
Created
Source

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):white_check_mark:String"pages"
descriptionTemplate description:white_check_mark:String"Page Generator"
promptsQuestions to be asked sequentially after selecting a template:white_check_mark:Array of mapping
type: "base"
name: "name"
message: "Type a page name"
actionsSettings for generating code using the values of the answers to the questions:white_check_mark:Array 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
:white_check_mark:"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.:white_check_mark:String
messageContent of the question:white_check_mark:String
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:white_check_mark:"add" | "append"
pathDestination of the code output:white_check_mark:String(text/template)
templateTemplate name for code generation (must match the heading in the content):white_check_mark: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

```tsx
export const 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.

import React from "react";

export const {{ 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

CyberAgent, Inc. All rights reserved.

FAQs

Package last updated on 19 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc