File size: 8,126 Bytes
2ad2741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { spawnSync } from "node:child_process";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { t } from "../i18n.mjs";
import { emit } from "../output.mjs";
import { discoverPlugins } from "../plugins.mjs";

// Run npm with an explicit argument array and no shell. Passing args this way
// (instead of string-interpolating into `execSync`) prevents a malicious plugin
// name like `foo; rm -rf ~` or `` foo`id` `` from being interpreted by the shell.
function runNpm(args) {
  const res = spawnSync("npm", args, { stdio: "inherit", shell: false });
  if (res.error) throw res.error;
  if (typeof res.status === "number" && res.status !== 0) {
    throw new Error(`npm exited with code ${res.status}`);
  }
}

const TEMPLATE_INDEX = `export const meta = {

  name: "PLUGIN_NAME",

  version: "0.1.0",

  description: "OmniRoute plugin",

  omnirouteApi: ">=4.0.0",

};



export function register(program, ctx) {

  program

    .command("PLUGIN_NAME")

    .description(meta.description)

    .action(async (opts, cmd) => {

      const gOpts = cmd.optsWithGlobals();

      // ctx provides: ctx.apiFetch, ctx.emit, ctx.t, ctx.withSpinner, ctx.baseUrl, ctx.apiKey

      const res = await ctx.apiFetch("/api/health", { baseUrl: gOpts.baseUrl, apiKey: gOpts.apiKey });

      const data = await res.json();

      ctx.emit(data, gOpts);

    });

}

`;

