tailwind-nextjs-blog/scripts/next-remote-watch.js

135 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2022-07-17 21:40:41 +08:00
#!/usr/bin/env node
// Adapted from https://github.com/hashicorp/next-remote-watch
// A copy of next-remote-watch with an additional ws reload emitter.
// The app listens to the event and triggers a client-side router refresh
// see components/ClientReload.js
2022-10-17 23:37:01 +08:00
const chalk = require('chalk');
const chokidar = require('chokidar');
const program = require('commander');
const http = require('http');
const SocketIO = require('socket.io');
const express = require('express');
const spawn = require('child_process').spawn;
const next = require('next');
const path = require('path');
const { parse } = require('url');
const pkg = require('../package.json');
const defaultWatchEvent = 'change';
program.storeOptionsAsProperties().version(pkg.version);
2022-07-17 21:40:41 +08:00
program
.option('-r, --root [dir]', 'root directory of your nextjs app')
2022-10-17 23:37:01 +08:00
.option(
'-s, --script [path]',
'path to the script you want to trigger on a watcher event',
false
)
2022-07-17 21:40:41 +08:00
.option('-c, --command [cmd]', 'command to execute on a watcher event', false)
.option(
'-e, --event [name]',
`name of event to watch, defaults to ${defaultWatchEvent}`,
defaultWatchEvent
)
2022-10-17 23:37:01 +08:00
.option(
'-p, --polling [name]',
`use polling for the watcher, defaults to false`,
false
)
.parse(process.argv);
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
const shell = process.env.SHELL;
const app = next({ dev: true, dir: program.root || process.cwd() });
const port = parseInt(process.env.PORT, 10) || 3000;
const handle = app.getRequestHandler();
2022-07-17 21:40:41 +08:00
app.prepare().then(() => {
// if directories are provided, watch them for changes and trigger reload
if (program.args.length > 0) {
chokidar
.watch(program.args, { usePolling: Boolean(program.polling) })
2022-10-17 23:37:01 +08:00
.on(
program.event,
async (filePathContext, eventContext = defaultWatchEvent) => {
// Emit changes via socketio
io.sockets.emit('reload', filePathContext);
app.server.hotReloader.send('building');
if (program.command) {
// Use spawn here so that we can pipe stdio from the command without buffering
spawn(
shell,
[
'-c',
program.command
.replace(/\{event\}/gi, filePathContext)
.replace(/\{path\}/gi, eventContext),
],
{
stdio: 'inherit',
}
);
}
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
if (program.script) {
try {
// find the path of your --script script
const scriptPath = path.join(
process.cwd(),
program.script.toString()
);
// require your --script script
const executeFile = require(scriptPath);
// run the exported function from your --script script
executeFile(filePathContext, eventContext);
} catch (e) {
console.error('Remote script failed');
console.error(e);
return e;
}
2022-07-17 21:40:41 +08:00
}
2022-10-17 23:37:01 +08:00
app.server.hotReloader.send('reloadPage');
}
);
2022-07-17 21:40:41 +08:00
}
// create an express server
2022-10-17 23:37:01 +08:00
const expressApp = express();
const server = http.createServer(expressApp);
2022-07-17 21:40:41 +08:00
// watch files with socketIO
2022-10-17 23:37:01 +08:00
const io = SocketIO(server);
2022-07-17 21:40:41 +08:00
// special handling for mdx reload route
2022-10-17 23:37:01 +08:00
const reloadRoute = express.Router();
reloadRoute.use(express.json());
2022-07-17 21:40:41 +08:00
reloadRoute.all('/', (req, res) => {
// log message if present
2022-10-17 23:37:01 +08:00
const msg = req.body.message;
const color = req.body.color;
msg && console.log(color ? chalk[color](msg) : msg);
2022-07-17 21:40:41 +08:00
// reload the nextjs app
2022-10-17 23:37:01 +08:00
app.server.hotReloader.send('building');
app.server.hotReloader.send('reloadPage');
res.end('Reload initiated');
});
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
expressApp.use('/__next_reload', reloadRoute);
2022-07-17 21:40:41 +08:00
// handle all other routes with next.js
2022-10-17 23:37:01 +08:00
expressApp.all('*', (req, res) => handle(req, res, parse(req.url, true)));
2022-07-17 21:40:41 +08:00
// fire it up
server.listen(port, (err) => {
2022-10-17 23:37:01 +08:00
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});