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