export function registerPlugin(program) {
  const plugin = program
    .command("plugin")
    .description(t("plugin.description") || "Manage CLI plugins (omniroute-cmd-*)");

  plugin
    .command("list")
    .description(t("plugin.list") || "List installed plugins")
    .action(async (opts, cmd) => {
      const plugins = await discoverPlugins();
      emit(
        plugins.map((p) => ({ name: p.name, version: p.version, description: p.description })),
        cmd.optsWithGlobals()
      );
      if (plugins.length === 0) {
        process.stdout.write("No plugins installed.\n");
        process.stdout.write(`Install: omniroute plugin install <name>\n`);
      }
    });

  plugin
    .command("install <name>")
    .description(t("plugin.install") || "Install a plugin")
    .option("-y, --yes", "Skip confirmation prompt")
    .action(async (name, opts) => {
      const isLocal = name.startsWith("./") || name.startsWith("/") || name.startsWith("../");
      const pkgName = isLocal ? name : `omniroute-cmd-${name}`;

      if (!opts.yes) {
        process.stderr.write(
          `⚠ WARNING: Plugins run with the same privileges as omniroute CLI.\n` +
            `  Only install plugins from sources you trust.\n` +
            `  Installing: ${pkgName}\n` +
            `  Pass --yes to skip this prompt.\n`
        );
        // In non-interactive mode, require explicit --yes
        if (!process.stdin.isTTY) {
          process.stderr.write("Non-interactive mode: use --yes to confirm.\n");
          process.exit(1);
        }
      }

      try {
        runNpm(["install", "-g", pkgName]);
        process.stdout.write(`\nβœ“ Installed: ${pkgName}\n`);
      } catch {
        process.stderr.write(`βœ— Failed to install ${pkgName}\n`);
        process.exit(1);
      }
    });

  plugin
    .command("remove <name>")
    .alias("uninstall")
    .description(t("plugin.remove") || "Remove a plugin")
    .option("-y, --yes", "Skip confirmation")
    .action(async (name, opts) => {
      const pkgName = name.startsWith("omniroute-cmd-") ? name : `omniroute-cmd-${name}`;
      if (!opts.yes) {
        process.stderr.write(`Removing: ${pkgName} β€” pass --yes to confirm.\n`);
        if (!process.stdin.isTTY) {
          process.exit(1);
        }
      }
      try {
        runNpm(["uninstall", "-g", pkgName]);
        process.stdout.write(`βœ“ Removed: ${pkgName}\n`);
      } catch {
        process.stderr.write(`βœ— Failed to remove ${pkgName}\n`);
        process.exit(1);
      }
    });

  plugin
    .command("info <name>")
    .description(t("plugin.info") || "Show plugin details")
    .action(async (name, opts, cmd) => {
      const plugins = await discoverPlugins();
      const p = plugins.find((x) => x.name === name || x.name === `omniroute-cmd-${name}`);
      if (!p) {
        process.stderr.write(`Plugin '${name}' not found.\n`);
        process.exit(1);
      }
      emit(p.pkg, cmd.optsWithGlobals());
    });

  plugin
    .command("search [query]")
    .description(t("plugin.search") || "Search npm for available plugins")
    .action(async (query) => {
      const q = query ? `omniroute-cmd-${query}` : "omniroute-cmd";
      const url = `https://registry.npmjs.org/-/v1/search?text=${encodeURIComponent(q)}&size=50`;
      try {
        const res = await fetch(url);
        if (!res.ok) throw new Error(`npm registry returned ${res.status}`);
        const data = await res.json();
        const rows = (data.objects || []).map((o) => ({
          name: o.package.name,
          version: o.package.version,
          description: o.package.description,
        }));
        if (rows.length === 0) {
          process.stdout.write(`No plugins found for '${query || "omniroute-cmd"}'.\n`);
        } else {
          rows.forEach((r) =>
            process.stdout.write(`  ${r.name}@${r.version}  ${r.description || ""}\n`)
          );
        }
      } catch (err) {
        process.stderr.write(`Search failed: ${err.message}\n`);
        process.exit(1);
      }
    });

  plugin
    .command("update [name]")
    .description(t("plugin.update") || "Update installed plugin(s)")
    .action(async (name) => {
      try {
        if (name) {
          const pkg = `omniroute-cmd-${name}`;
          runNpm(["update", "-g", pkg]);
          process.stdout.write(`βœ“ Updated: ${pkg}\n`);
          return;
        }
        // No name β†’ update every installed plugin. Enumerate them explicitly
        // instead of relying on a shell glob (`omniroute-cmd-*`), which never
        // expands without a shell and would otherwise update nothing.
        const plugins = await discoverPlugins();
        const names = plugins
          .map((p) => p.name)
          .filter((n) => typeof n === "string" && n.startsWith("omniroute-cmd-"));
        if (names.length === 0) {
          process.stdout.write("No plugins installed to update.\n");
          return;
        }
        runNpm(["update", "-g", ...names]);
        process.stdout.write(`βœ“ Updated: ${names.join(", ")}\n`);
      } catch {
        process.stderr.write(`βœ— Update failed\n`);
        process.exit(1);
      }
    });

  plugin
    .command("scaffold <name>")
    .description(t("plugin.scaffold") || "Scaffold a new plugin boilerplate")
    .action(async (name) => {
      const safeName = name.replace(/[^a-z0-9-]/g, "-");
      const dir = join(process.cwd(), `omniroute-cmd-${safeName}`);
      if (existsSync(dir)) {
        process.stderr.write(`Directory already exists: ${dir}\n`);
        process.exit(1);
      }
      mkdirSync(dir, { recursive: true });
      writeFileSync(
        join(dir, "package.json"),
        JSON.stringify(
          {
            name: `omniroute-cmd-${safeName}`,
            version: "0.1.0",
            type: "module",
            main: "index.mjs",
            description: `OmniRoute CLI plugin: ${safeName}`,
            engines: { omniroute: ">=4.0.0" },
            keywords: ["omniroute-plugin", "omniroute-cmd"],
          },
          null,
          2
        ) + "\n"
      );
      writeFileSync(join(dir, "index.mjs"), TEMPLATE_INDEX.replace(/PLUGIN_NAME/g, safeName));
      writeFileSync(
        join(dir, "README.md"),
        `# omniroute-cmd-${safeName}\n\nAn OmniRoute CLI plugin.\n\n## Install\n\n\`\`\`bash\nomniroute plugin install ${safeName}\n\`\`\`\n`
      );
      process.stdout.write(`βœ“ Scaffolded: ${dir}\n`);
      process.stdout.write(`  Run: cd ${dir} && omniroute plugin install .\n`);
    });
}