Skip to content

Add support for watching a directory other than project root. #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ Most features are disabled by default but you can customize behaviour by passing
Run as a terminal command without adding it as a dependency using `npx`:

```s
npx servor <root> <fallback> <port>
npx servor <root> <fallback> <port> <watchRoot>
```

> You can pass a GitHub repo as `<root>` using the syntax `gh:<user>/<repository>`

- `<root>` path to serve static files from (defaults to current directory `.`)
- `<fallback>` the file served for all non-file requests (defaults to `index.html`)
- `<port>` what port you want to serve the files from (defaults to `8080`)
- `<watchRoot>` path you want the file watcher to use (defaults to `<root>`)

Optional flags passed as non-positional arguments:

Expand Down Expand Up @@ -101,6 +102,7 @@ Use servor programmatically with node by requiring it as a module in your script
const servor = require('servor');
const instance = await servor({
root: '.',
watchRoot: '.',
fallback: 'index.html',
module: false,
static: false,
Expand Down
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const open =
root: args[0],
fallback: args[1],
port: args[2],
watchRoot: args[3],
reload: !!~process.argv.indexOf('--reload'),
module: !!~process.argv.indexOf('--module'),
static: !!~process.argv.indexOf('--static'),
Expand Down
6 changes: 4 additions & 2 deletions servor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { fileWatch, usePort, networkIps } = require('./utils/common.js');

module.exports = async ({
root = '.',
watchRoot = root,
module = false,
fallback = module ? 'index.js' : 'index.html',
reload = true,
Expand All @@ -36,6 +37,7 @@ module.exports = async ({
// Configure globals

root = root.startsWith('/') ? root : path.join(process.cwd(), root);
watchRoot = watchRoot.startsWith('/') ? watchRoot : path.join(process.cwd(), watchRoot);

const reloadClients = [];
const protocol = credentials ? 'https' : 'http';
Expand Down Expand Up @@ -152,7 +154,7 @@ module.exports = async ({
// Notify livereload reloadClients on file change

reload &&
fileWatch(root, () => {
fileWatch(watchRoot, () => {
while (reloadClients.length > 0)
sendMessage(reloadClients.pop(), 'message', 'reload');
});
Expand All @@ -165,5 +167,5 @@ module.exports = async ({
});

const x = { url: `${protocol}://localhost:${port}` };
return { ...x, root, protocol, port, ips: networkIps };
return { ...x, root, watchRoot, protocol, port, ips: networkIps };
};