1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PluginLegacy = void 0;
|
4 | const core_1 = require("@oclif/core");
|
5 | const path = require("path");
|
6 | const util_1 = require("util");
|
7 | const util_2 = require("./util");
|
8 | const debug = require('debug')('@oclif/plugin-legacy');
|
9 | const pjson = require('../package.json');
|
10 | function convertFlagsFromV5(Flags, flags) {
|
11 | if (!flags)
|
12 | return {};
|
13 | if (!Array.isArray(flags))
|
14 | return flags;
|
15 |
|
16 | return flags.reduce((flags, flag) => {
|
17 | const opts = {
|
18 | char: flag.char,
|
19 | completion: flag.completion,
|
20 | default: flag.default,
|
21 | description: flag.description,
|
22 | hidden: flag.hidden,
|
23 | parse: flag.parse,
|
24 | required: flag.required || flag.optional === false,
|
25 | };
|
26 | for (const [k, v] of Object.entries(opts)) {
|
27 | if (v === undefined)
|
28 | delete opts[k];
|
29 | }
|
30 | if (!opts.parse)
|
31 | delete opts.parse;
|
32 | flags[flag.name] = flag.hasValue ? Flags.string(opts) : Flags.boolean(opts);
|
33 | return flags;
|
34 | }, {});
|
35 | }
|
36 | class PluginLegacy extends core_1.Plugin {
|
37 | constructor(config, base) {
|
38 | super(base);
|
39 | this.config = config;
|
40 | this.base = base;
|
41 | this._base = `${pjson.name}@${pjson.version}`;
|
42 | debug('loading legacy plugin', base.root);
|
43 | }
|
44 | get moduleCommands() {
|
45 | if (this._moduleCommands)
|
46 | return this._moduleCommands;
|
47 | const { main } = this.pjson;
|
48 | if (!main)
|
49 | return [];
|
50 | const module = require(path.join(this.root, main));
|
51 | if (!module.commands)
|
52 | return [];
|
53 | debug('loading module commands', this.root);
|
54 | this._moduleCommands = module.commands.map((c) => this.convertCommand(c));
|
55 | return this._moduleCommands;
|
56 | }
|
57 | get moduleTopics() {
|
58 | if (this.pjson.oclif.topics)
|
59 | return [];
|
60 | if (this._moduleTopics)
|
61 | return this._moduleTopics;
|
62 | const { main } = this.pjson;
|
63 | if (!main)
|
64 | return [];
|
65 | const module = require(path.join(this.root, main));
|
66 | if (!module.commands)
|
67 | return [];
|
68 | debug('loading module topics', this.root);
|
69 | this._moduleTopics = module.topics;
|
70 | return this._moduleTopics;
|
71 | }
|
72 | get topics() {
|
73 | return super.topics.concat(this.moduleTopics);
|
74 | }
|
75 | async findCommand(id, opts = {}) {
|
76 | let cmd = await super.findCommand(id);
|
77 | if (cmd)
|
78 | return this.convertCommand(cmd);
|
79 | cmd = this.moduleCommands.find((c) => c.id === id);
|
80 | if (cmd) {
|
81 | cmd.plugin = this;
|
82 | return this.convertCommand(cmd);
|
83 | }
|
84 | if (opts.must)
|
85 | throw new Error(`command ${id} not found`);
|
86 | }
|
87 | async getCommandIds() {
|
88 | return [
|
89 |
|
90 | ...(await super.getCommandIds()),
|
91 | ...this.moduleCommands.map((c) => c.id),
|
92 | ];
|
93 | }
|
94 | convertCommand(c) {
|
95 | if (this.isICommand(c))
|
96 | return this.convertFromICommand(c);
|
97 | if (this.isV5Command(c))
|
98 | return this.convertFromV5(c);
|
99 | if (this.isFlowCommand(c))
|
100 | return this.convertFromFlow(c);
|
101 | debug(c);
|
102 | throw new Error(`Invalid command: ${(0, util_1.inspect)(c)}`);
|
103 | }
|
104 | convertFromFlow(c) {
|
105 | if (!c.id)
|
106 | c.id = (0, util_2.compact)([c.topic, c.command]).join(':');
|
107 | c._version = c._version || '0.0.0';
|
108 | return c;
|
109 | }
|
110 | convertFromICommand(c) {
|
111 | if (!c.id)
|
112 | c.id = (0, util_2.compact)([c.topic, c.command]).join(':');
|
113 | return c;
|
114 | }
|
115 | convertFromV5(c) {
|
116 | const { Command, flags: Flags, vars } = require('@heroku-cli/command');
|
117 | class V5 extends Command {
|
118 | async run() {
|
119 | const color = require('@oclif/color').default;
|
120 | const { args, argv, flags } = this.parse(this.constructor);
|
121 | const ctx = {
|
122 | apiHost: vars.apiHost,
|
123 | apiToken: this.heroku.auth,
|
124 | apiUrl: vars.apiUrl,
|
125 | app: flags.app,
|
126 | args: c.variableArgs ? argv : args,
|
127 | auth: {},
|
128 | config: this.config,
|
129 | cwd: process.cwd(),
|
130 | debug: Boolean(this.config.debug),
|
131 | debugHeaders: this.config.debug > 1 || ['1', 'true'].includes(process.env.HEROKU_DEBUG_HEADERS),
|
132 | flags,
|
133 | gitHost: vars.gitHost,
|
134 | herokuDir: this.config.cacheDir,
|
135 | httpGitHost: vars.httpGitHost,
|
136 | org: flags.org,
|
137 | supportsColor: Boolean(color.supports.stdout),
|
138 | team: flags.team,
|
139 | version: this.config.userAgent,
|
140 | };
|
141 | ctx.auth.password = ctx.apiToken;
|
142 | const ansi = require('ansi-escapes');
|
143 | process.once('exit', () => {
|
144 | if (process.stderr.isTTY) {
|
145 | process.stderr.write(ansi.cursorShow);
|
146 | }
|
147 | });
|
148 | return c.run(ctx);
|
149 | }
|
150 | }
|
151 | V5.aliases = c.aliases || [];
|
152 |
|
153 | V5.args = (c.args || []).map((a) => (Object.assign(Object.assign({}, a), { required: a.required !== false && !a.optional })));
|
154 | V5.description = [c.description, c.help].join('\n');
|
155 | V5.examples = c.examples || c.example;
|
156 | V5.flags = convertFlagsFromV5(Flags, c.flags);
|
157 | V5.help = c.help;
|
158 | V5.hidden = Boolean(c.hidden);
|
159 | V5.id = (0, util_2.compact)([c.topic, c.command]).join(':');
|
160 | V5.strict = c.strict || !c.variableArgs;
|
161 | V5.usage = c.usage;
|
162 | if (c.needsApp || c.wantsApp) {
|
163 | V5.flags.app = Flags.app({ required: Boolean(c.needsApp) });
|
164 | V5.flags.remote = Flags.remote();
|
165 | }
|
166 | if (c.needsOrg || c.wantsOrg) {
|
167 | const opts = { description: 'team to use', hidden: false, required: Boolean(c.needsOrg) };
|
168 | V5.flags.team = Flags.team(opts);
|
169 | V5.flags.org = Flags.team({ char: 'o', hidden: true });
|
170 | }
|
171 | return V5;
|
172 | }
|
173 | isFlowCommand(command) {
|
174 | const c = command;
|
175 | return typeof c === 'function';
|
176 |
|
177 | }
|
178 | isICommand(c) {
|
179 | const semver = require('semver');
|
180 | if (!c._version)
|
181 | return false;
|
182 | return semver.gte(c._version, '11.0.0');
|
183 | }
|
184 | isV5Command(command) {
|
185 | const c = command;
|
186 | return Boolean(typeof c === 'object');
|
187 | }
|
188 | }
|
189 | exports.PluginLegacy = PluginLegacy;
|