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

相关推荐
brzhang3 小时前
我操,终于有人把 AI 大佬们 PUA 程序员的套路给讲明白了!
前端·后端·架构
止观止4 小时前
React虚拟DOM的进化之路
前端·react.js·前端框架·reactjs·react
goms4 小时前
前端项目集成lint-staged
前端·vue·lint-staged
谢尔登4 小时前
【React Natve】NetworkError 和 TouchableOpacity 组件
前端·react.js·前端框架
Lin Hsüeh-ch'in4 小时前
如何彻底禁用 Chrome 自动更新
前端·chrome
augenstern4166 小时前
HTML面试题
前端·html
张可6 小时前
一个KMP/CMP项目的组织结构和集成方式
android·前端·kotlin
G等你下课6 小时前
React 路由懒加载入门:提升首屏性能的第一步
前端·react.js·前端框架
蓝婷儿7 小时前
每天一个前端小知识 Day 27 - WebGL / WebGPU 数据可视化引擎设计与实践
前端·信息可视化·webgl
然我7 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法