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