Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2.0.0 #539

Merged
merged 45 commits into from
Jan 12, 2023
Merged

Version 2.0.0 #539

merged 45 commits into from
Jan 12, 2023

Conversation

mdonnalley
Copy link
Contributor

@mdonnalley mdonnalley commented Oct 26, 2022

Breaking Changes

Command Args

We updated the Command.args to more closely resemble flags

Before

import { Command } from '@oclif/core'

export default MyCommand extends Command {
  static args = [{name: arg1, description: 'an argument', required: true}]

  public async run(): Promise<void> {
    const {args} = await this.parse(MyCommand) // args is useless {[name: string]: any}
  }
}

After

import { Command, Args } from '@oclif/core'

export default MyCommand extends Command {
  static args = {
    arg1: Args.string({description: 'an argument', required: true})
  }

  public async run(): Promise<void> {
    const {args} = await this.parse(MyCommand) // args is { arg1: string }
  }
}

These are the available Args:

  • string
  • integer
  • boolean
  • url
  • file
  • directory
  • custom

Interfaces

  • Removed Interfaces.Command since they were not usable for tests. These are replaced by types that are available under the Command namespace
Interfaces.Command => Command.Cached
Interfaces.Command.Class => Command.Class
Interfaces.Command.Loadable => Command.Lodable
  • Removed the following interfaces from the export. Exporting all of these made it difficult to make non-breaking changes when modifying types and/or fixing compilation bugs. We are open to PRs to reintroduce these to the export if they are needed for your project
    • Arg
    • ArgInput
    • ArgToken
    • CLIParseErrorOptions
    • CompletableFlag
    • CompletableOptionFlag
    • Completion
    • CompletionContext
    • Default
    • DefaultContext
    • Definition
    • EnumFlagOptions
    • FlagBase
    • FlagInput
    • FlagOutput
    • FlagToken
    • FlagUsageOptions
    • Input
    • List
    • ListItem
    • Metadata
    • OptionalArg
    • OptionFlagProps
    • OutputArgs
    • OutputFlags
    • ParseFn
    • ParserArg
    • ParserInput
    • ParserOutput
    • ParsingToken
    • RequiredArg

CliUx

We flattened CliUx.ux into ux for ease of use

Before

import {CliUx} from '@oclif/core'

CliUx.ux.log('Hello World')

After

import {ux} from '@oclif/core'

ux.log('Hello World')

CliUx.ux.open

We removed the open method since it was a direct import/export of the open package. If you need this functionality, then you should import open yourself.

Flags

  • Flags.custom replaces Flags.build, Flags.enum, and Flags.option
  • Removed builtin color flag
  • Renamed globalFlags to baseFlags
    • globalFlags was a misleading name because the flags added there weren't actually global to the entire CLI. Instead, they were just flags that would be inherited by any command that extended the command class they were defined in.

Flag and Arg Parsing

  • In v1, any input that didn't match a flag definition was assumed to be an argument. This meant that misspelled flags, e.g. --hekp were parsed as arguments, instead of throwing an error. In order to handle this, oclif now assumes that anything that starts with a hypen must be a flag and will throw an error if no corresponding flag definition is found. In other words, your command can no longer accept arguments that begin with a hyphen (fixes Adding strict=false parses a non-existing flag as a arg #526)
  • v1 allowed you to return an array from a flag's parse. This was added to support backwards compatibility for flags that separated values by commas (e.g. my-flag=val1,val2). However, this was problematic because it didn't allow the parse to manipulate the individual values. If you need this functionality, you can now set a delimiter option on your flags. By doing so, oclif will split the string on the delimiter before parsing.

ESM/CJS Friendliness

Writing plugins with ESM has always been possible, but it requires a handful of modifications for it to work, especially in the bin scripts. In v2 we've introduced an execute method that the bin scripts can use to avoid having to make changes for ESM of CJS.

CJS bin/dev before

#!/usr/bin/env node

const oclif = require('@oclif/core')

const path = require('path')
const project = path.join(__dirname, '..', 'tsconfig.json')

// In dev mode -> use ts-node and dev plugins
process.env.NODE_ENV = 'development'

require('ts-node').register({project})

// In dev mode, always show stack traces
oclif.settings.debug = true;


// Start the CLI
oclif.run().then(oclif.flush).catch(oclif.Errors.handle)

CJS bin/dev.js after

#!/usr/bin/env node
// eslint-disable-next-line node/shebang
(async () => {
  const oclif = await import('@oclif/core')
  await oclif.execute({type: 'cjs', development: true, dir: __dirname})
})()

ESM bin/dev.js before

#!/usr/bin/env ts-node

/* eslint-disable node/shebang */

