This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.ts
90 lines (80 loc) · 2.6 KB
/
logs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import BaseCommand from "../base-command";
import { flags } from "@oclif/command";
import { loadApp } from "@shipengine/connect-loader";
import Login from './login';
import { ApiClientErrors } from '../core/api-client'
import { parseDIPLogs } from '../core/utils/dip-log-helpers';
export default class Logs extends BaseCommand {
public static description = "Get the logs for your app";
static flags = {
help: flags.help({
char: "h",
description: "Show help for the logs command",
}),
debug: flags.boolean({
char: "d",
description: "Show network debugging information",
default: false,
hidden: true
}),
lines: flags.string({
char: "l",
default: "500",
description: "The number of lines of logs to show from the server, max of 1500"
}),
all: flags.boolean({
char: "a",
default: false,
description: "Show internal logs along with the app developer related ones"
}),
format: flags.string({
char: "f",
description: "The format the logs get shown in",
options: ["default", "raw"],
default: "default"
})
};
async run(): Promise<void> {
// When the -h flag is present the following line haults execution
const { flags } = this.parse(Logs);
// Verify user is logged in
try {
await this.getCurrentUser(flags.debug);
} catch {
await Login.run([])
}
try {
const pathToApp = process.cwd();
const app = await loadApp(pathToApp);
const apiClient = await this.apiClient(flags.debug)
const platformApp = await apiClient.apps.getByName(app.manifest.name);
const paginatedDeployments = await apiClient.deployments.getAllForAppId(
platformApp.id,
);
const latestDeployment = paginatedDeployments.items[0];
const logs = await apiClient.deployments.getLogsById({ deployId: latestDeployment.deployId, appId: platformApp.id })
if (flags.format !== "raw") {
const parsedLogs = parseDIPLogs(logs, Number(flags.lines), flags.all);
parsedLogs.map(log => this.log(log));
}
else {
this.log(logs);
}
} catch (error) {
switch (error.code) {
case "ERR_APP_ERROR":
return this.error("Error loading your app - please make sure you are in an app directory", {
exit: 1,
});
case ApiClientErrors.NotFound:
return this.error("This app has not been published yet", {
exit: 1,
});
default:
return this.error("Error retrieving app info", {
exit: 1,
});
}
}
}
}