Skip to content

Commit

Permalink
chore(cli): upgrade cli to eslint (heroku#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
chadian authored and RasPhilCo committed Feb 3, 2020
1 parent 5421196 commit 6b7711b
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 107 deletions.
1 change: 1 addition & 0 deletions packages/cli/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
8 changes: 8 additions & 0 deletions packages/cli/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"oclif",
"oclif-typescript"
],
"rules": {
}
}
9 changes: 6 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"devDependencies": {
"@oclif/dev-cli": "^1.21.3",
"@oclif/test": "^1.2.4",
"@oclif/tslint": "^3.1.1",
"@types/ansi-styles": "^3.2.1",
"@types/chai": "^4.1.7",
"@types/debug": "^4.1.2",
Expand All @@ -74,6 +73,9 @@
"@types/write-json-file": "^2.2.1",
"aws-sdk": "^2.421.0",
"chai": "^4.2.0",
"eslint": "^6.7.2",
"eslint-config-oclif": "^3.1.0",
"eslint-config-oclif-typescript": "^0.1.0",
"globby": "^10.0.1",
"lerna": "^3.18.0",
"lodash": "^4.17.11",
Expand All @@ -83,7 +85,6 @@
"read-pkg": "^4.0.1",
"sinon": "^7.2.4",
"ts-node": "^8.0.2",
"tslint": "^5.11.0",
"typescript": "3.3.3333"
},
"engines": {
Expand Down Expand Up @@ -274,11 +275,13 @@
},
"repository": "heroku/cli",
"scripts": {
"lint": "eslint . --ext .ts --config .eslintrc",
"build": "rm -rf lib && tsc",
"postpublish": "rm -f oclif.manifest.json",
"posttest": "tsc -p test --noEmit && tslint -p test -t stylish",
"prepack": "yarn run build && oclif-dev manifest",
"pretest": "tsc -p test --noEmit",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"posttest": "yarn lint",
"version": "oclif-dev readme --multi && git add README.md ../../docs"
},
"types": "lib/index.d.ts"
Expand Down
45 changes: 23 additions & 22 deletions packages/cli/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@ import deps from './deps'
const debug = require('debug')('heroku:analytics')

export interface RecordOpts {
Command: Config.Command.Class
argv: string[]
Command: Config.Command.Class;
argv: string[];
}

export interface AnalyticsInterface {
source: string,
event: string,
source: string;
event: string;
properties: {
cli: string,
command: string,
completion: number,
version: string,
plugin: string,
plugin_version: string,
os: string,
shell: string,
valid: boolean,
language: string,
install_id: string,
}
cli: string;
command: string;
completion: number;
version: string;
plugin: string;
plugin_version: string;
os: string;
shell: string;
valid: boolean;
language: string;
install_id: string;
};
}

export default class AnalyticsCommand {
config: Config.IConfig

userConfig!: typeof deps.UserConfig.prototype

http: typeof deps.HTTP

constructor(config: Config.IConfig) {
Expand Down Expand Up @@ -67,15 +69,14 @@ export default class AnalyticsCommand {
valid: true,
language: 'node',
install_id: this.userConfig.install,
}
},
}

const data = Buffer.from(JSON.stringify(analyticsData)).toString('base64')
if (this.authorizationToken) {
return this.http.get(`${this.url}?data=${data}`, {headers: {authorization: `Bearer ${this.authorizationToken}`}}).catch(error => debug(error))
} else {
return this.http.get(`${this.url}?data=${data}`).catch(error => debug(error))
}
return this.http.get(`${this.url}?data=${data}`).catch(error => debug(error))
}

