Skip to content

Commit

Permalink
fix(log): output as many lines as possible if n > file lines
Browse files Browse the repository at this point in the history
closes #221
- swap out slice-file dependency for `tail` and `read-last-lines`
- make `ghost log` instance-aware
  • Loading branch information
acburdine committed Jul 4, 2017
1 parent 57587a6 commit 3c56f2e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 44 deletions.
25 changes: 16 additions & 9 deletions lib/commands/log.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
'use strict';
const path = require('path');
const includes = require('lodash/includes');
const sliceFile = require('slice-file');
const lastLines = require('read-last-lines');
const Tail = require('tail').Tail;
const Promise = require('bluebird');
const PrettyStream = require('ghost-ignition/lib/logging/PrettyStream');

const errors = require('../errors');
const Command = require('../command');

class LogCommand extends Command {
run(argv) {
if (!argv.name) {
Command.checkValidInstall('log');
}

let instance = this.system.getInstance(argv.name);

if (!instance) {
Expand All @@ -34,7 +40,6 @@ class LogCommand extends Command {
}

let logFileName = path.join(process.cwd(), 'content/logs', `${instance.config.get('url').replace(/[^\w]/gi, '_')}_${this.system.environment}.log`);
let slice = sliceFile(logFileName);
let prettyStream = new PrettyStream();

prettyStream.on('error', (error) => {
Expand All @@ -45,17 +50,19 @@ class LogCommand extends Command {

prettyStream.pipe(this.ui.stdout);

if (argv.follow) {
slice.follow(-argv.number).pipe(prettyStream);
return;
} else {
slice.slice(-argv.number).pipe(prettyStream);
}
return lastLines.read(logFileName, argv.number).then((lines) => {
lines.trim().split('\n').forEach(line => prettyStream.write(line));

if (argv.follow) {
let tail = new Tail(logFileName);
tail.on('line', (line) => prettyStream.write(line, 'utf8'));
}
});
}
}

LogCommand.description = 'View logs of a running Ghost process';
LogCommand.params = '<name>';
LogCommand.params = '[name]';
LogCommand.options = {
number: {
alias: 'n',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
"nginx-conf": "1.3.0",
"ora": "1.3.0",
"portfinder": "1.0.13",
"read-last-lines": "1.2.0",
"rxjs": "5.4.1",
"semver": "5.3.0",
"shasum": "1.0.2",
"slice-file": "1.0.0",
"strip-ansi": "4.0.0",
"symlink-or-copy": "1.1.8",
"tail": "1.2.2",
"update-notifier": "2.2.0",
"validator": "7.2.0",
"yargs": "8.0.2",
Expand Down
99 changes: 65 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ ansi-styles@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"

any-promise@^1.0.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"

aproba@^1.0.3:
version "1.1.2"
resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.1.2.tgz#45c6629094de4e96f693ef7eab74ae079c240fc1"
Expand Down Expand Up @@ -1219,6 +1223,25 @@ fs-extra@3.0.1:
jsonfile "^3.0.0"
universalify "^0.1.0"

fs-extra@^0.26.5:
version "0.26.7"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-0.26.7.tgz#9ae1fdd94897798edab76d0918cf42d0c3184fa9"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^2.1.0"
klaw "^1.0.0"
path-is-absolute "^1.0.0"
rimraf "^2.2.8"

fs-promise@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/fs-promise/-/fs-promise-0.5.0.tgz#4347d6bf624655a7061a4319213c393276ad3ef3"
dependencies:
any-promise "^1.0.0"
fs-extra "^0.26.5"
mz "^2.3.1"
thenify-all "^1.6.0"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -1434,7 +1457,7 @@ got@^7.0.0:
timed-out "^4.0.0"
url-parse-lax "^1.0.0"

graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6:
graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

Expand Down Expand Up @@ -1621,10 +1644,6 @@ inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1,
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

inherits@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-1.0.2.tgz#ca4309dadee6b54cc0b8d247e8d7c7a0975bdc9b"

ini@^1.3.0, ini@^1.3.4, ini@~1.3.0:
version "1.3.4"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"
Expand Down Expand Up @@ -1935,6 +1954,12 @@ json3@3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1"

jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
optionalDependencies:
graceful-fs "^4.1.6"

jsonfile@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-3.0.0.tgz#92e7c7444e5ffd5fa32e6a9ae8b85034df8347d0"
Expand Down Expand Up @@ -1970,6 +1995,12 @@ kind-of@^4.0.0:
dependencies:
is-buffer "^1.1.5"

klaw@^1.0.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/klaw/-/klaw-1.3.1.tgz#4088433b46b3b1ba259d78785d8e96f73ba02439"
optionalDependencies:
graceful-fs "^4.1.9"

knex-migrator@2.0.16:
version "2.0.16"
resolved "https://registry.yarnpkg.com/knex-migrator/-/knex-migrator-2.0.16.tgz#6ac051d4e0fb2a8b633f8aab8b4d0019ecd4dd65"
Expand Down Expand Up @@ -2426,6 +2457,14 @@ mysql@^2.11.1:
readable-stream "1.1.14"
sqlstring "2.2.0"

mz@^2.3.1:
version "2.6.0"
resolved "https://registry.yarnpkg.com/mz/-/mz-2.6.0.tgz#c8b8521d958df0a4f2768025db69c719ee4ef1ce"
dependencies:
any-promise "^1.0.0"
object-assign "^4.0.1"
thenify-all "^1.0.0"

nan@^2.3.3, nan@~2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.4.0.tgz#fb3c59d45fe4effe215f0b890f8adf6eb32d2232"
Expand Down Expand Up @@ -2904,6 +2943,12 @@ rc@^1.0.1, rc@^1.1.2, rc@^1.1.6, rc@^1.1.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

read-last-lines@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/read-last-lines/-/read-last-lines-1.2.0.tgz#6970f68a2d5d03e1530b4b39373c00b941104890"
dependencies:
fs-promise "^0.5.0"

read-pkg-up@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be"
Expand Down Expand Up @@ -2934,15 +2979,6 @@ readable-stream@1.1.14, readable-stream@^1.1.12:
isarray "0.0.1"
string_decoder "~0.10.x"

"readable-stream@>=1.0.33-1 <1.1.0-0":
version "1.0.34"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "0.0.1"
string_decoder "~0.10.x"

readable-stream@^2.0.0, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.1.5, readable-stream@^2.2.2:
version "2.3.1"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.1.tgz#84e26965bb9e785535ed256e8d38e92c69f09d10"
Expand Down Expand Up @@ -3291,14 +3327,6 @@ slice-ansi@0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/slice-ansi/-/slice-ansi-0.0.4.tgz#edbf8903f66f7ce2f8eafd6ceed65e264c831b35"

slice-file@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slice-file/-/slice-file-1.0.0.tgz#8b6c79d0a50c3fc3c1159957478426443a9000b8"
dependencies:
inherits "~1.0.0"
split2 "^0.2.1"
through2 "^0.6.5"

slide@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/slide/-/slide-1.1.6.tgz#56eb027d65b4d2dce6cb2e2d32c4d4afc9e1d707"
Expand Down Expand Up @@ -3351,12 +3379,6 @@ spdx-license-ids@^1.0.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-1.2.2.tgz#c9df7a3424594ade6bd11900d596696dc06bac57"

split2@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/split2/-/split2-0.2.1.tgz#02ddac9adc03ec0bb78c1282ec079ca6e85ae900"
dependencies:
through2 "~0.6.1"

sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
Expand Down Expand Up @@ -3492,6 +3514,10 @@ table@^4.0.1:
slice-ansi "0.0.4"
string-width "^2.0.0"

tail@1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/tail/-/tail-1.2.2.tgz#3c40a47d53e137c541a14cc133532ecb2a75cc51"

tar-fs@^1.15.1:
version "1.15.3"
resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-1.15.3.tgz#eccf935e941493d8151028e636e51ce4c3ca7f20"
Expand Down Expand Up @@ -3552,12 +3578,17 @@ text-table@~0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"

through2@^0.6.5, through2@~0.6.1:
version "0.6.5"
resolved "https://registry.yarnpkg.com/through2/-/through2-0.6.5.tgz#41ab9c67b29d57209071410e1d7a7a968cd3ad48"
thenify-all@^1.0.0, thenify-all@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/thenify-all/-/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726"
dependencies:
thenify ">= 3.1.0 < 4"

"thenify@>= 3.1.0 < 4":
version "3.3.0"
resolved "https://registry.yarnpkg.com/thenify/-/thenify-3.3.0.tgz#e69e38a1babe969b0108207978b9f62b88604839"
dependencies:
readable-stream ">=1.0.33-1 <1.1.0-0"
xtend ">=4.0.0 <4.1.0-0"
any-promise "^1.0.0"

through2@^2.0.3:
version "2.0.3"
Expand Down Expand Up @@ -3820,7 +3851,7 @@ xdg-basedir@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4"

"xtend@>=4.0.0 <4.1.0-0", xtend@^4.0.0, xtend@~4.0.1:
xtend@^4.0.0, xtend@~4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

Expand Down

0 comments on commit 3c56f2e

Please sign in to comment.