2.1.0 • Published 4 months ago

hbs-commander v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

npm version npm downloads License

中文文档

🛠️ hbs-commander

A tool to simplify Handlebars template operations with precise control over content placement.

✨ Features

  • 📝 Comment Mode: Inject syntax rules by parsing Handlebars comments
  • ⚙️ Config Mode: Implement functionality through configuration options
  • 🔧 Multiple Operations: Supports append, prepend, replace, new, cover and more
  • 🎯 Parameter Support: Flexible parameter passing similar to Vue template syntax
  • 🧙 Magic-string Integration: Precise content manipulation with magic-string library
  • 📁 Directory Processing: Recursive directory handling
  • 🔄 Auto Extension Handling: Automatically remove .hbs extension
  • 🚫 File Filtering: Only process .hbs template files
  • 📂 Auto Directory Creation: Create missing target directories
  • ⏱️ Deferred Write: Prevent race conditions by deferring file writes

📦 Installation

# Using npm
npm install hbs-commander

# Using yarn 
yarn add hbs-commander

# Using pnpm
pnpm add hbs-commander

🚀 Usage

Comment Mode (Default)

{{!-- append --}}
<div class='new-content'>
  <p>append content</p>
</div>
{{!-- /append --}}
import hbscmd from 'hbs-commander';

hbscmd({
  template: './template.hbs',
  target: './target/file.vue'
});

Config Mode

import hbscmd from 'hbs-commander';

hbscmd({
  template: './template.hbs',
  target: './target/file.vue',
  mode: 'config',
  type: 'append'
});

Directory Processing Example

// Process entire directory
hbscmd({
  template: './templates',  // Template directory
  target: './src',          // Target directory
  mode: 'comment'           // or 'config'
})

Deferred Write Mode

Use deferred write to prevent race conditions when multiple operations modify the same file:

// Multiple operations on the same file
await Promise.all([
  hbscmd({
    template: './template1.hbs',
    target: './target/file.vue',
    deferWrite: true  // Enable deferred write
  }),
  hbscmd({
    template: './template2.hbs',
    target: './target/file.vue',
    deferWrite: true  // Enable deferred write
  })
]);

// Flush all changes to disk after all operations are complete
await hbscmd.applyDeferredWrites();

Extension Handling Rules

Template naming convention:

  • filename.ext.hbsfilename.ext
  • filename.hbsfilename

Examples:

  • button.html.hbsbutton.html
  • main.js.hbsmain.js
  • config.hbsconfig

📋 Supported Operations

OperationDescriptionAttrs
➕ appendAppend content to targetnewLine
⬅️ appendLeftAppend content to the leftnewLine,index, row, col
➡️ appendRightAppend content to the rightnewLine,index, row, col
⬆️ prependPrepend content to targetnewLine
⏴ prependLeftPrepend content to the leftnewLine,index
⏵ prependRightPrepend content to the rightnewLine,index
🔄 replaceReplace content in targetregexpOrString
🆕 newCreate new file with content
🖊️ coverOverwrite target file with content

🤝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/your-feature)
  5. Create a new Pull Request

📜 License

MIT © MJGang

2.1.0

4 months ago

2.0.8

5 months ago

2.0.7

5 months ago

2.0.6

5 months ago

2.0.5

5 months ago

2.0.4

5 months ago

2.0.3

5 months ago

2.0.2

5 months ago

2.0.1

5 months ago

2.0.0

5 months ago

1.0.0

6 months ago