Skip to content

Commit

Permalink
Merge pull request #405 from oclif/mdonnalley/clean-up
Browse files Browse the repository at this point in the history
feat: clean up
  • Loading branch information
mdonnalley committed Oct 24, 2023
2 parents df12997 + d4f50ca commit 2a56ea3
Show file tree
Hide file tree
Showing 17 changed files with 1,178 additions and 198 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@commitlint/config-conventional"]
}
11 changes: 0 additions & 11 deletions .editorconfig

This file was deleted.

5 changes: 1 addition & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"extends": [
"oclif",
"oclif-typescript"
],
"extends": ["oclif", "oclif-typescript", "prettier"],
"rules": {
"unicorn/import-style": 1,
"unicorn/prefer-node-protocol": 0
Expand Down
4 changes: 2 additions & 2 deletions .git2gus/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"productTag": "a1aB0000000ce2IIAQ",
"defaultBuild": "offcore.tooling.55",
"issueTypeLabels": { "enhancement": "USER STORY", "bug": "BUG P3" },
"defaultBuild": "offcore.tooling.59",
"issueTypeLabels": {"enhancement": "USER STORY", "bug": "BUG P3"},
"hideWorkItemUrl": true,
"statusWhenClosed": "CLOSED"
}
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname $0)/_/husky.sh"

yarn commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged --concurrent false
5 changes: 5 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.json": ["prettier --write"],
"*.md": ["prettier --write"],
"+(src|test)/**/*.+(ts|js)": ["eslint --fix", "prettier --write"]
}
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@oclif/prettier-config"
4 changes: 0 additions & 4 deletions git2gus/config.json

This file was deleted.

18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@
"author": "Salesforce",
"bugs": "https://github.com/oclif/test/issues",
"dependencies": {
"@oclif/core": "^3.0.0-beta.19",
"@oclif/core": "^3.6.0",
"chai": "^4.3.10",
"fancy-test": "^3.0.1"
},
"devDependencies": {
"@commitlint/config-conventional": "^18.0.0",
"@oclif/prettier-config": "^0.2.1",
"@types/cli-progress": "^3.11.4",
"@types/mocha": "^10",
"@types/node": "^18",
"commitlint": "^18.0.0",
"eslint": "^8.52.0",
"eslint-config-oclif": "^5.0.0",
"eslint-config-oclif-typescript": "^2.0.1",
"eslint-config-oclif-typescript": "^3.0.8",
"eslint-config-prettier": "^9.0.0",
"globby": "^11.0.1",
"husky": "^8.0.3",
"lint-staged": "^15.0.2",
"mocha": "^10",
"nock": "^13.3.6",
"prettier": "^3.0.3",
"shx": "^0.3.3",
"ts-node": "^10.9.1",
"tslib": "^2.6.2",
"typescript": "^5"
},
"engines": {
Expand All @@ -40,9 +47,10 @@
"build": "shx rm -rf lib && tsc",
"lint": "eslint . --ext .ts",
"posttest": "yarn lint",
"prepare": "husky install",
"prepublishOnly": "yarn run build",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"pretest": "yarn build --noEmit && tsc -p test --noEmit"
"pretest": "yarn build --noEmit && tsc -p test --noEmit",
"test": "mocha --forbid-only \"test/**/*.test.ts\""
},
"types": "lib/index.d.ts"
}
17 changes: 10 additions & 7 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ const castArray = <T>(input?: T | T[]): T[] => {
return Array.isArray(input) ? input : [input]
}

