@eserstack/codebase 0.0.0-stage → 5.0.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.
Files changed (175) hide show
  1. package/README.md +301 -2
  2. package/dist/changelog-gen.d.ts +105 -0
  3. package/dist/changelog-gen.js +498 -0
  4. package/dist/changelog-text.d.ts +39 -0
  5. package/dist/changelog-text.js +82 -0
  6. package/dist/cli-support.d.ts +140 -0
  7. package/dist/cli-support.js +244 -0
  8. package/dist/cli-system/app-opts.d.ts +15 -0
  9. package/dist/cli-system/app-opts.js +13 -0
  10. package/dist/cli-system/app.d.ts +29 -0
  11. package/dist/cli-system/app.js +12 -0
  12. package/dist/cli-system/handlers/completions-setup.d.ts +14 -0
  13. package/dist/cli-system/handlers/completions-setup.js +139 -0
  14. package/dist/cli-system/handlers/completions.d.ts +3 -0
  15. package/dist/cli-system/handlers/completions.js +79 -0
  16. package/dist/cli-system/handlers/doctor.d.ts +8 -0
  17. package/dist/cli-system/handlers/doctor.js +146 -0
  18. package/dist/cli-system/handlers/info.d.ts +3 -0
  19. package/dist/cli-system/handlers/info.js +69 -0
  20. package/dist/cli-system/handlers/install.d.ts +8 -0
  21. package/dist/cli-system/handlers/install.js +137 -0
  22. package/dist/cli-system/handlers/mod.d.ts +12 -0
  23. package/dist/cli-system/handlers/mod.js +13 -0
  24. package/dist/cli-system/handlers/uninstall.d.ts +3 -0
  25. package/dist/cli-system/handlers/uninstall.js +60 -0
  26. package/dist/cli-system/handlers/update.d.ts +76 -0
  27. package/dist/cli-system/handlers/update.js +310 -0
  28. package/dist/cli-system/handlers/version-check.d.ts +16 -0
  29. package/dist/cli-system/handlers/version-check.js +176 -0
  30. package/dist/cli-system/handlers/version.d.ts +3 -0
  31. package/dist/cli-system/handlers/version.js +45 -0
  32. package/dist/cli-system/mod.d.ts +19 -0
  33. package/dist/cli-system/mod.js +52 -0
  34. package/dist/commitmsg.d.ts +28 -0
  35. package/dist/commitmsg.js +232 -0
  36. package/dist/directive-analysis.d.ts +75 -0
  37. package/dist/directive-analysis.js +223 -0
  38. package/dist/ffi-client.d.ts +11 -0
  39. package/dist/ffi-client.js +12 -0
  40. package/dist/file-tool.d.ts +96 -0
  41. package/dist/file-tool.js +274 -0
  42. package/dist/file-tools-shared.d.ts +81 -0
  43. package/dist/file-tools-shared.js +276 -0
  44. package/dist/gh-contributors.d.ts +89 -0
  45. package/dist/gh-contributors.js +205 -0
  46. package/dist/gh.d.ts +3 -0
  47. package/dist/gh.js +64 -0
  48. package/dist/git-listing.d.ts +27 -0
  49. package/dist/git-listing.js +69 -0
  50. package/dist/git.d.ts +172 -0
  51. package/dist/git.js +277 -0
  52. package/dist/install.d.ts +34 -0
  53. package/dist/install.js +421 -0
  54. package/dist/main.d.ts +22 -0
  55. package/dist/main.js +14 -0
  56. package/dist/mod.d.ts +25 -0
  57. package/dist/mod.js +8 -0
  58. package/dist/module.d.ts +10 -0
  59. package/dist/module.js +221 -0
  60. package/dist/native-functions.d.ts +1088 -0
  61. package/dist/native-functions.js +2 -0
  62. package/dist/npm-externals.d.ts +27 -0
  63. package/dist/npm-externals.js +34 -0
  64. package/dist/npm-workspace-specifiers.d.ts +6 -0
  65. package/dist/npm-workspace-specifiers.js +45 -0
  66. package/dist/package/loader.d.ts +34 -0
  67. package/dist/package/loader.js +203 -0
  68. package/dist/package/mod.d.ts +36 -0
  69. package/dist/package/mod.js +5 -0
  70. package/dist/package/types.d.ts +142 -0
  71. package/dist/package/types.js +32 -0
  72. package/dist/package/workspace.d.ts +33 -0
  73. package/dist/package/workspace.js +158 -0
  74. package/dist/package/writer.d.ts +52 -0
  75. package/dist/package/writer.js +278 -0
  76. package/dist/package.json +275 -0
  77. package/dist/release-notes.d.ts +90 -0
  78. package/dist/release-notes.js +236 -0
  79. package/dist/release-tag.d.ts +46 -0
  80. package/dist/release-tag.js +95 -0
  81. package/dist/release.d.ts +145 -0
  82. package/dist/release.js +584 -0
  83. package/dist/source-mask.d.ts +3 -0
  84. package/dist/source-mask.js +185 -0
  85. package/dist/validate-bom.d.ts +5 -0
  86. package/dist/validate-bom.js +43 -0
  87. package/dist/validate-case-conflict.d.ts +5 -0
  88. package/dist/validate-case-conflict.js +43 -0
  89. package/dist/validate-circular-deps.d.ts +11 -0
  90. package/dist/validate-circular-deps.js +53 -0
  91. package/dist/validate-code-conventions.d.ts +22 -0
  92. package/dist/validate-code-conventions.js +166 -0
  93. package/dist/validate-commit-msg.d.ts +50 -0
  94. package/dist/validate-commit-msg.js +182 -0
  95. package/dist/validate-docs.d.ts +20 -0
  96. package/dist/validate-docs.js +78 -0
  97. package/dist/validate-eof.d.ts +5 -0
  98. package/dist/validate-eof.js +61 -0
  99. package/dist/validate-error-coverage.d.ts +28 -0
  100. package/dist/validate-error-coverage.js +93 -0
  101. package/dist/validate-export-names.d.ts +17 -0
  102. package/dist/validate-export-names.js +56 -0
  103. package/dist/validate-filenames.d.ts +5 -0
  104. package/dist/validate-filenames.js +151 -0
  105. package/dist/validate-json.d.ts +5 -0
  106. package/dist/validate-json.js +71 -0
  107. package/dist/validate-large-files.d.ts +5 -0
  108. package/dist/validate-large-files.js +41 -0
  109. package/dist/validate-laroux-boundaries.d.ts +19 -0
  110. package/dist/validate-laroux-boundaries.js +59 -0
  111. package/dist/validate-licenses.d.ts +5 -0
  112. package/dist/validate-licenses.js +137 -0
  113. package/dist/validate-line-endings.d.ts +5 -0
  114. package/dist/validate-line-endings.js +47 -0
  115. package/dist/validate-merge-conflict.d.ts +5 -0
  116. package/dist/validate-merge-conflict.js +46 -0
  117. package/dist/validate-mod-exports.d.ts +16 -0
  118. package/dist/validate-mod-exports.js +56 -0
  119. package/dist/validate-naming-and-todos.d.ts +5 -0
  120. package/dist/validate-naming-and-todos.js +70 -0
  121. package/dist/validate-runtime-js-apis.d.ts +5 -0
  122. package/dist/validate-runtime-js-apis.js +78 -0
  123. package/dist/validate-secrets.d.ts +5 -0
  124. package/dist/validate-secrets.js +82 -0
  125. package/dist/validate-self-imports.d.ts +5 -0
  126. package/dist/validate-self-imports.js +78 -0
  127. package/dist/validate-server-loc.d.ts +32 -0
  128. package/dist/validate-server-loc.js +94 -0
  129. package/dist/validate-shebangs.d.ts +5 -0
  130. package/dist/validate-shebangs.js +33 -0
  131. package/dist/validate-stream.d.ts +25 -0
  132. package/dist/validate-stream.js +62 -0
  133. package/dist/validate-submodules.d.ts +5 -0
  134. package/dist/validate-submodules.js +43 -0
  135. package/dist/validate-symlinks.d.ts +5 -0
  136. package/dist/validate-symlinks.js +46 -0
  137. package/dist/validate-toml.d.ts +5 -0
  138. package/dist/validate-toml.js +40 -0
  139. package/dist/validate-trailing-whitespace.d.ts +5 -0
  140. package/dist/validate-trailing-whitespace.js +49 -0
  141. package/dist/validate-yaml.d.ts +5 -0
  142. package/dist/validate-yaml.js +40 -0
  143. package/dist/validation/config.d.ts +17 -0
  144. package/dist/validation/config.js +30 -0
  145. package/dist/validation/mod.d.ts +71 -0
  146. package/dist/validation/mod.js +479 -0
  147. package/dist/validation/registry.d.ts +57 -0
  148. package/dist/validation/registry.js +256 -0
  149. package/dist/validation/types.d.ts +101 -0
  150. package/dist/validation/types.js +2 -0
  151. package/dist/validation/validators/circular-deps.d.ts +7 -0
  152. package/dist/validation/validators/circular-deps.js +31 -0
  153. package/dist/validation/validators/docs.d.ts +7 -0
  154. package/dist/validation/validators/docs.js +38 -0
  155. package/dist/validation/validators/error-coverage.d.ts +2 -0
  156. package/dist/validation/validators/error-coverage.js +46 -0
  157. package/dist/validation/validators/export-names.d.ts +7 -0
  158. package/dist/validation/validators/export-names.js +31 -0
  159. package/dist/validation/validators/laroux-boundaries.d.ts +2 -0
  160. package/dist/validation/validators/laroux-boundaries.js +29 -0
  161. package/dist/validation/validators/mod-exports.d.ts +7 -0
  162. package/dist/validation/validators/mod-exports.js +32 -0
  163. package/dist/validation/validators/mod.d.ts +11 -0
  164. package/dist/validation/validators/mod.js +12 -0
  165. package/dist/validation/validators/server-loc.d.ts +2 -0
  166. package/dist/validation/validators/server-loc.js +48 -0
  167. package/dist/versions.d.ts +166 -0
  168. package/dist/versions.js +349 -0
  169. package/dist/violation-baseline.d.ts +58 -0
  170. package/dist/violation-baseline.js +144 -0
  171. package/dist/walk-stream.d.ts +24 -0
  172. package/dist/walk-stream.js +55 -0
  173. package/dist/workspace-discovery.d.ts +60 -0
  174. package/dist/workspace-discovery.js +130 -0
  175. package/package.json +222 -3
