UNPKG

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