luoxiu / watch

Do something exciting on file changes!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Watch

Do something exciting on file changes!

ci npm license

Install

$ npm install -g @luoxiu/watch

$ yarn add -g @luoxiu/watch

$ pnpm add -g @luoxiu/watch

Usage

In your working directory, create a file named watch.config.js:

$ cd ./wd && touch watch.config.js

and execute:

$ watch

watch.config.js

The following example will format your swift code as it changes:

// watch.config.js
async function format(path) {
  await $`sleep 1 && swiftformat ${path}`
}

module.exports = {
  watchers: [
    {
      paths: ["Sources/**/*.swift", "Tests/**/*.swift"],
      options: {
        usePolling: true,
        interval: 500
      },
      callbacks: [
        {
          events: ["change"],
          callback: async (path) => {
            format(path)
          }
        }
      ]
    }
  ]
}

chokidar

watch uses chokidar to watch file changes, so you can watch add | change | unlink(remove) | etc events on file | dir | glob | etc.

Please refer to chokidar#api to learn more about paths, options, events and callbacks.

zx

watch uses zx to execute callbacks, so you can write expressive scripts like:

{
    events: ["changes"],
    callback: async (path) => {
        const resp = await fetch('https://wttr.in')
        const text = await fs.readFile(path)

        console.log(chalk.blue('Hello world!'))

        cd('/a-path')
        await $`pwd` // outputs /tmp
    }
}

Please refer to zx#documentation to learn more about zx.

lodash

watch also includes lodash, so you can use your favorite lodash functions like _.cloneDeep, _.uniq, _.debounce to write your ambitious configs.

Please refer to lodash#Docs to learn more about lodash.

About

Do something exciting on file changes!

License:MIT License


Languages

Language:JavaScript 67.8%Language:TypeScript 32.2%