@pluginfinity/cli 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli/program.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { buildCommand } from "../commands/build.js";
2
2
  import { doctorCommand } from "../commands/doctor.js";
3
3
  import { initCommand } from "../commands/init.js";
4
+ import { logsCommand } from "../commands/logs.js";
4
5
  import { pluginCommand } from "../commands/plugin.js";
5
6
  import { validateCommand } from "../commands/validate.js";
6
7
  import { CliAudience } from "@effected/cli";
@@ -12,7 +13,8 @@ const root = (launch) => Command.make("pluginfinity").pipe(Command.withDescripti
12
13
  pluginCommand,
13
14
  buildCommand(launch),
14
15
  validateCommand(launch),
15
- doctorCommand(launch)
16
+ doctorCommand(launch),
17
+ logsCommand(launch)
16
18
  ]));
17
19
  /**
18
20
  * Parse `args` and run the selected command, with the audience flags
package/cli/run.js CHANGED
@@ -17,7 +17,8 @@ const run = (args, options) => Effect.gen(function* () {
17
17
  const deps = {
18
18
  version: options.version,
19
19
  cwd: options.cwd,
20
- nodeVersion: options.nodeVersion
20
+ nodeVersion: options.nodeVersion,
21
+ stateHome: options.stateHome
21
22
  };
22
23
  return yield* CliRuntime.main(program(args, deps).pipe(Effect.provide(options.tools ?? ToolDiscoveryLive)), {
23
24
  platform: options.platform,
@@ -0,0 +1,87 @@
1
+ import { reportFindings } from "../render/config-error.js";
2
+ import { checkPluginName } from "./name.js";
3
+ import { entryJson, logsJson, logsLines, sectionHeader } from "../render/logs.js";
4
+ import { Command, Flag } from "effect/cli";
5
+ import { CurrentDistribution } from "@effected/engine";
6
+ import { Audience } from "@effected/env";
7
+ import { configPluginNames, listLogPlugins, logRoot, parseLogLine, readLog, readLogFrom } from "@pluginfinity/engine";
8
+ import { Console, Context, Effect, FileSystem, Option } from "effect";
9
+
10
+ //#region src/commands/logs.ts
11
+ /** A follow that never stops by itself, checking twice a second. */
12
+ const DEFAULT_FOLLOW = {
13
+ stop: Effect.never,
14
+ interval: "500 millis",
15
+ onStart: () => {}
16
+ };
17
+ /** The `FollowControl` a `logs --follow` runs under. */
18
+ const CurrentFollow = Context.Reference("@pluginfinity/cli/CurrentFollow", { defaultValue: () => DEFAULT_FOLLOW });
19
+ /** The plugin names to read: `--plugin`, else the nearest config's, else every directory under the state directory. */
20
+ const resolvePlugins = (launch, explicit) => explicit.length > 0 ? Effect.succeed(explicit) : configPluginNames({
21
+ _tag: "Nearest",
22
+ start: launch.cwd
23
+ }).pipe(Effect.catchTag("ConfigNotFound", () => listLogPlugins(launch.stateHome)));
24
+ /** Print lines appended to the followed files until `stop` completes, checking every `interval`. */
25
+ const follow = (tails, control, print) => Effect.gen(function* () {
26
+ const offsets = new Map(tails.map((tail) => [tail.path, tail.offset]));
27
+ yield* Effect.forever(Effect.gen(function* () {
28
+ for (const tail of tails) {
29
+ const next = yield* readLogFrom(tail.path, offsets.get(tail.path) ?? 0);
30
+ offsets.set(tail.path, next.offset);
31
+ for (const line of next.lines) yield* print(tail, line);
32
+ }
33
+ yield* Effect.sleep(control.interval);
34
+ }));
35
+ }).pipe((loop) => Effect.raceFirst(loop, control.stop));
36
+ /** `pluginfinity logs`: read the logs plugins write under the XDG state directory. */
37
+ const logsCommand = (launch) => Command.make("logs", {
38
+ plugin: Flag.String("plugin").pipe(Flag.atLeast(0), Flag.withDescription("a plugin name; repeatable (default: the config's plugins, else every plugin with logs)")),
39
+ debug: Flag.Boolean("debug").pipe(Flag.withDefault(false), Flag.withDescription("show debug.log instead of error.log")),
40
+ follow: Flag.Boolean("follow").pipe(Flag.withAlias("f"), Flag.withDefault(false), Flag.withDescription("keep printing lines as they are appended, until interrupted")),
41
+ lines: Flag.Int("lines").pipe(Flag.withAlias("n"), Flag.filter((n) => n >= 0, (n) => `--lines must be zero or more, got ${n}`), Flag.withDefault(50), Flag.withDescription("how many of the last lines of each file to show"))
42
+ }, (input) => Effect.gen(function* () {
43
+ const fs = yield* FileSystem.FileSystem;
44
+ const human = (yield* Audience).kind === "human";
45
+ const file = input.debug ? "debug.log" : "error.log";
46
+ const explicit = yield* Effect.forEach(input.plugin, (name) => checkPluginName("--plugin", name));
47
+ const plugins = yield* resolvePlugins(launch, explicit);
48
+ const root = yield* logRoot(launch.stateHome);
49
+ const rootExists = yield* fs.exists(root);
50
+ const tails = [];
51
+ for (const plugin of plugins) tails.push(yield* readLog({
52
+ stateHome: launch.stateHome,
53
+ plugin,
54
+ file,
55
+ lines: input.lines
56
+ }));
57
+ const view = {
58
+ root,
59
+ rootExists,
60
+ tails,
61
+ limit: input.lines
62
+ };
63
+ if (!input.follow) {
64
+ if (human) for (const line of logsLines(view)) yield* Console.log(line);
65
+ else yield* Console.log(logsJson(view, Option.getOrNull(yield* CurrentDistribution)));
66
+ return;
67
+ }
68
+ const control = yield* CurrentFollow;
69
+ control.onStart();
70
+ let shown;
71
+ if (human) {
72
+ for (const line of logsLines(view)) yield* Console.log(line);
73
+ const last = tails.at(-1);
74
+ shown = last === void 0 ? void 0 : last.path;
75
+ } else for (const tail of tails) for (const line of tail.lines) yield* Console.log(entryJson(parseLogLine(line, tail.plugin, tail.file)));
76
+ yield* follow(tails, control, (tail, line) => Effect.gen(function* () {
77
+ if (!human) return yield* Console.log(entryJson(parseLogLine(line, tail.plugin, tail.file)));
78
+ if (shown !== tail.path) {
79
+ shown = tail.path;
80
+ yield* Console.log(sectionHeader(tail.plugin, tail.file));
81
+ }
82
+ yield* Console.log(line);
83
+ }));
84
+ }).pipe(reportFindings)).pipe(Command.withDescription("Show the logs plugins write under the XDG state directory, optionally following them"));
85
+
86
+ //#endregion
87
+ export { CurrentFollow, DEFAULT_FOLLOW, logsCommand };
package/index.d.ts CHANGED
@@ -17,6 +17,8 @@ interface LaunchFacts {
17
17
  readonly cwd: string;
18
18
  /** The running Node.js version, without a leading `v`. */
19
19
  readonly nodeVersion: string;
20
+ /** The XDG state directory (`$XDG_STATE_HOME`, else `~/.local/state`): where `logs` finds `pluginfinity/<plugin>/`. */
21
+ readonly stateHome: string;
20
22
  }
