Skip to content

Commit

Permalink
chore(buildpacks): upgrade buildpacks to eslint (heroku#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
chadian authored and RasPhilCo committed Feb 3, 2020
1 parent 5d999ae commit bb45760
Show file tree
Hide file tree
Showing 24 changed files with 927 additions and 914 deletions.
1 change: 1 addition & 0 deletions packages/buildpacks/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
9 changes: 9 additions & 0 deletions packages/buildpacks/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": [
"oclif",
"oclif-typescript"
],
"rules": {
"unicorn/no-abusive-eslint-disable": "off"
}
}
9 changes: 6 additions & 3 deletions packages/buildpacks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@oclif/dev-cli": "^1.21.3",
"@oclif/plugin-help": "^2.1.6",
"@oclif/test": "^1.2.4",
"@oclif/tslint": "^3.1.1",
"@types/ansi-styles": "^3.2.1",
"@types/chai": "^4.1.7",
"@types/lodash": "^4.14.123",
Expand All @@ -31,14 +30,16 @@
"@types/node-fetch": "^2.1.6",
"@types/supports-color": "^5.3.0",
"chai": "^4.2.0",
"eslint": "^6.7.2",
"eslint-config-oclif": "^3.1.0",
"eslint-config-oclif-typescript": "^0.1.0",
"globby": "^9.0.0",
"mocha": "^5",
"nock": "^10.0.6",
"nyc": "^13.2.0",
"tmp": "^0.0.33",
"ts-node": "^8.0.2",
"tslib": "^1",
"tslint": "^5",
"typescript": "3.3.3333"
},
"engines": {
Expand All @@ -65,11 +66,13 @@
},
"repository": "heroku/cli",
"scripts": {
"lint": "eslint . --ext .ts --config .eslintrc",
"postpack": "rm -f oclif.manifest.json npm-shrinkwrap.json",
"posttest": "tsc -p test --noEmit && tslint -p test -t stylish",
"prepack": "rm -rf lib && tsc && oclif-dev manifest && oclif-dev readme && npm shrinkwrap",
"prepare": "rm -rf lib && tsc",
"pretest": "tsc -p test --noEmit",
"test": "nyc mocha --forbid-only \"test/**/*.test.ts\"",
"posttest": "yarn lint",
"version": "oclif-dev readme && git add README.md"
}
}
54 changes: 29 additions & 25 deletions packages/buildpacks/src/buildpacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import {cli} from 'cli-ux'
import {findIndex as lodashFindIndex} from 'lodash'
import {Result} from 'true-myth'

// eslint-disable-next-line node/no-missing-require
const push = require('./push')
const validUrl = require('valid-url')

export type BuildpackResponse = {
buildpack: {
url: string,
name: string,
},
ordinal: number
url: string;
name: string;
};
ordinal: number;
}

