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

@tols/gm-base

v1.0.4

Published

用于@tols gm工具的基础类。具有数据定义,生命周期等能力

Downloads

9

Readme

说明

@tols/gm-base是@tols gm所有工具的基础类。它提供了一套批处理的解决方案。具有数据定义、生命周期钩子等能力。

安装

npm i -S @tols/gm-base

使用示例

const gmBase = require('@tols/gm-base')

gmBase({
  //=============== 数据定义部分 =============
  
  /**
   * 当前工具需要用到的变量数据
   * 每次都返回一个全新的数据对象
   * @returns {{}}
   */
  data () {
    return {
      directory: 'dist', // 压缩之后存放到指定目录的文件夹名称。默认''
      threads: 5, // 支持同时处理多个文件,建议5~8个。默认5
      qualityVal: 60, // 压缩质量值,0 ~ 100之间
      progressInfo: ['压缩进度', '正在压缩'] // 进度条信息提示
    }
  },
  
  
  //=============== 生命周期部分 =============
  
  /**
   * 收集信息之前的钩子
   * @param qualityVal 压缩质量的默认值。注:这里是data参数的解构
   * @returns {*[]} 可返回需要收集信息的问题。注:源文件目录name值必须是'srcPath',存放目录name值必须是'distPath'
   */
  beforeCollectInfo ({threads, qualityVal}) {
    return [
      {
        type: 'input',
        message: '请输入要同时压缩的文件数量:',
        name: 'threads',
        default: threads
      },
      {
        type: 'input',
        message: '请输入压缩后的图片质量:',
        name: 'qualityVal',
        default: qualityVal
      },
      {
        type: 'input',
        message: '请输入要压缩的图片源文件目录:',
        name: 'srcPath',
        default: ''
      },
      {
        type: 'input',
        message: '请输入压缩之后图片将要存放的目录:',
        name: 'distPath',
        default: ''
      }
    ]
  },
  
  /**
   * 开始批量压缩图片之前的钩子
   * @param data 开始时定义的data数据对象
   * @param answers 收集信息阶段的信息对象
   */
  beforeStart ({data, answers, fromCode}) {
    // 如果来自代码逻辑操作,则不往下执行数据验证
    if (fromCode) {
      return
    }
    
    // 以下操作来自命令行
    // 为data赋值收集到的信息
    data.threads = Number(answers.threads)
    data.qualityVal = Number(answers.qualityVal)
    
    // 数据验证
    if (!answers.threads || typeof data.threads !== 'number' || data.threads !== data.threads) {
      throw '请输入要同时压缩的文件数量'
    }
    if (!answers.qualityVal || typeof data.qualityVal !== 'number' || data.qualityVal !== data.qualityVal) {
      throw '请输入压缩后的图片质量'
    }
    if (!answers.srcPath) {
      throw '请输入要压缩的图片源文件目录'
    }
    if (!answers.distPath) {
      throw '请输入压缩之后图片将要存放的目录'
    }
  },
  
  /**
   * 批处理实时抛出的钩子。(每处理一张图片触发一次)
   * @param data 开始时定义的data数据对象
   * @param srcFile 当前图片的文件路径
   * @param distFile 当前图片即将要保存的文件路径
   * @param gm 图像处理器实例
   * @param warnings 警告信息收集器(数组)
   * @param next 处理图片之后的回调,调用该方法会进入下一个处理
   */
  batching ({data, srcFile, distFile, gm, warnings, next}) {
    // 根据业务逻辑,可以收集一些警告信息
    /* if (isWarnings) {
      warnings.push('一些警告信息')
    } */

    // 开始处理图片
    gm(srcFile)
      .quality(data.qualityVal)
      .write(distFile, function (err) {
        if (err) {
          console.log(err)
          return
        }
        next()
      })
  },
  
  /**
   * 批量处理完毕之后的钩子
   * @param distPath 保存图片的目录路径
   * @param chalk 文字着色器
   * @param warnings 警告信息收集器(数组)
   */
  afterBatch ({distPath, chalk, warnings}) {
    console.log(`您的图片已经压缩完毕。\n存放在: ${distPath}`)
    if (warnings.length) {
      console.log(chalk.yellow(`温馨提示:检测到有以下些图片被拉扯,可能会导致模糊!\n${warnings.join('\n')}`))
    }
  }
})