Skip to content

Commit

Permalink
Add windows support (#194)
Browse files Browse the repository at this point in the history
* fix: replace native tail with fs-tail-stream
fixes #111

* fix: use options.buffer as start parameter

* feat: add additional windows support

* feat: add additional windows support
  • Loading branch information
mojoaxel committed Feb 3, 2020
1 parent 1357793 commit 0eac303
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

## Features

* log rotation
* log rotation (not on windows!)
* auto-scrolling
* marking logs
* pausing logs
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ if (program.daemonize) {
appBuilder.authorize(program.user, program.password);
}
appBuilder
.static(path.join(__dirname, 'web/assets'))
.index(path.join(__dirname, 'web/index.html'), files, filesNamespace, program.theme);
.static(path.join(__dirname, 'web', 'assets'))
.index(path.join(__dirname, 'web', 'index.html'), files, filesNamespace, program.theme);

const builder = serverBuilder();
if (doSecure) {
Expand All @@ -74,7 +74,7 @@ if (program.daemonize) {
/**
* socket.io setup
*/
const io = new SocketIO({ path: path.join(urlPath, '/socket.io') });
const io = new SocketIO({ path: `${urlPath}/socket.io` });
io.attach(server);

if (doAuthorization) {
Expand Down Expand Up @@ -105,7 +105,7 @@ if (program.daemonize) {
let presetPath;

if (!program.uiHighlightPreset) {
presetPath = path.join(__dirname, 'preset/default.json');
presetPath = path.join(__dirname, 'preset', 'default.json');
} else {
presetPath = path.resolve(untildify(program.uiHighlightPreset));
}
Expand Down
65 changes: 39 additions & 26 deletions lib/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

const events = require('events');
const childProcess = require('child_process');
const tailStream = require('fs-tail-stream');
const util = require('util');
const CBuffer = require('CBuffer');
const byline = require('byline');
const commandExistsSync = require('command-exists').sync;

function Tail(path, opts) {
events.EventEmitter.call(this);
Expand All @@ -16,43 +18,54 @@ function Tail(path, opts) {
};
this._buffer = new CBuffer(options.buffer);

var stream;

if (path[0] === '-') {
byline(process.stdin, { keepEmptyLines: true }).on('data', (line) => {
const str = line.toString();
this._buffer.push(str);
this.emit('line', str);
});
stream = process.stdin;
} else {
let followOpt = '-F';
if (process.platform === 'openbsd') {
followOpt = '-f';
}

const tail = childProcess.spawn('tail', ['-n', options.buffer, followOpt].concat(path));

tail.stderr.on('data', (data) => {
// If there is any important error then display it in the console. Tail will keep running.
// File can be truncated over network.
if (data.toString().indexOf('file truncated') === -1) {
console.error(data.toString());
/* Check if this os provides the `tail` command. */
const hasTailCommand = commandExistsSync('tail');
if (hasTailCommand) {

let followOpt = '-F';
if (process.platform === 'openbsd') {
followOpt = '-f';
}
});

byline(tail.stdout, { keepEmptyLines: true }).on('data', (line) => {
const str = line.toString();
this._buffer.push(str);
this.emit('line', str);
});
const cp = childProcess.spawn('tail', ['-n', options.buffer, followOpt].concat(path));
cp.stderr.on('data', (data) => {
// If there is any important error then display it in the console. Tail will keep running.
// File can be truncated over network.
if (data.toString().indexOf('file truncated') === -1) {
console.error(data.toString());
}
});
stream = cp.stdout;

process.on('exit', () => {
tail.kill();
});
process.on('exit', () => {
cp.kill();
});
} else {
/* This is used if the os does not support the `tail`command. */
stream = tailStream.createReadStream(path.join(), {
encoding: 'utf8',
start: options.buffer,
tail: true
});
}
}

byline(stream, { keepEmptyLines: true }).on('data', (line) => {
const str = line.toString();
this._buffer.push(str);
this.emit('line', str);
});
}
util.inherits(Tail, events.EventEmitter);

Tail.prototype.getBuffer = function getBuffer() {
return this._buffer.toArray();
};

module.exports = (path, options) => new Tail(path, options);
module.exports = (path, options) => new Tail(path, options);
29 changes: 20 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
"CBuffer": "~0.1.4",
"basic-auth-connect": "~1.0.0",
"byline": "~5.0.0",
"command-exists": "^1.2.8",
"commander": "~3.0.1",
"configstore": "~4.0.0",
"connect": "~3.6.6",
"cookie": "~0.1.0",
"cookie-parser": "~1.4.3",
"daemon-fix41": "~1.1.2",
"express-session": "~1.15.6",
"fs-tail-stream": "^1.1.0",
"is-docker": "~1.1.0",
"serve-static": "~1.13.2",
"socket.io": "^2.2.0",
Expand Down

0 comments on commit 0eac303

Please sign in to comment.