node 简单实现打包后推送系统通知,自动打开项目文件夹

场景

不知道大家有没有遇到过打包时间较长的项目,又想在第一时间把dist包发给后端又不想一直盯着cli的情况,那能不能在打包完成后能自动推送系统通知和帮我启动文件资源管理器呢,今天就用node-notifier来快速实现一下🚀。

ps:项目集成CI/CD的掘友可能就不需要了🤡

方案

  1. ✨推荐使用插件 unplugin-build-notifier (npmjs.com) ,支持 Vite、Webpack、Nuxt 等主流框架及Windows、MacOS、Linux系统
  2. 使用node-notifier-cli能快速实现简单需求
  3. 写一个基于node-notifier的node脚本来完成一些自定义需求

✨unplugin-build-notifier

该插件使用简单,直接下载引入即可,图片、消息、行为均可配置,默认点击通知以及通知超时未操作则打开当前项目文件夹。

这里以vite项目为例,其它框架示例可查看 unplugin-build-notifier (npmjs.com)

Install

css 复制代码
npm i unplugin-build-notifier -D

Usage

ts 复制代码
// vite.config.ts
import BuildNotifier from 'unplugin-build-notifier/vite'

export default defineConfig({
  plugins: [
    BuildNotifier({ /* options */ }),
  ],
})

Options

ts 复制代码
/**
 * plugin options.
 */
interface Options {
  /**
   * The message to display in the build notifier.
   */
  message?: string

  /**
   * The path to the icon to display in the build notifier.
   */
  iconPath?: string

  /**
   * The callback function to execute when the build notifier is clicked.
   */
  click?: () => void

  /**
   * The callback function to execute when the build notifier times out.
   */
  timeout?: () => void
}

node-notifier-cli

这个使用也很简单直接在package.json配置一下命令即可,不过该方法在打包完成后会有一点延迟才能推送通知,且不能自定义通知点击和超时行为,所以更推荐使用unplugin-build-notifier插件。

Install

css 复制代码
npm i [-g] node-notifier-cli

Usage

用 && 符可在前面命令执行完后接着执行build-notifier-cli

json 复制代码
// package.json
"build": "xxxx && npm run build-notifier-cli",
"build-notifier-cli": "notify -t 'Hello' -m 'My message'",

更多参数可查看 node-notifier-cli (npmjs.com)

node-notifier

如果需要高度自定义通知消息和行为,那就自己写一个node脚本吧。 以下是简单示例实现打包完成时推送系统通知,自动打开项目文件夹功能,想要更完善的代码可参考 unplugin-build-notifier (github.com),核心代码也才 80 行。

1. 在项目根目录中创建 scripts

js 复制代码
├── scripts
├── ├── icon
│   ├── ├── SpongeBob.jpg
├── ├── buildNotifier.js

2. buildNotifier.js 核心代码

js 复制代码
import { basename, dirname, join, resolve } from 'node:path'
import { exec } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import notifier from 'node-notifier'

// 获取当前目录
const __dirname = dirname(fileURLToPath(import.meta.url))
// 获取项目根目录
const rootPath = resolve(__dirname, '../')

// Windows系统 用shell命令打开当前项目文件夹
function startdir() {
  exec(`start "" "${rootPath}"`, (error, stdout, stderr) => {
    if (error) {
      console.error(
        `exec error: ${error}\nstdout: ${stdout}\nstderr: ${stderr}`,
      )
    }
  })
}

// node-notifier 配置消息和行为
function buildNotifier() {   
  notifier.notify({
    title: `✨ ${basename(rootPath)}`,
    message: `🎉 build success!\n${rootPath}`,
    icon: join(__dirname, './icon/SpongeBob.jpg'),
  })
  notifier.on('click', startdir)
  notifier.on('timeout', startdir)
}

buildNotifier()

3. 配置命令

用 && 符可在前面命令执行完后接着执行buildNotifier.js

json 复制代码
// package.json
"build": "xxxx && node ./scripts/buildNotifier.js",

更多配置信息可查看 node-notifier (npmjs.com)

总结

以上方法都是不错的选择,如果需求符合个人推荐使用unplugin-build-notifier通过插件集成到项目中,而node-notifier-clinode-notifier 通过配置命令的方式实现会在打包完成后存在一些延迟才推送通知,当然如果需要的话也可以通过node-notifier自行封装插件。

相关推荐
我是Superman丶1 小时前
【技巧】前端VUE用中文方法名调用没效果的问题
前端·javascript·vue.js
斯~内克1 小时前
Vue 3 中 watch 的使用与深入理解
前端·javascript·vue.js
蜡笔小柯南3 小时前
解决:npm install报错,reason: certificate has expired
前端·npm·node.js
lqj_本人4 小时前
鸿蒙OS&UniApp制作多选框与单选框组件#三方框架 #Uniapp
前端·javascript·uni-app
@PHARAOH5 小时前
WHAT - 前端开发流程 SOP(标准操作流程)参考
前端·领导力
松树戈5 小时前
plus-ui&RuoYi-Vue-Plus 基于pgSql本地运行实践
前端·vue.js·spring boot·ui
new6669996 小时前
css画图形
前端·css
Yvonne爱编码7 小时前
CSS- 1.1 css选择器
前端·css·状态模式·html5·hbuilder
山河故人1637 小时前
uniapp使用npm下载
前端·npm·uni-app
-曾牛7 小时前
基于微信小程序的在线聊天功能实现:WebSocket通信实战
前端·后端·websocket·网络协议·微信小程序·小程序·notepad++