npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zhandlebars

v0.1.8

Published

zhandlebars

Downloads

10

Readme

zHandlebars

zHandlebars 是一个基于 Handlebars 的项目生成器。

How

我们会需要根据一组模板生成一个初始项目,一般来说,目录复制,然后改一些小地方即可。

但还有可能生成的文件名和项目名有关系,目录结构可能也和项目名有关,甚至会根据一组不定数量的最终生成的文件等等。

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 是类型,目前就 dirfilebinfile 3种,如果是 dir,就只是建目录而已,所以 src 可以给空字符串。

特别提一下 overwrite 属性,这个属性主要用于 typefile 或者 binfile 的,有3个值,分别是 wrrw ,表示如果源文件存在时,是直接覆盖写,还是彻底放弃处理,或者是读取已经存在的目标文件里的标识内容,重新整合写入,默认是 w ,如果是 binfilerw 就是 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 类型,对于不需要模板处理的文件都可以这样设置,会快一些。

使用到的第三方库

  • 使用 glob 做文件通配符处理。
  • 使用 handlebars 模板做基本代码模板。