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自行封装插件。

相关推荐
2601_9496130226 分钟前
flutter_for_openharmony家庭药箱管理app实战+药品详情实现
java·前端·flutter
We་ct1 小时前
LeetCode 15. 三数之和:排序+双指针解法全解析
前端·算法·leetcode·typescript
美狐美颜SDK开放平台1 小时前
直播场景下抖动特效的实现方案:美颜sdk开发经验分享
前端·人工智能·美颜sdk·直播美颜sdk·视频美颜sdk
草青工作室1 小时前
java-FreeMarker3.4自定义异常处理
java·前端·python
美狐美颜sdk1 小时前
抖动特效在直播美颜sdk中的实现方式与优化思路
前端·图像处理·人工智能·深度学习·美颜sdk·直播美颜sdk·美颜api
Mr Xu_1 小时前
Vue3 + Element Plus 实战:App 版本管理后台——动态生成下载二维码与封装文件上传
前端·javascript·vue.js
闻哥1 小时前
从 AJAX 到浏览器渲染:前端底层原理与性能指标全解析
java·前端·spring boot·ajax·okhttp·面试
比特森林探险记2 小时前
Vue基础语法与响应式系统详解
前端·javascript·vue.js
m0_694845572 小时前
网站账号太多难管理?Enterr 开源自动化工具搭建教程
运维·服务器·前端·开源·自动化·云计算
光影少年2 小时前
react中redux的connect作用是什么
前端·react.js·前端框架