@@ -0,0 +1,79 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * Completions command handler - generates shell completion scripts
4
+ *
5
+ * @module
6
+ */
7
+ import * as span from "@eserstack/streams/span";
8
+ import * as streams from "@eserstack/streams";
9
+ import * as results from "@eserstack/primitives/results";
10
+ import * as shellEnv from "@eserstack/shell/env";
11
+ const getInstallInstructions = (shell, appName) => {
12
+ const config = shellEnv.getShellConfig(shell, appName);
13
+ const renderer = streams.renderers.ansi();
14
+ // Every shell gets a generated file now, so the instructions are the same
15
+ // shape for all of them: write it once, then load it. fish discovers its
16
+ // completions directory automatically and needs no rc line; zsh and bash need
17
+ // one, but it SOURCES a file rather than eval-ing a command substitution --
18
+ // the latter booted the CLI on every terminal open.
19
+ const dir = config.completionsFile !== undefined
20
+ ? config.completionsFile.slice(0, config.completionsFile.lastIndexOf("/"))
21
+ : "";
22
+ const write = `mkdir -p ${dir} && ${appName} system completions --shell ${shell} > ${config.completionsFile}`;
23
+ if (shell === "fish") {
24
+ return `
25
+ To install, run:
26
+
27
+ ${renderer.render([span.dim(write)])}
28
+ `;
29
+ }
30
+ const sourceLine = shellEnv.getCompletionSourceLine(shell, appName);
31
+ return `
32
+ To install, run:
33
+
34
+ ${renderer.render([span.dim(write)])}
35
+
36
+ Then add the following to your ${renderer.render([span.cyan(config.rcFile)])}:
37
+
38
+ ${renderer.render([span.dim(sourceLine)])}
39
+ `;
40
+ };
41
+ export const completionsHandler = (ctx, app) => {
42
+ const shellFlag = ctx.flags["shell"];
43
+ const renderer = streams.renderers.ansi();
44
+ const out = streams.output({
45
+ renderer,
46
+ sink: streams.sinks.stdout(),
47
+ });
48
+ let shell;
49
+ if (shellFlag !== undefined) {
50
+ if (!["bash", "zsh", "fish"].includes(shellFlag)) {
51
+ return results.fail({
52
+ message: `${renderer.render([span.red(`Invalid shell: ${shellFlag}`)])}\n` +
53
+ "Supported shells: bash, zsh, fish",
54
+ exitCode: 1,
55
+ });
56
+ }
57
+ shell = shellFlag;
58
+ }
59
+ else {
60
+ shell = shellEnv.detectShell();
61
+ }
62
+ // Get the root command and generate completions from its tree
63
+ const rootCommand = ctx.root;
64
+ const script = rootCommand.completions(shell);
65
+ // If shell was auto-detected, show instructions
66
+ if (shellFlag === undefined) {
67
+ out.writeln(span.text("Generating "), span.cyan(shell), span.text(" completions..."));
68
+ // Plain text written synchronously, like the script below: this handler
69
+ // is synchronous and never flushes the ANSI output above.
70
+ // oxlint-disable-next-line no-console
71
+ console.log(getInstallInstructions(shell, app.command));
72
+ out.writeln(span.dim("--- Completion script ---\n"));
73
+ }
74
+ // The script must reach stdout unrendered and before the handler returns,
75
+ // so `eval "$(... completions)"` and redirects to a file get it intact.
76
+ // oxlint-disable-next-line no-console
77
+ console.log(script);
78
+ return results.ok(undefined);
79
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Doctor command handler - runs diagnostic checks on the user's environment
3
+ *
4
+ * @module
5
+ */
6
+ import type { CliApp } from "../app.ts";
7
+ import type * as shellArgs from "@eserstack/shell/args";
8
+ export declare const doctorHandler: (_ctx: shellArgs.CommandContext, app: CliApp) => Promise<shellArgs.CliResult<void>>;
@@ -0,0 +1,146 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ import * as appOpts from "../app-opts.js";
3
+ import * as results from "@eserstack/primitives/results";
4
+ import * as shellExec from "@eserstack/shell/exec";
5
+ import * as span from "@eserstack/streams/span";
6
+ import * as streams from "@eserstack/streams";
7
+ import * as standardsCrossRuntime from "@eserstack/standards/cross-runtime";
8
+ import * as versionCheck from "./version-check.js";
9
+ const runtime = standardsCrossRuntime.runtime;
10
+ const LABEL_WIDTH = 17;
11
+ const ok = (out, label, value) => {
12
+ const padded = `${label}:`.padEnd(LABEL_WIDTH);
13
+ out.writeln(span.text(` ${padded}`), span.green("\u2713"), span.text(` ${value}`));
14
+ };
15
+ const fail = (out, label, value) => {
16
+ const padded = `${label}:`.padEnd(LABEL_WIDTH);
17
+ out.writeln(span.text(` ${padded}`), span.red("\u2717"), span.text(` ${value}`));
18
+ };
19
+ const info = (out, label, value) => {
20
+ const padded = `${label}:`.padEnd(LABEL_WIDTH);
21
+ out.writeln(span.text(` ${padded}${value}`));
22
+ };
23
+ const neutral = (out, label, value) => {
24
+ const padded = `${label}:`.padEnd(LABEL_WIDTH);
25
+ out.writeln(span.text(` ${padded}`), span.dim("-"), span.text(` ${value}`));
26
+ };
27
+ const fileExists = async (path) => {
28
+ try {
29
+ await runtime.fs.stat(path);
30
+ return true;
31
+ }
32
+ catch {
33
+ return false;
34
+ }
35
+ };
36
+ const readFileOrEmpty = async (path) => {
37
+ try {
38
+ return await runtime.fs.readTextFile(path);
39
+ }
40
+ catch {
41
+ return "";
42
+ }
43
+ };
44
+ const parseToolVersion = (output, pattern) => {
45
+ const match = output.match(pattern);
46
+ return match?.[1];
47
+ };
48
+ const checkGitHooks = async (out, appName) => {
49
+ const hookPath = runtime.path.join(".", ".git", "hooks", "pre-commit");
50
+ const exists = await fileExists(hookPath);
51
+ if (!exists) {
52
+ fail(out, "Git hooks", "Not installed");
53
+ return;
54
+ }
55
+ const content = await readFileOrEmpty(hookPath);
56
+ // The hook is ours if it invokes this binary. Hardcoding "eser"
57
+ // reported "Not installed" for a perfectly good noskills hook.
58
+ if (content.includes(appName)) {
59
+ ok(out, "Git hooks", "Installed");
60
+ }
61
+ else {
62
+ fail(out, "Git hooks", "Not installed");
63
+ }
64
+ };
65
+ const checkManifest = async (out) => {
66
+ const newPath = runtime.path.join(".", ".eser", "manifest.yml");
67
+ const legacyPath = runtime.path.join(".", ".manifest.yml");
68
+ const newExists = await fileExists(newPath);
69
+ if (newExists) {
70
+ ok(out, "Manifest", ".eser/manifest.yml found");
71
+ return;
72
+ }
73
+ const legacyExists = await fileExists(legacyPath);
74
+ if (legacyExists) {
75
+ ok(out, "Manifest", ".manifest.yml found (legacy path)");
76
+ return;
77
+ }
78
+ fail(out, "Manifest", ".eser/manifest.yml not found");
79
+ };
80
+ const checkGo = async (out) => {
81
+ try {
82
+ const output = await shellExec.exec `go version`.noThrow().quiet().text();
83
+ const version = parseToolVersion(output, /go(\d+\.\d+(?:\.\d+)?)/);
84
+ if (version !== undefined) {
85
+ ok(out, "Go", version);
86
+ }
87
+ else {
88
+ fail(out, "Go", "Not found");
89
+ }
90
+ }
91
+ catch {
92
+ fail(out, "Go", "Not found");
93
+ }
94
+ };
95
+ const checkNode = async (out) => {
96
+ try {
97
+ const output = await shellExec.exec `node --version`
98
+ .noThrow()
99
+ .quiet()
100
+ .text();
101
+ const version = output.trim();
102
+ if (version.length > 0) {
103
+ ok(out, "Node", version);
104
+ }
105
+ else {
106
+ fail(out, "Node", "Not found");
107
+ }
108
+ }
109
+ catch {
110
+ fail(out, "Node", "Not found");
111
+ }
112
+ };
113
+ export const doctorHandler = async (_ctx, app) => {
114
+ // Created per run, not at module load: importing must not touch stdout,
115
+ // and the run closes it at the end.
116
+ const out = streams.output({
117
+ renderer: streams.renderers.ansi(),
118
+ sink: streams.sinks.stdout(),
119
+ });
120
+ out.writeln(span.text("eser doctor\n"));
121
+ // Install method & version
122
+ const execContext = await standardsCrossRuntime.detectExecutionContext(appOpts.appOpts(app));
123
+ info(out, "Install method", `${execContext.invoker} (${execContext.mode})`);
124
+ info(out, "Version", _ctx.root.versionString ?? "unknown");
125
+ // Update check
126
+ const updateResult = await versionCheck.checkForUpdate(_ctx.root.versionString ?? "0.0.0");
127
+ if (updateResult === undefined) {
128
+ neutral(out, "Update", "Could not check for updates");
129
+ }
130
+ else if (updateResult.updateAvailable) {
131
+ fail(out, "Update", `Update available: v${updateResult.latestVersion}`);
132
+ }
133
+ else {
134
+ ok(out, "Update", "Up to date");
135
+ }
136
+ out.writeln();
137
+ // Project checks
138
+ await checkGitHooks(out, app.command);
139
+ await checkManifest(out);
140
+ out.writeln();
141
+ // Tool checks
142
+ await checkGo(out);
143
+ await checkNode(out);
144
+ await out.close();
145
+ return results.ok(undefined);
146
+ };
@@ -0,0 +1,3 @@
1
+ import type { CliApp } from "../app.ts";
2
+ import type * as shellArgs from "@eserstack/shell/args";
3
+ export declare const infoHandler: (_ctx: shellArgs.CommandContext, app: CliApp) => Promise<shellArgs.CliResult<void>>;
@@ -0,0 +1,69 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * Info command handler - shows runtime and execution context diagnostics
4
+ *
5
+ * @module
6
+ */
7
+ import * as appOpts from "../app-opts.js";
8
+ import * as results from "@eserstack/primitives/results";
9
+ import * as span from "@eserstack/streams/span";
10
+ import * as streams from "@eserstack/streams";
11
+ import * as standardsCrossRuntime from "@eserstack/standards/cross-runtime";
12
+ const runtime = standardsCrossRuntime.runtime;
13
+ const LABEL_WIDTH = 20;
14
+ const info = (out, label, value) => {
15
+ const padded = `${label}:`.padEnd(LABEL_WIDTH);
16
+ out.writeln(span.text(` ${padded}${value}`));
17
+ };
18
+ export const infoHandler = async (_ctx, app) => {
19
+ // Created per run, not at module load: importing must not touch stdout,
20
+ // and the run closes it at the end.
21
+ const out = streams.output({
22
+ renderer: streams.renderers.ansi(),
23
+ sink: streams.sinks.stdout(),
24
+ });
25
+ out.writeln(span.text("eser system info"));
26
+ // ── Runtime ──
27
+ out.writeln(span.dim("\n Runtime"));
28
+ info(out, "Name", runtime.name);
29
+ info(out, "Version", runtime.version);
30
+ info(out, "Platform", standardsCrossRuntime.getPlatform());
31
+ info(out, "Arch", standardsCrossRuntime.getArch());
32
+ const caps = runtime.capabilities;
33
+ const capList = Object.entries(caps)
34
+ .map(([k, v]) => `${k}=${v ? "yes" : "no"}`)
35
+ .join(", ");
36
+ info(out, "Capabilities", capList);
37
+ // ── Locations ──
38
+ out.writeln(span.dim("\n Locations"));
39
+ info(out, "Homedir", standardsCrossRuntime.getHomedir());
40
+ info(out, "Tmpdir", standardsCrossRuntime.getTmpdir());
41
+ info(out, "CWD", runtime.process.cwd());
42
+ info(out, "PID", String(runtime.process.pid));
43
+ info(out, "Exec path", runtime.process.execPath());
44
+ // ── Process ──
45
+ out.writeln(span.dim("\n Process"));
46
+ info(out, "argv0", runtime.process.argv0);
47
+ info(out, "argv", runtime.process.argv.join(" "));
48
+ info(out, "args", runtime.process.args.join(" "));
49
+ // ── Execution Context ──
50
+ out.writeln(span.dim("\n Execution Context"));
51
+ const ctx = await standardsCrossRuntime.detectExecutionContext(appOpts.appOpts(app));
52
+ info(out, "Runtime", ctx.runtime);
53
+ info(out, "Invoker", `${ctx.invoker} (${ctx.mode})`);
54
+ info(out, "Command", ctx.command);
55
+ info(out, "In PATH", ctx.isInPath ? "yes" : "no");
56
+ // ── Environment (selected) ──
57
+ out.writeln(span.dim("\n Environment"));
58
+ const envKeys = ["PATH", "HOME", "SHELL", "TERM", "NODE_ENV"];
59
+ for (const key of envKeys) {
60
+ const value = runtime.env.get(key);
61
+ if (value !== undefined) {
62
+ const display = key === "PATH" ? `(${value.split(":").length} entries)` : value;
63
+ info(out, key, display);
64
+ }
65
+ }
66
+ out.writeln();
67
+ await out.close();
68
+ return results.ok(undefined);
69
+ };
@@ -0,0 +1,8 @@
1
+ import type { CliApp } from "../app.ts";
2
+ import type * as shellArgs from "@eserstack/shell/args";
3
+ /**
4
+ * Whether a file operation was refused for lack of permission: the runtime's
5
+ * PermissionDeniedError, or a raw EACCES or EPERM from node:fs.
6
+ */
7
+ export declare const isPermissionDenied: (error: unknown) => boolean;
8
+ export declare const installHandler: (_ctx: shellArgs.CommandContext, app: CliApp) => Promise<shellArgs.CliResult<void>>;
@@ -0,0 +1,137 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * Install command handler — installs eser CLI globally.
4
+ *
5
+ * For compiled binaries: copies self to ~/.local/bin or /usr/local/bin + sets up completions.
6
+ * For Node installs: runs `npm install -g`.
7
+ *
8
+ * @module
9
+ */
10
+ import * as span from "@eserstack/streams/span";
11
+ import * as streams from "@eserstack/streams";
12
+ import * as standardsCrossRuntime from "@eserstack/standards/cross-runtime";
13
+ import * as appOpts from "../app-opts.js";
14
+ import * as results from "@eserstack/primitives/results";
15
+ import * as shellExec from "@eserstack/shell/exec";
16
+ import * as shellEnv from "@eserstack/shell/env";
17
+ import * as completionsSetup from "./completions-setup.js";
18
+ const runtime = standardsCrossRuntime.runtime;
19
+ // A function of the app, not a constant. The same command installs eser,
20
+ // noskills or laroux; only the npm package differs.
21
+ const installConfig = (app) => ({
22
+ cmd: "npm",
23
+ args: ["install", "-g", "-f", app.npmPackage ?? app.command],
24
+ });
25
+ /**
26
+ * Determines the best install directory for a compiled binary.
27
+ * Prefers ~/.local/bin if it exists; falls back to /usr/local/bin.
28
+ */
29
+ const getInstallDir = async () => {
30
+ const home = standardsCrossRuntime.getHomedir();
31
+ const localBin = runtime.path.join(home, ".local", "bin");
32
+ try {
33
+ await runtime.fs.stat(localBin);
34
+ return localBin;
35
+ }
36
+ catch {
37
+ // Fall through to /usr/local/bin
38
+ }
39
+ return "/usr/local/bin";
40
+ };
41
+ /**
42
+ * Whether a file operation was refused for lack of permission: the runtime's
43
+ * PermissionDeniedError, or a raw EACCES or EPERM from node:fs.
44
+ */
45
+ export const isPermissionDenied = (error) => {
46
+ if (error instanceof standardsCrossRuntime.PermissionDeniedError) {
47
+ return true;
48
+ }
49
+ if (error instanceof Error && "code" in error) {
50
+ const code = error.code;
51
+ return code === "EACCES" || code === "EPERM";
52
+ }
53
+ return false;
54
+ };
55
+ /**
56
+ * Installs a compiled binary by copying itself to the install directory.
57
+ */
58
+ const installCompiledBinary = async (app) => {
59
+ const currentPath = runtime.process.execPath();
60
+ const installDir = await getInstallDir();
61
+ const targetPath = runtime.path.join(installDir, app.command);
62
+ const out = streams.output({
63
+ renderer: streams.renderers.ansi(),
64
+ sink: streams.sinks.stdout(),
65
+ });
66
+ out.writeln(span.text("Install method: "), span.cyan("compiled binary"));
67
+ out.writeln(span.dim(`Copying to ${targetPath}...`));
68
+ try {
69
+ await runtime.fs.copyFile(currentPath, targetPath);
70
+ await runtime.fs.chmod(targetPath, 0o755);
71
+ }
72
+ catch (error) {
73
+ if (isPermissionDenied(error)) {
74
+ out.writeln(span.red(`\nPermission denied writing to ${installDir}.\nTry: sudo ${app.command} install`));
75
+ await out.close();
76
+ return results.fail({ exitCode: 1 });
77
+ }
78
+ await out.close();
79
+ throw error;
80
+ }
81
+ out.writeln(span.green("\nInstallation complete!"));
82
+ out.writeln(span.text("You can now use "), span.cyan(app.command), span.text(" from anywhere in your terminal."));
83
+ // Setup shell completions
84
+ const shell = completionsSetup.detectShell();
85
+ const alreadyHasCompletions = await completionsSetup.hasCompletions(shell, app.command);
86
+ if (!alreadyHasCompletions) {
87
+ out.writeln(span.text("\nSetting up "), span.cyan(shell), span.text(" completions..."));
88
+ await completionsSetup.addCompletions(shell, app.command);
89
+ const shellConfig = shellEnv.getShellConfig(shell);
90
+ if (shellConfig.completionType === "eval") {
91
+ out.writeln(span.dim(` Restart your shell or run 'source ${shellConfig.rcFile}' to enable completions.`));
92
+ }
93
+ }
94
+ await out.close();
95
+ return results.ok(undefined);
96
+ };
97
+ export const installHandler = async (_ctx, app) => {
98
+ const execContext = await standardsCrossRuntime.detectExecutionContext(appOpts.appOpts(app));
99
+ // Compiled binary: copy self to install directory
100
+ if (execContext.invoker === "binary") {
101
+ return await installCompiledBinary(app);
102
+ }
103
+ const out = streams.output({
104
+ renderer: streams.renderers.ansi(),
105
+ sink: streams.sinks.stdout(),
106
+ });
107
+ // Node install: use npm
108
+ out.writeln(span.text("Detected runtime: "), span.cyan(execContext.runtime));
109
+ const { cmd, args } = installConfig(app);
110
+ out.writeln(span.dim(`Running: ${cmd} ${args.join(" ")}`));
111
+ out.writeln();
112
+ const result = await shellExec.exec `${cmd} ${args}`
113
+ .stdout("inherit")
114
+ .stderr("inherit")
115
+ .noThrow()
116
+ .spawn();
117
+ if (!result.success) {
118
+ out.writeln(span.red("\nInstallation failed."));
119
+ await out.close();
120
+ return results.fail({ exitCode: result.code });
121
+ }
122
+ out.writeln(span.green("\nInstallation complete!"));
123
+ out.writeln(span.text("You can now use "), span.cyan(app.command), span.text(" from anywhere in your terminal."));
124
+ // Setup shell completions
125
+ const shell = completionsSetup.detectShell();
126
+ const alreadyHasCompletions = await completionsSetup.hasCompletions(shell, app.command);
127
+ if (!alreadyHasCompletions) {
128
+ out.writeln(span.text("\nSetting up "), span.cyan(shell), span.text(" completions..."));
129
+ await completionsSetup.addCompletions(shell, app.command);
130
+ const shellConfig = shellEnv.getShellConfig(shell);
131
+ if (shellConfig.completionType === "eval") {
132
+ out.writeln(span.dim(` Restart your shell or run 'source ${shellConfig.rcFile}' to enable completions.`));
133
+ }
134
+ }
135
+ await out.close();
136
+ return results.ok(undefined);
137
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Command handlers
3
+ *
4
+ * @module
5
+ */
6
+ export { installHandler } from "./install.ts";
7
+ export { uninstallHandler } from "./uninstall.ts";
8
+ export { updateHandler } from "./update.ts";
9
+ export { completionsHandler } from "./completions.ts";
10
+ export { versionHandler } from "./version.ts";
11
+ export { doctorHandler } from "./doctor.ts";
12
+ export { infoHandler } from "./info.ts";
@@ -0,0 +1,13 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * Command handlers
4
+ *
5
+ * @module
6
+ */
7
+ export { installHandler } from "./install.js";
8
+ export { uninstallHandler } from "./uninstall.js";
9
+ export { updateHandler } from "./update.js";
10
+ export { completionsHandler } from "./completions.js";
11
+ export { versionHandler } from "./version.js";
12
+ export { doctorHandler } from "./doctor.js";
13
+ export { infoHandler } from "./info.js";
@@ -0,0 +1,3 @@
1
+ import type { CliApp } from "../app.ts";
2
+ import type * as shellArgs from "@eserstack/shell/args";
3
+ export declare const uninstallHandler: (_ctx: shellArgs.CommandContext, app: CliApp) => Promise<shellArgs.CliResult<void>>;
@@ -0,0 +1,60 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * Uninstall command handler - uninstalls eser CLI globally
4
+ *
5
+ * @module
6
+ */
7
+ import * as span from "@eserstack/streams/span";
8
+ import * as streams from "@eserstack/streams";
9
+ import * as standardsCrossRuntime from "@eserstack/standards/cross-runtime";
10
+ import * as results from "@eserstack/primitives/results";
11
+ import * as shellExec from "@eserstack/shell/exec";
12
+ import * as completionsSetup from "./completions-setup.js";
13
+ // A function of the app, not a constant: the same command uninstalls eser,
14
+ // noskills or laroux, and only the package name differs.
15
+ const uninstallConfigs = (app) => ({
16
+ node: {
17
+ cmd: "npm",
18
+ args: ["uninstall", "-g", app.npmPackage ?? app.command],
19
+ },
20
+ });
21
+ export const uninstallHandler = async (_ctx, app) => {
22
+ const runtimeName = standardsCrossRuntime.detectRuntime();
23
+ const out = streams.output({
24
+ renderer: streams.renderers.ansi(),
25
+ sink: streams.sinks.stdout(),
26
+ });
27
+ out.writeln(span.text("Detected runtime: "), span.cyan(runtimeName));
28
+ const config = uninstallConfigs(app)[runtimeName];
29
+ if (config === undefined) {
30
+ const renderer = streams.renderers.ansi();
31
+ await out.close();
32
+ return results.fail({
33
+ message: `${renderer.render([
34
+ span.red(`\nUnsupported runtime: ${runtimeName}`),
35
+ ])}\n` + "Global uninstallation is only supported for Node.js.",
36
+ exitCode: 1,
37
+ });
38
+ }
39
+ const { cmd, args } = config;
40
+ // Remove shell completions first
41
+ const shell = completionsSetup.detectShell();
42
+ out.writeln(span.text("\nRemoving "), span.cyan(shell), span.text(" completions..."));
43
+ await completionsSetup.removeCompletions(shell, app.command);
44
+ out.writeln(span.dim(`\nRunning: ${cmd} ${args.join(" ")}`));
45
+ out.writeln();
46
+ const result = await shellExec.exec `${cmd} ${args}`
47
+ .stdout("inherit")
48
+ .stderr("inherit")
49
+ .noThrow()
50
+ .spawn();
51
+ if (!result.success) {
52
+ out.writeln(span.red("\nUninstallation failed."));
53
+ await out.close();
54
+ return results.fail({ exitCode: result.code });
55
+ }
56
+ out.writeln(span.green("\nUninstallation complete!"));
57
+ out.writeln(span.text("The "), span.cyan(app.command), span.text(" command has been removed from your system."));
58
+ await out.close();
59
+ return results.ok(undefined);
60
+ };
@@ -0,0 +1,76 @@
1
+ import type { CliApp } from "../app.ts";
2
+ import * as results from "@eserstack/primitives/results";
3
+ import type * as shellArgs from "@eserstack/shell/args";
4
+ /** A program and its arguments for a package manager update. */
5
+ export type UpdateConfig = {
6
+ readonly cmd: string;
7
+ readonly args: readonly string[];
8
+ };
9
+ /**
10
+ * The command that updates a Node install. A function of the app: the same
11
+ * command updates eser, noskills or laroux; only the npm package differs.
12
+ */
13
+ export declare const updateConfig: (app: CliApp) => UpdateConfig;
14
+ /**
15
+ * The checksum SHA256SUMS.txt records for `archiveName`, if any.
16
+ *
17
+ * Matches the second field exactly, the way install.sh's
18
+ * `awk -v want="${ARCHIVE}" '$2 == want { print $1 }'` does. A substring test
19
+ * would happily accept the line for a DIFFERENT artifact whose name merely
20
+ * contains this one -- `<archive>.sig`, `<archive>.sha256` -- and verify the
21
+ * download against a checksum that was never about it.
22
+ */
23
+ export declare const findExpectedChecksum: (sumsText: string, archiveName: string) => string | undefined;
24
+ /**
25
+ * Verifies a downloaded release archive against the release's SHA256SUMS.txt.
26
+ *
27
+ * Fails CLOSED on every path, matching install.sh: the list being unreachable
28
+ * and the list not naming this archive are refusals, not permission to skip
29
+ * verification. Anyone able to suppress or truncate SHA256SUMS.txt -- a proxy,
30
+ * a captive portal, a partial release upload -- would otherwise get an
31
+ * unverified binary written over the running one.
32
+ */
33
+ export declare const verifyArchiveChecksum: (baseUrl: string, archiveName: string, archiveData: Uint8Array, fetchFn?: typeof fetch) => Promise<results.Result<void, string>>;
34
+ /**
35
+ * The message for a release that has no archive for this binary: the update
36
+ * stops there, and the user gets the two ways to install the current version
37
+ * instead of a download or checksum error. A release can drop an archive when
38
+ * a tool stops shipping as a standalone binary, as laroux does after 4.5.x.
39
+ */
40
+ export declare const missingArchiveMessage: (app: Pick<CliApp, "command" | "npmPackage">, tag: string, archiveName: string, isWindows: boolean) => string;
41
+ /** A program and its arguments, run without a shell. */
42
+ export type ExtractCommand = {
43
+ readonly cmd: string;
44
+ readonly args: readonly string[];
45
+ };
46
+ /** Where a release archive is unpacked: Windows (zip) or Unix (tar.gz). */
47
+ export type ExtractTarget = {
48
+ readonly platform: "windows";
49
+ /** The `SystemRoot` environment variable, e.g. `C:\Windows` */
50
+ readonly systemRoot: string | undefined;
51
+ } | {
52
+ readonly platform: "unix";
53
+ };
54
+ /**
55
+ * Builds the command that unpacks a downloaded release archive into
56
+ * `destDir`. Both paths stay separate arguments, so no shell or PowerShell
57
+ * parser reads them: a Windows path keeps its backslashes, and a quote in a
58
+ * path cannot end a string and start a command.
59
+ *
60
+ * Windows uses the bsdtar that ships in System32 (Windows 10 1803 and later),
61
+ * which reads zip archives. The absolute path matters: a GNU tar from Git for
62
+ * Windows earlier on PATH cannot read zip and takes `C:` for a remote host.
63
+ */
64
+ export declare const extractArchiveCommand: (archivePath: string, destDir: string, target: ExtractTarget) => ExtractCommand;
65
+ /**
66
+ * Self-updates a compiled binary from GitHub Releases.
67
+ *
68
+ * Exported for the legacy-update preflight
69
+ * (cmd/legacy-forwarder/legacy_update_test.go), which runs this flow for a
70
+ * v4.5.2 laroux and noskills against a local release.
71
+ */
72
+ export declare const updateCompiledBinary: (app: CliApp, currentVersion: string) => Promise<shellArgs.CliResult<void>>;
73
+ /**
74
+ * Updates eser CLI using the appropriate method for the current install type.
75
+ */
76
+ export declare const updateHandler: (_ctx: shellArgs.CommandContext, app: CliApp) => Promise<shellArgs.CliResult<void>>;