Socket
Socket
Sign inDemoInstall

@tols/gm-base

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tols/gm-base

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


Version published
Maintainers
1
Created
Source

说明

@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')}`))
    }
  }
})

Keywords

FAQs

Package last updated on 21 Aug 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc