Skip to content

Commit 791bf2f

Browse files
committed
minimon
1 parent a9a9e20 commit 791bf2f

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
11
# minimon
2+
3+
A minimal Node.js restarter based on [zx](https://github.com/google/zx).
4+
5+
A modest [`nodemon`](https://github.com/remy/nodemon) replacement.
6+
7+
Check out [the blog post covering how it was written](https://hire.jonasgalvez.com.br/2022/mar/20/building-your-own-nodemon).
8+
9+
## Install
10+
11+
```bash
12+
npm i minimon -g
13+
```
14+
15+
## Usage
16+
17+
```bash
18+
minimon app.js
19+
```
20+
21+
## Configuration
22+
23+
Just create `minimon.conf` and override the defaults. JSON5 is accepted.
24+
25+
```js
26+
{
27+
watch: ['**/*.mjs', '**/*.js'],
28+
ignored: ['**/node_modules/**'],
29+
}
30+
```

‎minimon.mjs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
3+
import JSON5 from 'json5'
4+
import chokidar from 'chokidar'
5+
import { registerGlobals } from './zx.mjs'
6+
import { EventEmitter } from 'events'
7+
8+
registerGlobals()
9+
10+
let node
11+
12+
const ee = new EventEmitter()
13+
const config = await loadConfig()
14+
15+
chokidar
16+
.watch(config.watch, {
17+
ignoreInitial: true,
18+
ignored: config.ignored,
19+
})
20+
.on('all', () => {
21+
ee.emit('restart')
22+
})
23+
24+
ee.on('restart', async () => {
25+
node.catch(() => start())
26+
node.kill()
27+
console.log('ℹ restarting')
28+
})
29+
30+
await start()
31+
32+
async function start () {
33+
node = $`${
34+
process.argv[0]
35+
} ${
36+
process.argv.slice(2).map($.originalQuote).join(' ')
37+
}`
38+
try {
39+
console.log('ℹ started')
40+
await node
41+
} catch {
42+
// No need to do anything as zx will print the original stderr
43+
}
44+
}
45+
46+
async function loadConfig () {
47+
const defaults = {
48+
watch: ['**/*.mjs', '**/*.js'],
49+
ignored: ['**/node_modules/**'],
50+
}
51+
if (await fs.exists('./minimon.conf')) {
52+
return Object.assign(defaults, JSON5.parse(await fs.readFile('./minimon.conf', 'utf8')))
53+
} else {
54+
return defaults
55+
}
56+
}

‎package.json‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "minimon",
3+
"version": "0.0.2",
4+
"description": "A minimal Node.js restarter.",
5+
"dependencies": {
6+
"chokidar": "^3.5.3",
7+
"json5": "^2.2.0",
8+
"zx": "^6.0.6"
9+
},
10+
"files": [
11+
"minimon.mjs",
12+
"zx.mjs"
13+
],
14+
"bin": {
15+
"minimon": "./minimon.mjs"
16+
}
17+
}

0 commit comments

Comments
 (0)