
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
zhandlebars
Advanced tools
zHandlebars 是一个基于 Handlebars 的项目生成器。
我们会需要根据一组模板生成一个初始项目,一般来说,目录复制,然后改一些小地方即可。
但还有可能生成的文件名和项目名有关系,目录结构可能也和项目名有关,甚至会根据一组不定数量的最终生成的文件等等。
而 zHandlebars 就是用来做这件事的。
我们基于模板引擎 Handlebars ,也就是说所有的语法和 Handlebars 一致,我们就是在 Handlebars 对一个文件处理的基础上,增加了一个组合配置文件,而且这个配置文件支持通配符,支持 Handlebars 语法。
这个配置文件我们用json的格式来保存:
[
{"name": "{{projname_lc}}", "src": "", "type": "dir"},
{"name": "{{projname_lc}}/bin/{{projname_lc}}.js", "src": "/main/bin/main.js", "type": "file"},
{"name": "{{projname_lc}}/config.js", "src": "/main/config.js", "type": "file"},
{"name": "{{projname_lc}}/package.json", "src": "/main/package.json", "type": "file"},
{"name": "{{projname_lc}}/lib/*.js", "src": "/main/lib/*.js", "type": "file"},
{"name": "{{projname_lc}}/src/*.js", "src": "/main/src/*.js", "type": "file"},
{"name": "{{projname_lc}}/ctrl/*.js", "src": "/main/ctrl/*.js", "type": "file"}
]
上面可以看到,其实这个配置文件就是一个简单的json数组,每个元素是一条匹配条件,其中 name 表示目标文件或目录名,src 表示原始文件名,type 是类型,目前就 dir、file、binfile 3种,如果是 dir,就只是建目录而已,所以 src 可以给空字符串。
特别提一下 overwrite 属性,这个属性主要用于 type 是 file 或者 binfile 的,有3个值,分别是 w 、 r 、 rw ,表示如果源文件存在时,是直接覆盖写,还是彻底放弃处理,或者是读取已经存在的目标文件里的标识内容,重新整合写入,默认是 w ,如果是 binfile ,rw 就是 w 。
如果是 file 或者 binfile ,这里支持通配符,通配符的规则兼容 glob 。
规则上,是会先把这个配置文件通过模板引擎 Handlebars 处理一轮,然后按顺序新建目录和文件,新建文件的时候,其实会自动把不存在的目录生成出来的,不需要特别处理目录。
有了配置文件和模板文件外,使用起来就非常简单了。
"use strict";
var path = require('path');
var zhandlebars = require('../lib/index');
let params = {projname: 'test', projname_lc: 'test'};
zhandlebars.procProj(params, path.join(__dirname, './zrestify.json'), path.join(__dirname, './'));
ver 0.1.7
json配置文件里支持 overwrite ,可以决定这个文件的覆盖规则,主要用于处理文件时,如果目标文件已经存在,使用何种方式来处理。
ver 0.1.6
json配置文件里支持 jscode ,可以在处理这个文件的时候执行一段js脚本,主要是用来处理批量的参数的。
ver 0.1.3
支持 binfile 类型,对于不需要模板处理的文件都可以这样设置,会快一些。
FAQs
zhandlebars
We found that zhandlebars demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.