import oclif from '@oclif/core'
import path from 'node:path'
import url from 'node:url'
// eslint-disable-next-line node/no-unpublished-import
import {register} from 'ts-node'

// In dev mode -> use ts-node and dev plugins
process.env.NODE_ENV = 'development'

register({
  project: path.join(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'tsconfig.json'),
})

// In dev mode, always show stack traces
oclif.settings.debug = true

// Start the CLI
oclif
.run(process.argv.slice(2), import.meta.url)
.then(oclif.flush)
.catch(oclif.Errors.handle)

ESM bin/dev.js after

#!/usr/bin/env node
// eslint-disable-next-line node/shebang
(async () => {
  const oclif = await import('@oclif/core')
  await oclif.execute({type: 'esm', dir: import.meta.url})
})()

Note that ESM and CJS plugins still require different settings in the tsconfig.json - you will still need to make those modifications yourself.

Other Changes

TODO

  • Remove ux.open method (users can import the open library directly if they need it)
  • More unit tests for arg parsing
  • Handle will-fix-in-v2 issues
  • Update migration guide
  • Update oclif.io docs
  • Investigate impact on oclif and salesforce plugins
  • Deprecate @oclif/screen
  • Deprecate @oclif/linewrap

Related

oclif/oclif.github.io#188

@mdonnalley mdonnalley marked this pull request as draft October 26, 2022 20:36
@mdonnalley mdonnalley self-assigned this Oct 26, 2022
@mshanemc mshanemc removed their request for review October 28, 2022 16:46
@mdonnalley mdonnalley changed the base branch from main to prerelease/v2 January 12, 2023 17:23
@mdonnalley mdonnalley marked this pull request as ready for review January 12, 2023 17:30
@mdonnalley mdonnalley changed the title [DO NOT MERGE] Version 2.0.0 Version 2.0.0 Jan 12, 2023
@mdonnalley mdonnalley merged commit d5f45af into prerelease/v2 Jan 12, 2023
@mdonnalley mdonnalley deleted the mdonnalley/v2 branch January 12, 2023 17:32
mdonnalley added a commit that referenced this pull request Jan 23, 2023
* chore: create v2 prerelease

* fix: tests

* chore(release): 2.0.0-beta.1 [skip ci]

* chore(release): 2.0.1 [skip ci]

* fix: onRelease github action

* fix: correct version

* chore(release): 2.0.0-beta.3 [skip ci]

* feat: version 2.0.0 (#539)

* feat: improve interfaces

* feat: new command types

* feat: more changes

* chore: clean up

* chore: enable eslint rules

* chore: add files

* feat: remove parser/deps

* chore: more clean up

* chore: tests

* feat: rename globalFlags to baseFlags

* feat: typed args

* chore: renaming things and cleanup

* chore: make tests compilable

* chore: add args types test

* chore: get tests passing

* feat: flatten ux

* fix: ux.table export

* fix: read from stdin

* fix: args and cleanup

* chore: tests

* fix: arg and flag types

* fix: small bugs

* fix: handle non-existent flags

* chore: tests

* chore: compilation issues

* chore: update cli-ux docs

* feat: add execute

* refactor: clean up cli-ux imports

* test: begin removing fancy-test

* feat: remove ux.open

* chore: cleam up from merge

* feat: add delimiter option

* fix: deprecateAliases

* chore: update tests

* chore: update migration guide

* fix: add backwards compatability for v1 args

* fix: typing for default context

* fix: styledObject type and boolean flag parser

* fix: types

* chore(release): 2.0.0-beta.4 [skip ci]

* fix: make ux stubbable again

* chore(release): 2.0.0-beta.5 [skip ci]

* fix: default id on statically instantiated commands

* chore(release): 2.0.0-beta.6 [skip ci]

* fix: remove PromiseLike

* chore(release): 2.0.0-beta.7 [skip ci]

* fix: bump package version

* chore(release): 2.0.2-beta.1 [skip ci]

* fix: add missing dev dependency

* chore(release): 2.0.2-beta.2 [skip ci]

* fix: force publish

* chore(release): 2.0.2-beta.4 [skip ci]

* fix: type issues

* chore(release): 2.0.2-beta.5 [skip ci]

* fix: types

* chore(release): 2.0.2-beta.6 [skip ci]

* fix: more backwards compatiblity

* chore(release): 2.0.2-beta.7 [skip ci]

* chore(release): 2.0.2-beta.8 [skip ci]

* fix: code review

* chore(release): 2.0.2-beta.9 [skip ci]

* chore(release): 2.0.2-beta.10 [skip ci]

* chore(release): 2.0.2-beta.11 [skip ci]

Co-authored-by: svc-cli-bot <svc_cli_bot@salesforce.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant