@heroku-cli/command
Version:
base class for Heroku CLI commands
161 lines (160 loc) • 5.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.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;
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const path = tslib_1.__importStar(require("node:path"));
const deps_1 = tslib_1.__importDefault(require("./deps"));
const git_1 = require("./git");
exports.oneDay = 60 * 60 * 24;
const herokuGet = async (resource, ctx) => {
const heroku = new deps_1.default.APIClient(ctx.config);
let { body: resources } = await heroku.get(`/${resource}`);
if (typeof resources === 'string')
resources = JSON.parse(resources);
return resources.map((a) => a.name).sort();
};
exports.herokuGet = herokuGet;
exports.AppCompletion = {
cacheDuration: exports.oneDay,
async options(ctx) {
const apps = await (0, exports.herokuGet)('apps', ctx);
return apps;
},
};
exports.AppAddonCompletion = {
cacheDuration: exports.oneDay,
async cacheKey(ctx) {
return ctx.flags && ctx.flags.app ? `${ctx.flags.app}_addons` : '';
},
async options(ctx) {
const addons = ctx.flags && ctx.flags.app ? await (0, exports.herokuGet)(`apps/${ctx.flags.app}/addons`, ctx) : [];
return addons;
},
};
exports.AppDynoCompletion = {
cacheDuration: exports.oneDay,
async cacheKey(ctx) {
return ctx.flags && ctx.flags.app ? `${ctx.flags.app}_dynos` : '';
},
async options(ctx) {
const dynos = ctx.flags && ctx.flags.app ? await (0, exports.herokuGet)(`apps/${ctx.flags.app}/dynos`, ctx) : [];
return dynos;
},
};
exports.BuildpackCompletion = {
async options() {
return [
'heroku/ruby',
'heroku/nodejs',
'heroku/clojure',
'heroku/python',
'heroku/java',
'heroku/gradle',
'heroku/scala',
'heroku/php',
'heroku/go',
];
},
skipCache: true,
};
exports.DynoSizeCompletion = {
cacheDuration: exports.oneDay * 90,
async options(ctx) {
const sizes = await (0, exports.herokuGet)('dyno-sizes', ctx);
return sizes;
},
};
exports.FileCompletion = {
async options() {
const files = await deps_1.default.file.readdir(process.cwd());
return files;
},
skipCache: true,
};
exports.PipelineCompletion = {
cacheDuration: exports.oneDay,
async options(ctx) {
const pipelines = await (0, exports.herokuGet)('pipelines', ctx);
return pipelines;
},
};
exports.ProcessTypeCompletion = {
async options() {
let types = [];
const procfile = path.join(process.cwd(), 'Procfile');
try {
const buff = await deps_1.default.file.readFile(procfile);
types = buff
.toString()
.split('\n')
.map((s) => {
if (!s)
return false;
const m = s.match(/^([\w-]+)/);
return m ? m[0] : false;
})
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
.filter((t) => t);
}
catch (error) {
if (error instanceof core_1.Errors.CLIError && error.code !== 'ENOENT')
throw error;
}
return types;
},
skipCache: true,
};
exports.RegionCompletion = {
cacheDuration: exports.oneDay * 7,
async options(ctx) {
const regions = await (0, exports.herokuGet)('regions', ctx);
return regions;
},
};
exports.RemoteCompletion = {
async options() {
const remotes = (0, git_1.getGitRemotes)((0, git_1.configRemote)());
return remotes.map(r => r.remote);
},
skipCache: true,
};
exports.RoleCompletion = {
async options() {
return ['admin', 'collaborator', 'member', 'owner'];
},
skipCache: true,
};
exports.ScopeCompletion = {
async options() {
return ['global', 'identity', 'read', 'write', 'read-protected', 'write-protected'];
},
skipCache: true,
};
exports.SpaceCompletion = {
cacheDuration: exports.oneDay,
async options(ctx) {
const spaces = await (0, exports.herokuGet)('spaces', ctx);
return spaces;
},
};
exports.StackCompletion = {
cacheDuration: exports.oneDay,
async options(ctx) {
const stacks = await (0, exports.herokuGet)('stacks', ctx);
return stacks;
},
};
exports.StageCompletion = {
async options() {
return ['test', 'review', 'development', 'staging', 'production'];
},
skipCache: true,
};
exports.TeamCompletion = {
cacheDuration: exports.oneDay,
async options(ctx) {
const teams = await (0, exports.herokuGet)('teams', ctx);
return teams;
},
};