export function command(args: string[] | string, opts: loadConfig.Options = {}): {
run(ctx: {
config: Interfaces.Config; expectation: string; returned: unknown
}): Promise<void>
type Context = {config: Interfaces.Config; expectation: string; returned: unknown}

export function command(
args: string | string[],
opts: loadConfig.Options = {},
): {
run(ctx: Context): Promise<void>
} {
return {
async run(ctx: {config: Interfaces.Config; expectation: string; returned: unknown}) {
if (!ctx.config || opts.reset) ctx.config = await loadConfig(opts).run({} as any)
async run(ctx: Context) {
if (!ctx.config || opts.reset) ctx.config = await loadConfig(opts).run({} as Context)
args = castArray(args)
const [id, ...extra] = args
const cmdId = toStandardizedId(id, ctx.config)
ctx.expectation = ctx.expectation || `runs ${args.join(' ')}`
await ctx.config.runHook('init', {id: cmdId, argv: extra})
await ctx.config.runHook('init', {argv: extra, id: cmdId})
ctx.returned = await ctx.config.runCommand(cmdId, extra)
},
}
Expand Down
18 changes: 10 additions & 8 deletions src/exit.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import {Errors} from '@oclif/core'
import {expect} from 'chai'

// eslint-disable-next-line valid-jsdoc
/**
* ensures that a oclif command or hook exits
*/
export default (code = 0): {
run(): never; catch(ctx: {
error: any
}): void
export default (
code = 0,
): {
catch(ctx: {error: Errors.CLIError}): void
run(): never
} => ({
catch(ctx: {error: Errors.CLIError}) {
if (!ctx.error.oclif || ctx.error.oclif.exit === undefined) throw ctx.error
expect(ctx.error.oclif.exit).to.equal(code)
},
run() {
expect(process.exitCode).to.equal(code)
throw new Error(`Expected to exit with code ${code} but it ran without exiting`)
},
catch(ctx: {error: any}) {
if (!ctx.error.oclif || ctx.error.oclif.exit === undefined) throw ctx.error
expect(ctx.error.oclif.exit).to.equal(code)
},
})
16 changes: 10 additions & 6 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Interfaces} from '@oclif/core'

import {loadConfig} from './load-config'

type Context = {config: Interfaces.Config; expectation: string; returned: unknown}

// eslint-disable-next-line valid-jsdoc
/**
* tests a oclif hook
Expand All @@ -14,14 +16,16 @@ import {loadConfig} from './load-config'
* @param {string} event hook to run
* @param {object} hookOpts options to pass to hook. Config object will be passed automatically.
*/
export default (event: string, hookOpts: Record<string, unknown> = {}, options: loadConfig.Options = {}): {
run(ctx: {
config: Interfaces.Config; expectation: string; returned: unknown
}): Promise<void>;
export default (
event: string,
hookOpts: Record<string, unknown> = {},
options: loadConfig.Options = {},
): {
run(ctx: Context): Promise<void>
} => ({
async run(ctx: {config: Interfaces.Config; expectation: string; returned: unknown}) {
async run(ctx: Context) {
if (!event) throw new Error('no hook provided')
if (!ctx.config) ctx.config = await loadConfig(options).run({} as any)
if (!ctx.config) ctx.config = await loadConfig(options).run({} as Context)
ctx.expectation = ctx.expectation || `runs ${event} hook`
ctx.returned = await ctx.config.runHook(event, hookOpts || {})
},
Expand Down
21 changes: 10 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {dirname} from 'node:path'

import {fancy} from 'fancy-test'
import {dirname} from 'node:path'

import {command} from './command'
import exit from './exit'
Expand All @@ -18,18 +17,18 @@ function traverseFilePathUntil(filename: string, predicate: (filename: string) =

// Update to path.dirname(url.fileURLToPath(import.meta.url)) whenever we update tsconfig target to ES2020
// eslint-disable-next-line unicorn/prefer-module
loadConfig.root = traverseFilePathUntil(require.main?.path ?? module.path, p => !p.includes('node_modules'))
loadConfig.root = traverseFilePathUntil(require.main?.path ?? module.path, (p) => !p.includes('node_modules'))

export const test = fancy
.register('loadConfig', loadConfig)
.register('command', command)
.register('exit', exit)
.register('hook', hook)
.env({NODE_ENV: 'test'})
.register('loadConfig', loadConfig)
.register('command', command)
.register('exit', exit)
.register('hook', hook)
.env({NODE_ENV: 'test'})

export default test

export {Config} from '@oclif/core'
export {expect, FancyTypes} from 'fancy-test'

export {command} from './command'

export {Config} from '@oclif/core'
export {FancyTypes, expect} from 'fancy-test'
11 changes: 7 additions & 4 deletions src/load-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {Interfaces, Config} from '@oclif/core'
import {Config, Interfaces} from '@oclif/core'

/**
* loads CLI plugin/multi config
* @param {loadConfig.Options} opts options
* @return {Promise<Interfaces.Config>} config
*/
export function loadConfig(opts: loadConfig.Options = {}): { run(ctx: { config: Interfaces.Config}): Promise<Interfaces.Config> } {
export function loadConfig(opts: loadConfig.Options = {}): {
run(ctx: {config: Interfaces.Config}): Promise<Interfaces.Config>
} {
return {
async run(ctx: {config: Interfaces.Config}) {
ctx.config = await Config.load(opts.root || loadConfig.root)
Expand All @@ -14,10 +16,11 @@ export function loadConfig(opts: loadConfig.Options = {}): { run(ctx: { config:
}
}

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace loadConfig {
export let root: string
export interface Options {
root?: string;
reset?: boolean;
reset?: boolean
root?: string
}
}
11 changes: 3 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
"compilerOptions": {
"declaration": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"module": "commonjs",
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./lib",
"pretty": true,
"rootDirs": [
"./src"
],
"rootDirs": ["./src"],
"strict": true,
"target": "es2017",
"target": "es2022",
"esModuleInterop": true
},
"include": [
"./src/**/*"
]
"include": ["./src/**/*"]
}
Loading

0 comments on commit 2a56ea3

Please sign in to comment.