get url(): string {
Expand All @@ -92,7 +93,7 @@ export default class AnalyticsCommand {

get usingHerokuAPIKey(): boolean {
const k = process.env.HEROKU_API_KEY
return !!(k && k.length > 0)
return Boolean(k && k.length > 0)
}

get netrcLogin(): string | undefined {
Expand All @@ -106,8 +107,8 @@ export default class AnalyticsCommand {

async _acAnalytics(id: string): Promise<number> {
if (id === 'autocomplete:options') return 0
let root = path.join(this.config.cacheDir, 'autocomplete', 'completion_analytics')
let meta = {
const root = path.join(this.config.cacheDir, 'autocomplete', 'completion_analytics')
const meta = {
cmd: deps.file.exists(path.join(root, 'command')),
flag: deps.file.exists(path.join(root, 'flag')),
value: deps.file.exists(path.join(root, 'value')),
Expand Down
21 changes: 10 additions & 11 deletions packages/cli/src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import FS = require('fs-extra')
import {HTTP} from 'http-call'
import UserConfig from './user-config'
import FS = require('fs-extra')

import file = require('./file')
import UserConfig from './user-config'

const cache: any = {}
function fetch(s: string) {
if (!cache[s]) {
cache[s] = require(s)
}
return cache[s]
}

export default {
get fs(): typeof FS {
Expand All @@ -18,12 +26,3 @@ export default {
return fetch('./user-config').default
},
}

const cache: any = {}

function fetch(s: string) {
if (!cache[s]) {
cache[s] = require(s)
}
return cache[s]
}
19 changes: 9 additions & 10 deletions packages/cli/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import deps from './deps'
const debug = require('debug')('heroku-cli:file')

export function exists(f: string): Promise<boolean> {
// debug('exists', f)
// @ts-ignore
return deps.fs.pathExists(f)
}

Expand All @@ -28,23 +26,24 @@ export async function remove(file: string) {
}

export async function ls(dir: string): Promise<{ path: string; stat: FS.Stats }[]> {
let files = await deps.fs.readdir(dir)
let paths = files.map(f => path.join(dir, f))
const files = await deps.fs.readdir(dir)
const paths = files.map(f => path.join(dir, f))
return Promise.all(paths.map(path => deps.fs.stat(path).then(stat => ({path, stat}))))
}

export async function removeEmptyDirs(dir: string): Promise<void> {
let files
try {
files = await ls(dir)
} catch (err) {
if (err.code === 'ENOENT') return
throw err
} catch (error) {
if (error.code === 'ENOENT') return
throw error
}
let dirs = files.filter(f => f.stat.isDirectory()).map(f => f.path)
for (let p of dirs.map(removeEmptyDirs)) await p
const dirs = files.filter(f => f.stat.isDirectory()).map(f => f.path)
// eslint-disable-next-line no-await-in-loop
for (const p of dirs.map(removeEmptyDirs)) await p
files = await ls(dir)
if (!files.length) await remove(dir)
if (files.length === 0) await remove(dir)
}

export async function readJSON(file: string) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare namespace NodeJS {
interface Global {
columns?: number
testing?: boolean
columns?: number;
testing?: boolean;
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/hooks/init/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const Whitelist = [

export const version: Hook.Init = async function () {
if (['-v', '--version', 'version'].includes(process.argv[2])) {
for (let env of Whitelist) {
for (const env of Whitelist) {
if (process.env[env]) {
let value = env === 'HEROKU_API_KEY' ? 'to [REDACTED]' : `to ${process.env[env]}`
const value = env === 'HEROKU_API_KEY' ? 'to [REDACTED]' : `to ${process.env[env]}`
this.warn(`${env} set ${value}`)
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/hooks/update/b.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function brew(args: string[], opts: SpawnSyncOptions = {}) {

interface InstallReceipt {
source: {
tap: string
}
tap: string;
};
}

export const brewHook: Hook<'update'> = async function () {
Expand All @@ -24,9 +24,9 @@ export const brewHook: Hook<'update'> = async function () {
let binPath
try {
binPath = fs.realpathSync(path.join(brewRoot, 'bin/heroku'))
} catch (err) {
if (err.code === 'ENOENT') return
throw err
} catch (error) {
if (error.code === 'ENOENT') return
throw error
}
let cellarPath: string
if (binPath && binPath.startsWith(path.join(brewRoot, 'Cellar'))) {
Expand All @@ -39,7 +39,7 @@ export const brewHook: Hook<'update'> = async function () {
}

const needsMigrate = async (): Promise<boolean> => {
let receipt = await fetchInstallReceipt()
const receipt = await fetchInstallReceipt()
if (!receipt) return false
return receipt.source.tap === 'homebrew/core'
}
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/hooks/update/brew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function brew(args: string[], opts: SpawnSyncOptions = {}) {

interface InstallReceipt {
source: {
tap: string
}
tap: string;
};
}

export const brewHook: Hook<'update'> = async function () {
Expand All @@ -24,9 +24,9 @@ export const brewHook: Hook<'update'> = async function () {
let binPath
try {
binPath = fs.realpathSync(path.join(brewRoot, 'bin/heroku'))
} catch (err) {
if (err.code === 'ENOENT') return
throw err
} catch (error) {
if (error.code === 'ENOENT') return
throw error
}
let cellarPath: string
if (binPath && binPath.startsWith(path.join(brewRoot, 'Cellar'))) {
Expand All @@ -39,7 +39,7 @@ export const brewHook: Hook<'update'> = async function () {
}

const needsMigrate = async (): Promise<boolean> => {
let receipt = await fetchInstallReceipt()
const receipt = await fetchInstallReceipt()
if (!receipt) return false
return receipt.source.tap === 'homebrew/core'
}
Expand Down
14 changes: 8 additions & 6 deletions packages/cli/src/hooks/update/plugin-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ export const migrate: Hook<'init'> = async function () {
const p = path.join(pluginsDir, 'user.json')
if (await fs.pathExists(p)) {
const {manifest} = await fs.readJSON(p)
for (let plugin of Object.keys(manifest.plugins)) {
for (const plugin of Object.keys(manifest.plugins)) {
process.stderr.write(`heroku-cli: migrating ${plugin}\n`)
// eslint-disable-next-line no-await-in-loop
await exec('heroku', ['plugins:install', plugin])
}
}
} catch (err) {
this.warn(err)
} catch (error) {
this.warn(error)
}
try {
const p = path.join(pluginsDir, 'link.json')
if (await fs.pathExists(p)) {
const {manifest} = await fs.readJSON(path.join(pluginsDir, 'link.json'))
for (let {root} of Object.values(manifest.plugins) as any) {
for (const {root} of Object.values(manifest.plugins) as any) {
process.stderr.write(`heroku-cli: migrating ${root}\n`)
// eslint-disable-next-line no-await-in-loop
await exec('heroku', ['plugins:link', root])
}
}
} catch (err) {
this.warn(err)
} catch (error) {
this.warn(error)
}
await fs.remove(pluginsDir)
process.stderr.write('heroku: done migrating plugins\n')
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/hooks/update/tidy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import deps from '../../deps'

export const tidy: Hook<'update'> = async function () {
const cleanupPlugins = async () => {
let pluginsDir = path.join(this.config.dataDir, 'plugins')
const pluginsDir = path.join(this.config.dataDir, 'plugins')
if (await deps.file.exists(path.join(pluginsDir, 'plugins.json'))) return
let pjson
try {
pjson = await deps.file.readJSON(path.join(pluginsDir, 'package.json'))
} catch (err) {
if (err.code !== 'ENOENT') throw err
} catch (error) {
if (error.code !== 'ENOENT') throw error
return
}
if (!pjson.dependencies || pjson.dependencies === {}) {
Expand Down
Loading

0 comments on commit 6b7711b

Please sign in to comment.