https://github.com/mjgang/hbs-commander
A library for intelligently inserting and replacing Handlebars templates with context-aware placement
https://github.com/mjgang/hbs-commander
automated code-generator handlebars intelligence low-code templates
Last synced: about 4 hours ago
JSON representation
A library for intelligently inserting and replacing Handlebars templates with context-aware placement
- Host: GitHub
- URL: https://github.com/mjgang/hbs-commander
- Owner: MJGang
- License: mit
- Created: 2025-01-20T03:30:23.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-03-20T02:07:56.000Z (about 1 month ago)
- Last Synced: 2025-04-28T12:12:01.303Z (about 4 hours ago)
- Topics: automated, code-generator, handlebars, intelligence, low-code, templates
- Language: JavaScript
- Homepage:
- Size: 96.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/hbs-commander)
[](https://npm-stat.com/charts.html?package=hbs-commander)
[](https://github.com/MJGang/hbs-commander/blob/main/LICENSE)[δΈζζζ‘£](README_ZH.md)
# π οΈ 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
```bash
# Using npm
npm install hbs-commander# Using yarn
yarn add hbs-commander# Using pnpm
pnpm add hbs-commander
```## π Usage
### Comment Mode (Default)
```hbs
{{!-- append --}}
append content
{{!-- /append --}}
``````javascript
import hbscmd from 'hbs-commander';hbscmd({
template: './template.hbs',
target: './target/file.vue'
});
```### Config Mode
```javascript
import hbscmd from 'hbs-commander';hbscmd({
template: './template.hbs',
target: './target/file.vue',
mode: 'config',
type: 'append'
});
```### Directory Processing Example
```javascript
// 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:
```javascript
// 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.hbs` β `filename.ext`
- `filename.hbs` β `filename`Examples:
- `button.html.hbs` β `button.html`
- `main.js.hbs` β `main.js`
- `config.hbs` β `config`## π Supported Operations
| Operation | Description | Attrs |
|-----------|-------------|--------|
| β append | Append content to target | `newLine` |
| β¬ οΈ appendLeft | Append content to the left | `newLine`,`index`, `row`, `col` |
| β‘οΈ appendRight | Append content to the right | `newLine`,`index`, `row`, `col` |
| β¬οΈ prepend | Prepend content to target | `newLine` |
| β΄ prependLeft | Prepend content to the left | `newLine`,`index` |
| β΅ prependRight | Prepend content to the right | `newLine`,`index` |
| π replace | Replace content in target | `regexpOrString` |
| π new | Create new file with content | |
| ποΈ cover | Overwrite 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](https://github.com/MJGang)