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

peppa

v0.0.7

Published

Peppa pig

Downloads

7

Readme

Peppa

应用管理和配置工具集

目录

Install

安装

$ npm i [-g] peppa

Alias

别名管理

应用管理中 经常会用到文件路径的抽象管理 为常用的路径提供别名以方便调用

// alias.js
const alias = require('peppa').alias();

// 设置别名, 必须以'@'开头
alias('app', __dirname);  // -> Error: Alias must start with @.
alias('@app', __dirname);

// 获取别名路径
console.log(alias('@app'));  // -> /home/xiewulong/peppa/sample
console.log(alias('@app/foo'));  // -> /home/xiewulong/peppa/sample/foo
console.log(alias('@app/foo/bar'));  // -> /home/xiewulong/peppa/sample/foo/bar
console.log(alias('@app/foo/bar/qux'));  // -> /home/xiewulong/peppa/sample/foo/bar/qux

// 非别名路径原样返回
console.log(alias('app/normal/path/will/original/return'));  // -> app/normal/path/will/original/return

Stdin

简化stdin输入

nodejs的process.stdin用起来挺麻烦的 干脆封装一个让代码组织起来更舒服

// stdin.js
const peppa = require('peppa');

process.stdout.write('Hey, baby! Do you like peppa? [yes/no] ');
peppa.stdin((chunk) => {
  let answer;

  switch(chunk) {
    case 'y':
    case 'yes':
      answer = true;
      break;
    default:
      answer = false;
      break;
  }

  console.log((answer && 'Yes' || 'No') + ', I' + (!answer && ' do not' || '') + ' like her.');
});
$ node stdin.js
Hey, baby! Do you like peppa? [yes/no] y
Yes, I like her.

Initialization

应用初始化

$ peppa init [dirname] [cwd]

一个应用工程通常需要根据不同的环境运行在不同的配置下

  • 开发环境(Development) -> session本地存储、图片本地存储、连接开发数据库
  • 测试环境(Testing) -> session本地存储、图片本地存储、连接测试数据库
  • 预生产环境(Pre-production) -> session存预生产mongodb、图片存预生产cdn、连接预生产数据库
  • 生产环境(Production) -> session存生产mongodb、图片存生产cdn、连接生产数据库

根据开发-上线流程的不同 可能还会有更多的环境配置(如压力测试环境, 甚至是个人专属的开发环境等) 如果都使用if...else...或switch来维护 可配置性和扩展性比较差 特别对当前环境来说其它环境的判断会显得很冗余 从而显得不够优雅

应用初始化工具是为了解决在不同环境下的配置(包括配置文件、密钥、证书等)的切换问题而出现的 零冗余 无缝切换

// 工程结构 sample示例 初始化之前
sample/
  apps/
    bar/
      config/
        .gitignore        忽略.local.js结尾的配置文件
        app.js            将合并app.local.js配置
        index.js
    foo/
      config/
        .gitignore        忽略.local.js结尾的配置文件
        app.js            将合并app.local.js配置
        index.js
  common/
    config/
      .gitignore          忽略.local.js结尾的配置文件
      app.js              定义static静态目录为dist/, 并将合并app.local.js配置
      index.js
  envs/
    dev/
      apps/
        bar/
          config/
            app.local.js  定义端口为30002
        foo/
          config/
            app.local.js  定义端口为30001
      common/
        config/
          app.local.js    定义当前环境为development环境
    prod/
      apps/
        bar/
          config/
            app.local.js  定义端口为3002
        foo/
          config/
            app.local.js  定义端口为3001
      common/
        config/
          app.local.js    定义了当前环境为production环境
    index.js              定义环境变量和环境配置目录的关系

运行./sample/envs环境配置目录来初始化./sample工程

$ peppa init envs sample
Application Initialization

Which environment do you want the application to be initialized in?

  [0] Production
  [1] Development

  Your choice [0-1, or "q" to quit] 1

  Initialize the application under 'Development' environment? [yes|no] y

  Start initialization ...

   generate apps/bar/config/app.local.js
   generate apps/foo/config/app.local.js
   generate common/config/app.local.js

  ... initialization completed.
// 工程结构 sample示例 development初始化后
sample/
  apps/
    bar/
      config/
        .gitignore        忽略.local.js结尾的配置文件
        app.js            将合并app.local.js配置
        app.local.js      development环境配置: 定义端口为30002
        index.js
    foo/
      config/
        .gitignore        忽略.local.js结尾的配置文件
        app.js            将合并app.local.js配置
        app.local.js      development环境配置: 定义端口为30001
        index.js
  common/
    config/
      .gitignore          忽略.local.js结尾的配置文件
      app.js              定义static静态目录为dist/, 并将合并app.local.js配置
      app.local.js        development环境配置: 定义当前环境为development环境
      index.js
  envs/
    dev/
      apps/
        bar/
          config/
            app.local.js  定义端口为30002
        foo/
          config/
            app.local.js  定义端口为30001
      common/
        config/
          app.local.js    定义当前环境为development环境
    prod/
      apps/
        bar/
          config/
            app.local.js  定义端口为3002
        foo/
          config/
            app.local.js  定义端口为3001
      common/
        config/
          app.local.js    定义了当前环境为production环境
    index.js              定义环境变量和环境配置目录的关系
$ peppa init envs sample
Application Initialization

Which environment do you want the application to be initialized in?

  [0] Production
  [1] Development

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Production' environment? [yes|no] y

  Start initialization ...

  overwrite apps/bar/config/app.local.js
  overwrite apps/foo/config/app.local.js
  overwrite common/config/app.local.js

  ... initialization completed.
// 工程结构 sample示例 production初始化后
sample/
  apps/
    bar/
      config/
        .gitignore        忽略.local.js结尾的配置文件
        app.js            将合并app.local.js配置
        app.local.js      production环境配置: 定义端口为3002
        index.js
    foo/
      config/
        .gitignore        忽略.local.js结尾的配置文件
        app.js            将合并app.local.js配置
        app.local.js      production环境配置: 定义端口为3001
        index.js
  common/
    config/
      .gitignore          忽略.local.js结尾的配置文件
      app.js              定义static静态目录为dist/, 并将合并app.local.js配置
      app.local.js        production环境配置: 定义当前环境为production环境
      index.js
  envs/
    dev/
      apps/
        bar/
          config/
            app.local.js  定义端口为30002
        foo/
          config/
            app.local.js  定义端口为30001
      common/
        config/
          app.local.js    定义当前环境为development环境
    prod/
      apps/
        bar/
          config/
            app.local.js  定义端口为3002
        foo/
          config/
            app.local.js  定义端口为3001
      common/
        config/
          app.local.js    定义了当前环境为production环境
    index.js              定义环境变量和环境配置目录的关系

License

MIT - xiewulong