UNPKG

5.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TeamCompletion = exports.StageCompletion = exports.StackCompletion = exports.SpaceCompletion = exports.ScopeCompletion = exports.RoleCompletion = exports.RemoteCompletion = exports.RegionCompletion = exports.ProcessTypeCompletion = exports.PipelineCompletion = exports.FileCompletion = exports.DynoSizeCompletion = exports.BuildpackCompletion = exports.AppDynoCompletion = exports.AppAddonCompletion = exports.AppCompletion = exports.herokuGet = exports.oneDay = void 0;
4const tslib_1 = require("tslib");
5const errors_1 = require("@oclif/core/lib/errors");
6const path = tslib_1.__importStar(require("path"));
7const deps_1 = tslib_1.__importDefault(require("./deps"));
8const git_1 = require("./git");
9exports.oneDay = 60 * 60 * 24;
10const herokuGet = async (resource, ctx) => {
11 const heroku = new deps_1.default.APIClient(ctx.config);
12 let { body: resources } = await heroku.get(`/${resource}`);
13 if (typeof resources === 'string')
14 resources = JSON.parse(resources);
15 return resources.map((a) => a.name).sort();
16};
17exports.herokuGet = herokuGet;
18exports.AppCompletion = {
19 cacheDuration: exports.oneDay,
20 options: async (ctx) => {
21 const apps = await (0, exports.herokuGet)('apps', ctx);
22 return apps;
23 },
24};
25exports.AppAddonCompletion = {
26 cacheDuration: exports.oneDay,
27 cacheKey: async (ctx) => {
28 return ctx.flags && ctx.flags.app ? `${ctx.flags.app}_addons` : '';
29 },
30 options: async (ctx) => {
31 const addons = ctx.flags && ctx.flags.app ? await (0, exports.herokuGet)(`apps/${ctx.flags.app}/addons`, ctx) : [];
32 return addons;
33 },
34};
35exports.AppDynoCompletion = {
36 cacheDuration: exports.oneDay,
37 cacheKey: async (ctx) => {
38 return ctx.flags && ctx.flags.app ? `${ctx.flags.app}_dynos` : '';
39 },
40 options: async (ctx) => {
41 const dynos = ctx.flags && ctx.flags.app ? await (0, exports.herokuGet)(`apps/${ctx.flags.app}/dynos`, ctx) : [];
42 return dynos;
43 },
44};
45exports.BuildpackCompletion = {
46 skipCache: true,
47 options: async () => {
48 return [
49 'heroku/ruby',
50 'heroku/nodejs',
51 'heroku/clojure',
52 'heroku/python',
53 'heroku/java',
54 'heroku/gradle',
55 'heroku/scala',
56 'heroku/php',
57 'heroku/go',
58 ];
59 },
60};
61exports.DynoSizeCompletion = {
62 cacheDuration: exports.oneDay * 90,
63 options: async (ctx) => {
64 const sizes = await (0, exports.herokuGet)('dyno-sizes', ctx);
65 return sizes;
66 },
67};
68exports.FileCompletion = {
69 skipCache: true,
70 options: async () => {
71 const files = await deps_1.default.file.readdir(process.cwd());
72 return files;
73 },
74};
75exports.PipelineCompletion = {
76 cacheDuration: exports.oneDay,
77 options: async (ctx) => {
78 const pipelines = await (0, exports.herokuGet)('pipelines', ctx);
79 return pipelines;
80 },
81};
82exports.ProcessTypeCompletion = {
83 skipCache: true,
84 options: async () => {
85 let types = [];
86 const procfile = path.join(process.cwd(), 'Procfile');
87 try {
88 const buff = await deps_1.default.file.readFile(procfile);
89 types = buff
90 .toString()
91 .split('\n')
92 .map((s) => {
93 if (!s)
94 return false;
95 const m = s.match(/^([\w-]+)/);
96 return m ? m[0] : false;
97 })
98 .filter((t) => t);
99 }
100 catch (error) {
101 if (error instanceof errors_1.CLIError && error.code !== 'ENOENT')
102 throw error;
103 }
104 return types;
105 },
106};
107exports.RegionCompletion = {
108 cacheDuration: exports.oneDay * 7,
109 options: async (ctx) => {
110 const regions = await (0, exports.herokuGet)('regions', ctx);
111 return regions;
112 },
113};
114exports.RemoteCompletion = {
115 skipCache: true,
116 options: async () => {
117 const remotes = (0, git_1.getGitRemotes)((0, git_1.configRemote)());
118 return remotes.map(r => r.remote);
119 },
120};
121exports.RoleCompletion = {
122 skipCache: true,
123 options: async () => {
124 return ['admin', 'collaborator', 'member', 'owner'];
125 },
126};
127exports.ScopeCompletion = {
128 skipCache: true,
129 options: async () => {
130 return ['global', 'identity', 'read', 'write', 'read-protected', 'write-protected'];
131 },
132};
133exports.SpaceCompletion = {
134 cacheDuration: exports.oneDay,
135 options: async (ctx) => {
136 const spaces = await (0, exports.herokuGet)('spaces', ctx);
137 return spaces;
138 },
139};
140exports.StackCompletion = {
141 cacheDuration: exports.oneDay,
142 options: async (ctx) => {
143 const stacks = await (0, exports.herokuGet)('stacks', ctx);
144 return stacks;
145 },
146};
147exports.StageCompletion = {
148 skipCache: true,
149 options: async () => {
150 return ['test', 'review', 'development', 'staging', 'production'];
151 },
152};
153exports.TeamCompletion = {
154 cacheDuration: exports.oneDay,
155 options: async (ctx) => {
156 const teams = await (0, exports.herokuGet)('teams', ctx);
157 return teams;
158 },
159};