1 | 'use strict';
|
2 |
|
3 | var _herokuCliUtil = require('heroku-cli-util');
|
4 |
|
5 | var _herokuCliUtil2 = _interopRequireDefault(_herokuCliUtil);
|
6 |
|
7 | var _push = require('./push');
|
8 |
|
9 | var _push2 = _interopRequireDefault(_push);
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12 |
|
13 | function BuildpackCommand(context, heroku, command, action) {
|
14 | this.app = context.app;
|
15 | this.heroku = heroku;
|
16 |
|
17 | if (context.flags && context.flags.index) {
|
18 | let index = parseInt(context.flags.index);
|
19 | if (isNaN(index) || index <= 0) {
|
20 | _herokuCliUtil2.default.exit(1, 'Invalid index. Must be greater than 0.');
|
21 | }
|
22 | this.index = index;
|
23 | } else {
|
24 | this.index = null;
|
25 | }
|
26 |
|
27 | this.url = context.args && context.args.url;
|
28 | this.action = action;
|
29 | this.command = command;
|
30 | this.context = context;
|
31 | }
|
32 |
|
33 | BuildpackCommand.prototype.mapBuildpackResponse = function (buildpacks) {
|
34 | return buildpacks.map(function (bp) {
|
35 | bp.buildpack.url = bp.buildpack.url.replace(/^urn:buildpack:/, '');
|
36 | return bp;
|
37 | });
|
38 | };
|
39 |
|
40 | BuildpackCommand.prototype.get = function () {
|
41 | let buildpackCommand = this;
|
42 | return this.heroku.request({
|
43 | path: `/apps/${this.app}/buildpack-installations`,
|
44 | headers: { Range: '' }
|
45 | }).then(function (buildpacks) {
|
46 | return buildpackCommand.mapBuildpackResponse(buildpacks);
|
47 | });
|
48 | };
|
49 |
|
50 | BuildpackCommand.prototype.put = function (buildpackUpdates) {
|
51 | let app = this.app;
|
52 | let buildpackCommand = this;
|
53 | return this.heroku.request({
|
54 | path: `/apps/${app}/buildpack-installations`,
|
55 | headers: { Range: '' },
|
56 | method: 'PUT',
|
57 | body: { updates: buildpackUpdates }
|
58 | }).then(function (buildpacks) {
|
59 | return buildpackCommand.mapBuildpackResponse(buildpacks);
|
60 | });
|
61 | };
|
62 |
|
63 | BuildpackCommand.prototype.clear = function* () {
|
64 | let res = yield {
|
65 | config: this.heroku.request({
|
66 | path: `/apps/${this.app}/config-vars`
|
67 | }),
|
68 | clear: this.put([])
|
69 | };
|
70 |
|
71 | let configVars = res.config;
|
72 | let message = `Buildpack${this.command === 'clear' ? 's' : ''} ${this.action}.`;
|
73 | if (configVars.hasOwnProperty('BUILDPACK_URL')) {
|
74 | _herokuCliUtil2.default.log(message);
|
75 | _herokuCliUtil2.default.warn('The BUILDPACK_URL config var is still set and will be used for the next release');
|
76 | } else if (configVars.hasOwnProperty('LANGUAGE_PACK_URL')) {
|
77 | _herokuCliUtil2.default.log(message);
|
78 | _herokuCliUtil2.default.warn('The LANGUAGE_PACK_URL config var is still set and will be used for the next release');
|
79 | } else {
|
80 | _herokuCliUtil2.default.log(`${message} Next release on ${this.app} will detect buildpack normally.`);
|
81 | }
|
82 | };
|
83 |
|
84 | BuildpackCommand.prototype.findIndex = function (buildpacks) {
|
85 | const findIndex = require('lodash.findindex');
|
86 | let index = this.index;
|
87 | if (index) {
|
88 | return findIndex(buildpacks, function (b) {
|
89 | return b.ordinal + 1 === index;
|
90 | });
|
91 | } else {
|
92 | return -1;
|
93 | }
|
94 | };
|
95 |
|
96 | BuildpackCommand.prototype.findUrl = function findUrl(buildpacks) {
|
97 | const findIndex = require('lodash.findindex');
|
98 | let url = this.url;
|
99 | let mappedUrl = this.url.replace(/^urn:buildpack:/, '').replace(/^https:\/\/codon-buildpacks\.s3\.amazonaws\.com\/buildpacks\/heroku\/(.*)\.tgz$/, 'heroku/$1');
|
100 | return findIndex(buildpacks, function (b) {
|
101 | return b.buildpack.url === url || b.buildpack.url === mappedUrl;
|
102 | });
|
103 | };
|
104 |
|
105 | BuildpackCommand.prototype.display = function (buildpacks, indent) {
|
106 | if (buildpacks.length === 1) {
|
107 | _herokuCliUtil2.default.log(buildpacks[0].buildpack.url);
|
108 | } else {
|
109 | buildpacks.forEach(function (b, i) {
|
110 | _herokuCliUtil2.default.log(`${indent}${i + 1}. ${b.buildpack.url}`);
|
111 | });
|
112 | }
|
113 | };
|
114 |
|
115 | BuildpackCommand.prototype.displayUpdate = function (buildpacks) {
|
116 | if (buildpacks.length === 1) {
|
117 | _herokuCliUtil2.default.log(`Buildpack ${this.action}. Next release on ${this.app} will use ${buildpacks[0].buildpack.url}.`);
|
118 | _herokuCliUtil2.default.log(`Run ${_herokuCliUtil2.default.color.magenta((0, _push2.default)(this.context.flags.remote))} to create a new release using this buildpack.`);
|
119 | } else {
|
120 | _herokuCliUtil2.default.log(`Buildpack ${this.action}. Next release on ${this.app} will use:`);
|
121 | this.display(buildpacks, ' ');
|
122 | _herokuCliUtil2.default.log(`Run ${_herokuCliUtil2.default.color.magenta((0, _push2.default)(this.context.flags.remote))} to create a new release using these buildpacks.`);
|
123 | }
|
124 | };
|
125 |
|
126 | BuildpackCommand.prototype.mutate = function (buildpacksGet, spliceIndex) {
|
127 | let buildpackUpdates = buildpacksGet.map(function (b) {
|
128 | return { buildpack: b.buildpack.url };
|
129 | });
|
130 |
|
131 | let howmany = this.command === 'add' ? 0 : 1;
|
132 | let urls = this.command === 'remove' ? [] : [{ buildpack: this.url }];
|
133 |
|
134 | Array.prototype.splice.apply(buildpackUpdates, [spliceIndex, howmany].concat(urls));
|
135 |
|
136 | let bp = this;
|
137 | return this.put(buildpackUpdates).then(function (buildpackPut) {
|
138 | bp.displayUpdate(buildpackPut);
|
139 | });
|
140 | };
|
141 |
|
142 | BuildpackCommand.prototype.validateUrlNotSet = function (buildpacks) {
|
143 | if (this.findUrl(buildpacks, this.url) !== -1) {
|
144 | _herokuCliUtil2.default.exit(1, `The buildpack ${this.url} is already set on your app.`);
|
145 | }
|
146 | };
|
147 |
|
148 | BuildpackCommand.prototype.validateUrlPassed = function () {
|
149 | if (this.url) {
|
150 | return this.url;
|
151 | }
|
152 | _herokuCliUtil2.default.exit(1, `Usage: heroku buildpacks:${this.command} BUILDPACK_URL.
|
153 | Must specify target buildpack URL.`);
|
154 | };
|
155 |
|
156 | BuildpackCommand.prototype.validateIndexInRange = function (buildpacks) {
|
157 | if (this.index < 0 || this.index > buildpacks.length) {
|
158 | if (buildpacks.length === 1) {
|
159 | _herokuCliUtil2.default.exit(1, 'Invalid index. Only valid value is 1.');
|
160 | } else {
|
161 | _herokuCliUtil2.default.exit(1, `Invalid index. Please choose a value between 1 and ${buildpacks.length}`);
|
162 | }
|
163 | }
|
164 | };
|
165 |
|
166 | module.exports = BuildpackCommand; |
\ | No newline at end of file |