21
23
  //#endregion
22
24
  //#region src/cli/program.d.ts
package/main.js CHANGED
@@ -1,12 +1,14 @@
1
+ import { CurrentFollow, DEFAULT_FOLLOW } from "./commands/logs.js";
1
2
  import { run } from "./cli/run.js";
2
3
  import { CLI_VERSION } from "./version.js";
3
4
  import { CurrentDistribution } from "@effected/engine";
4
- import { Effect, Option, Runtime } from "effect";
5
+ import { Cause, Effect, Exit, Option, Runtime } from "effect";
6
+ import { homedir } from "node:os";
5
7
  import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
8
  import * as NodeServices from "@effect/platform-node/NodeServices";
7
9
 
8
10
  //#region src/main.ts
9
- const exitAfterFlush = (exit, onExit) => Runtime.defaultTeardown(exit, (code) => {
11
+ const exitAfterFlush = (following) => (exit, onExit) => Runtime.defaultTeardown(following() && !Exit.isSuccess(exit) && Cause.hasInterruptsOnly(exit.cause) ? Exit.void : exit, (code) => {
10
12
  process.stdout.write("", () => {
11
13
  onExit(code);
12
14
  process.exit(code);
@@ -21,13 +23,21 @@ const exitAfterFlush = (exit, onExit) => Runtime.defaultTeardown(exit, (code) =>
21
23
  */
22
24
  const main = (options = {}) => {
23
25
  const argv = process.argv.slice(2);
26
+ let following = false;
27
+ const stateHome = process.env.XDG_STATE_HOME || `${process.env.HOME || homedir()}/.local/state`;
24
28
  NodeRuntime.runMain(run(argv, {
25
29
  version: CLI_VERSION,
26
30
  cwd: process.cwd(),
27
31
  nodeVersion: process.versions.node,
32
+ stateHome,
28
33
  platform: NodeServices.layer,
29
34
  stderrIsTerminal: Effect.sync(() => process.stderr.isTTY === true)
30
- }).pipe(Effect.provideService(CurrentDistribution, Option.fromNullishOr(options.distribution))), { teardown: exitAfterFlush });
35
+ }).pipe(Effect.provideService(CurrentDistribution, Option.fromNullishOr(options.distribution)), Effect.provideService(CurrentFollow, {
36
+ ...DEFAULT_FOLLOW,
37
+ onStart: () => {
38
+ following = true;
39
+ }
40
+ })), { teardown: exitAfterFlush(() => following) });
31
41
  };
32
42
 
33
43
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluginfinity/cli",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "description": "The pluginfinity command-line front end. Install the pluginfinity package to get the pluginfinity bin.",
6
6
  "keywords": [
@@ -48,9 +48,9 @@
48
48
  "@effected/glob": "^0.10.0",
49
49
  "@effected/semver": "^0.11.0",
50
50
  "@effected/walker": "^0.15.0",
51
- "@pluginfinity/core": "0.2.1",
52
- "@pluginfinity/engine": "0.2.1",
53
- "@pluginfinity/targets": "0.2.1",
51
+ "@pluginfinity/core": "0.3.0",
52
+ "@pluginfinity/engine": "0.3.0",
53
+ "@pluginfinity/targets": "0.3.0",
54
54
  "effect": "^4.0.1",
55
55
  "jiti": "^2.6.1"
56
56
  },
package/render/logs.js ADDED
@@ -0,0 +1,35 @@
1
+ import { ENGINE_VERSION, parseLogLine } from "@pluginfinity/engine";
2
+
3
+ //#region src/render/logs.ts
4
+ /** The header that opens a file's section. */
5
+ const sectionHeader = (plugin, file) => `==> ${plugin}/${file} <==`;
6
+ const absentLine = (file) => file === "debug.log" ? " (no debug.log; set PLUGINFINITY_DEBUG=1 to write one)" : " (no error.log yet; nothing has been logged)";
7
+ /** Whether anything was found to show: a log root with at least one selected plugin. */
8
+ const isFound = (view) => view.rootExists && view.tails.length > 0;
9
+ /** The lines a person reads: the directory, then a headed section per file. */
10
+ const logsLines = (view) => {
11
+ if (!isFound(view)) return [`No logs yet: ${view.root} ${view.rootExists ? "has no plugin directories" : "does not exist"}.`, "A plugin's components write there when they log an error; set PLUGINFINITY_DEBUG=1 to write debug.log too."];
12
+ return [`Logs in ${view.root}`, ...view.tails.flatMap((tail) => [sectionHeader(tail.plugin, tail.file), ...!tail.present ? [absentLine(tail.file)] : tail.lines.length === 0 ? view.limit === 0 ? [] : [" (no entries)"] : tail.lines])];
13
+ };
14
+ /** One entry as `--agent` and `--ci` read it. */
15
+ const entryJson = (entry) => JSON.stringify(entry);
16
+ /** The entries of every tail, parsed, oldest first within each file. */
17
+ const tailEntries = (tails) => tails.flatMap((tail) => tail.lines.map((line) => parseLogLine(line, tail.plugin, tail.file)));
18
+ /** The one JSON object a non-following `logs` prints for agents and CI. */
19
+ const logsJson = (view, distribution) => JSON.stringify({
20
+ engine_version: ENGINE_VERSION,
21
+ distribution,
22
+ ok: true,
23
+ directory: view.root,
24
+ found: view.tails.some((tail) => tail.present),
25
+ files: view.tails.map((tail) => ({
26
+ plugin: tail.plugin,
27
+ file: tail.file,
28
+ path: tail.path,
29
+ present: tail.present
30
+ })),
31
+ entries: tailEntries(view.tails)
32
+ });
33
+
34
+ //#endregion
35
+ export { entryJson, isFound, logsJson, logsLines, sectionHeader, tailEntries };
package/version.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * with this package's own version, so this is not a runtime environment read.
9
9
  * `"0.0.0"` is the unbuilt-source fallback.
10
10
  */
11
- const CLI_VERSION = "0.2.1";
11
+ const CLI_VERSION = "0.3.0";
12
12
 
13
13
  //#endregion
14
14
  export { CLI_VERSION };