export class BuildpackCommand {
heroku: APIClient

registry: BuildpackRegistry

constructor(heroku: APIClient) {
Expand All @@ -26,12 +28,12 @@ export class BuildpackCommand {
}

async fetch(app: string): Promise<any[]> {
let buildpacks = await this.heroku.get(`/apps/${app}/buildpack-installations`)
const buildpacks = await this.heroku.get(`/apps/${app}/buildpack-installations`)
return this.mapBuildpackResponse(buildpacks)
}

mapBuildpackResponse(buildpacks: {body: any}): BuildpackResponse[] {
let body = buildpacks.body
const body = buildpacks.body
return body.map((bp: BuildpackResponse) => {
bp.buildpack.url = bp.buildpack.url.replace(/^urn:buildpack:/, '')
return bp
Expand All @@ -54,31 +56,32 @@ export class BuildpackCommand {
}

Result.match({
// eslint-disable-next-line @typescript-eslint/no-empty-function
Ok: _ => {},
Err: err => {
cli.error(`Could not find the buildpack: ${buildpack}. ${err}`, {exit: 1})
},
}, BuildpackRegistry.isValidBuildpackSlug(buildpack))

try {
let response = await this.registry.buildpackExists(buildpack)
let body = await response.json()
const response = await this.registry.buildpackExists(buildpack)
const body = await response.json()
return body.blob_url
} catch (err) {
if (err.statusCode === 404) {
} catch (error) {
if (error.statusCode === 404) {
cli.error(`${buildpack} is not in the buildpack registry.`, {exit: 1})
} else if (err.statusCode) {
cli.error(`${err.statusCode}: ${err.message}`, {exit: 1})
} else if (error.statusCode) {
cli.error(`${error.statusCode}: ${error.message}`, {exit: 1})
} else {
cli.error(err.message, {exit: 1})
cli.error(error.message, {exit: 1})
}
}

return ''
}

async findUrl(buildpacks: BuildpackResponse[], buildpack: string): Promise<number> {
let mappedUrl = await this.registryNameToUrl(buildpack)
const mappedUrl = await this.registryNameToUrl(buildpack)
return lodashFindIndex(buildpacks, (b: BuildpackResponse) => {
return b.buildpack.url === buildpack || b.buildpack.url === mappedUrl
})
Expand All @@ -95,30 +98,29 @@ export class BuildpackCommand {
return lodashFindIndex(buildpacks, function (b: BuildpackResponse) {
return b.ordinal + 1 === index
})
} else {
return -1
}
return -1
}

async mutate(app: string, buildpacks: BuildpackResponse[], spliceIndex: number, buildpack: string, command: 'add' | 'set' | 'remove'): Promise<BuildpackResponse[]> {
let buildpackUpdates = buildpacks.map(function (b: BuildpackResponse) {
const buildpackUpdates = buildpacks.map(function (b: BuildpackResponse) {
return {buildpack: b.buildpack.url}
})

let howmany = (command === 'add') ? 0 : 1
let urls = (command === 'remove') ? [] : [{buildpack: await this.registryNameToUrl(buildpack)}]
const howmany = (command === 'add') ? 0 : 1
const urls = (command === 'remove') ? [] : [{buildpack: await this.registryNameToUrl(buildpack)}]

let indexes: any[] = [spliceIndex, howmany]
let array: any[] = indexes.concat(urls)
const indexes: any[] = [spliceIndex, howmany]
const array: any[] = indexes.concat(urls)
Array.prototype.splice.apply(buildpackUpdates, array as any)

return this.put(app, buildpackUpdates)
}

async put(app: string, buildpackUpdates: {buildpack: string}[]): Promise<BuildpackResponse[]> {
let buildpacks = await this.heroku.put(`/apps/${app}/buildpack-installations`, {
const buildpacks = await this.heroku.put(`/apps/${app}/buildpack-installations`, {
headers: {Range: ''},
body: {updates: buildpackUpdates}
body: {updates: buildpackUpdates},
})

return this.mapBuildpackResponse(buildpacks)
Expand All @@ -136,12 +138,14 @@ export class BuildpackCommand {
}

registryUrlToName(buildpack: string, registryOnly = false): string {
// eslint-disable-next-line no-useless-escape
let match = /^https:\/\/buildpack\-registry\.s3\.amazonaws\.com\/buildpacks\/([\w\-]+\/[\w\-]+).tgz$/.exec(buildpack)
if (match) {
return match[1]
}

if (!registryOnly) {
// eslint-disable-next-line no-useless-escape
match = /^https:\/\/codon\-buildpacks\.s3\.amazonaws\.com\/buildpacks\/heroku\/([\w\-]+).tgz$/.exec(buildpack)
if (match) {
return `heroku/${match[1]}`
Expand All @@ -154,8 +158,8 @@ export class BuildpackCommand {
async clear(app: string, command: 'clear' | 'remove', action: 'cleared' | 'removed') {
await this.put(app, [])

let configVars: any = await this.heroku.get(`/apps/${app}/config-vars`)
let message = `Buildpack${command === 'clear' ? 's' : ''} ${action}.`
const configVars: any = await this.heroku.get(`/apps/${app}/config-vars`)
const message = `Buildpack${command === 'clear' ? 's' : ''} ${action}.`
if (configVars.body.BUILDPACK_URL) {
cli.log(message)
cli.warn('The BUILDPACK_URL config var is still set and will be used for the next release')
Expand Down
20 changes: 11 additions & 9 deletions packages/buildpacks/src/commands/buildpacks/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,44 @@ import {BuildpackCommand} from '../../buildpacks'

export default class Add extends Command {
static description = 'add new app buildpack, inserting into list of buildpacks if necessary'

static flags = {
app: Flags.app({required: true}),
remote: Flags.remote(),
index: Flags.integer({
description: 'the 1-based index of the URL in the list of URLs',
char: 'i'
})
char: 'i',
}),
}

static args = [
{
name: 'buildpack',
required: true,
description: 'namespace/name of the buildpack'
}
description: 'namespace/name of the buildpack',
},
]

async run() {
let {args, flags} = this.parse(Add)
let buildpackCommand = new BuildpackCommand(this.heroku)
const {args, flags} = this.parse(Add)
const buildpackCommand = new BuildpackCommand(this.heroku)

if (flags.index !== undefined) {
buildpackCommand.validateIndex(flags.index)
}

let buildpacks = await buildpackCommand.fetch(flags.app)
const buildpacks = await buildpackCommand.fetch(flags.app)
await buildpackCommand.validateUrlNotSet(buildpacks, args.buildpack)

let spliceIndex: number
if (flags.index === undefined) {
spliceIndex = buildpacks.length
} else {
let foundIndex = buildpackCommand.findIndex(buildpacks, flags.index)
const foundIndex = buildpackCommand.findIndex(buildpacks, flags.index)
spliceIndex = (foundIndex === -1) ? buildpacks.length : foundIndex
}

let buildpackUpdates = await buildpackCommand.mutate(flags.app, buildpacks, spliceIndex, args.buildpack, 'add')
const buildpackUpdates = await buildpackCommand.mutate(flags.app, buildpacks, spliceIndex, args.buildpack, 'add')
buildpackCommand.displayUpdate(flags.app, flags.remote || '', buildpackUpdates, 'added')
}
}
7 changes: 4 additions & 3 deletions packages/buildpacks/src/commands/buildpacks/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {BuildpackCommand} from '../../buildpacks'

export default class Clear extends Command {
static description = 'clear all buildpacks set on the app'

static flags = {
app: Flags.app({required: true}),
remote: Flags.remote()
remote: Flags.remote(),
}

async run() {
let {flags} = this.parse(Clear)
let buildpackCommand = new BuildpackCommand(this.heroku)
const {flags} = this.parse(Clear)
const buildpackCommand = new BuildpackCommand(this.heroku)
await buildpackCommand.clear(flags.app, 'clear', 'cleared')
}
}
9 changes: 5 additions & 4 deletions packages/buildpacks/src/commands/buildpacks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import {BuildpackCommand} from '../../buildpacks'

export default class Index extends Command {
static description = 'display the buildpacks for an app'

static flags = {
app: Flags.app({required: true}),
remote: Flags.remote()
remote: Flags.remote(),
}

async run() {
let {flags} = this.parse(Index)
let buildpacksCommand = new BuildpackCommand(this.heroku)
const {flags} = this.parse(Index)
const buildpacksCommand = new BuildpackCommand(this.heroku)

let buildpacks = await buildpacksCommand.fetch(flags.app)
const buildpacks = await buildpacksCommand.fetch(flags.app)
if (buildpacks.length === 0) {
this.log(`${flags.app} has no Buildpack URL set.`)
} else {
Expand Down
14 changes: 8 additions & 6 deletions packages/buildpacks/src/commands/buildpacks/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@ import {BuildpackRegistry} from '@heroku/buildpack-registry'

export default class Info extends Command {
static description = 'fetch info about a buildpack'

static args = [
{
name: 'buildpack',
required: true,
description: 'namespace/name of the buildpack'
}
description: 'namespace/name of the buildpack',
},
]

async run() {
let {args} = this.parse(Info)
let registry = new BuildpackRegistry()
const {args} = this.parse(Info)
const registry = new BuildpackRegistry()

Result.match({
// eslint-disable-next-line @typescript-eslint/no-empty-function
Ok: _ => {},
Err: err => {
this.error(`Could not publish the buildpack.\n${err}`)
},
}, BuildpackRegistry.isValidBuildpackSlug(args.buildpack))

let result = await registry.info(args.buildpack)
const result = await registry.info(args.buildpack)
Result.match({
Ok: buildpack => {
cli.styledHeader(args.buildpack)
Expand All @@ -37,7 +39,7 @@ export default class Info extends Command {
} else {
cli.error(`Problems finding buildpack info: ${err.description}`)
}
}
},
}, result)
}
}
18 changes: 10 additions & 8 deletions packages/buildpacks/src/commands/buildpacks/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import {BuildpackCommand} from '../../buildpacks'

export default class Remove extends Command {
static description = 'remove a buildpack set on the app'

static flags = {
app: Flags.app({required: true}),
remote: Flags.remote(),
index: Flags.integer({
description: 'the 1-based index of the URL to remove from the list of URLs',
char: 'i'
})
char: 'i',
}),
}

static args = [
{
name: 'buildpack',
description: 'namespace/name of the buildpack'
}
description: 'namespace/name of the buildpack',
},
]

async run() {
let {args, flags} = this.parse(Remove)
let buildpackCommand = new BuildpackCommand(this.heroku)
const {args, flags} = this.parse(Remove)
const buildpackCommand = new BuildpackCommand(this.heroku)

if (flags.index && args.buildpack) {
cli.error('Please choose either index or Buildpack, but not both.', {exit: 1})
Expand All @@ -31,7 +33,7 @@ export default class Remove extends Command {
cli.error('Usage: heroku buildpacks:remove [BUILDPACK_URL]. Must specify a buildpack to remove, either by index or URL.')
}

let buildpacks = await buildpackCommand.fetch(flags.app)
const buildpacks = await buildpackCommand.fetch(flags.app)
if (buildpacks.length === 0) {
cli.error(`No buildpacks were found. Next release on ${flags.app} will detect buildpack normally.`, {exit: 1})
}
Expand All @@ -51,7 +53,7 @@ export default class Remove extends Command {
if (buildpacks.length === 1) {
await buildpackCommand.clear(flags.app, 'remove', 'removed')
} else {
let buildpackUpdates = await buildpackCommand.mutate(flags.app, buildpacks, spliceIndex, args.buildpack, 'remove')
const buildpackUpdates = await buildpackCommand.mutate(flags.app, buildpacks, spliceIndex, args.buildpack, 'remove')
buildpackCommand.displayUpdate(flags.app, flags.remote || '', buildpackUpdates, 'removed')
}
}
Expand Down
Loading

0 comments on commit bb45760

Please sign in to comment.