From 84617a1b51ae12f71f0590b88793549e4b8152c0 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Wed, 4 Oct 2023 14:33:56 -0600 Subject: [PATCH] chore: esm docs --- docs/ES2022.md | 73 ------------------------------------------- docs/esm.md | 11 +++---- website/sidebars.json | 3 +- 3 files changed, 6 insertions(+), 81 deletions(-) delete mode 100644 docs/ES2022.md diff --git a/docs/ES2022.md b/docs/ES2022.md deleted file mode 100644 index 3dbef0df..00000000 --- a/docs/ES2022.md +++ /dev/null @@ -1,73 +0,0 @@ ---- -title: ES2022 ---- - -If your tsconfig.json targets `ES2022` or newer there are some changes you will need to make to your commands to ensure that flags are properly set. See [this comment](https://github.com/oclif/oclif/issues/1100#issuecomment-1454910926) on github for more context on why these changes are necessary. - -## Use static initialization blocks - -All static properties on `Command` classes need to be set inside of a [static initialization block](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks). - -For instance your command may look something like this: - -```typescript -import {Command, Flags} from '@oclif/core' - -export default class Hello extends Command { - static summary = 'Say hello!' - static aliases = ['howdy'] - static enableJsonFlag = true - static flags = { - name: Flags.string({ - summary: 'person to say hello to', - required: true, - char: 'n', - }) - } - - public async run(): Promise<{name: string}> { - const {flags} = await this.parse(Hello) - this.log(`hello ${flags.name}`) - return { - name: flags.name - } - } -} -``` - -To target ES2022 you'll need to update the command to look like this: - -```typescript -import {Command, Flags} from '@oclif/core' - -export default class Hello extends Command { - static summary - static aliases - static enableJsonFlag - // It's very important to declare the static flags prop here. - // Otherwise, you'll loose all type safety on flags. - static flags - - static { - this.summary = 'Say hello!' - this.aliases = ['howdy'] - this.enableJsonFlag = true - this.flags = { - name: Flags.string({ - summary: 'person to say hello to', - required: true, - char: 'n', - }), - } - } - - public async run(): Promise<{name: string}> { - const {flags} = await this.parse(Hello) - this.log(`hello ${flags.name}`) - return { - name: flags.name - } - } -} - -``` diff --git a/docs/esm.md b/docs/esm.md index fc6ba432..ad855998 100644 --- a/docs/esm.md +++ b/docs/esm.md @@ -2,7 +2,7 @@ title: ESM --- -Version [3.0.0 of `@oclif/core`](https://github.com/oclif/core/tree/3.0.0-beta.12) officially supports ESM plugin development and CJS/ESM interoperability, meaning that you can have a root plugin written with CJS and your bundled plugins written in ESM or vice versa. +Version 3.0.0 of `@oclif/core` officially supports ESM plugin development and CJS/ESM interoperability, meaning that you can have a root plugin written with CJS and your bundled plugins written in ESM or vice versa. - [Interoperability Overview](#interoperability-overview) - [ESM Root plugin](#esm-root-plugin) @@ -68,7 +68,7 @@ Rename `bin/dev` to `bin/dev.js` and replace the existing code with the followin ```js #!/usr/bin/env node // eslint-disable-next-line node/shebang -void (async () => { +(async () => { const oclif = await import('@oclif/core') await oclif.execute({development: true, dir: import.meta.url}) })() @@ -79,7 +79,7 @@ This leverages oclif's `execute` function which handles all the development setu ```js #!/usr/bin/env node // eslint-disable-next-line node/shebang -void (async () => { +(async () => { const oclif = await import('@oclif/core') oclif.settings.performanceEnabled = true await oclif.execute({type: 'esm', development: true, dir: import.meta.url}) @@ -92,7 +92,7 @@ Rename `bin/run` to `bin/run.js` and replace the existing code with the followin ```js #!/usr/bin/env node -void (async () => { +(async () => { const oclif = await import('@oclif/core') await oclif.execute({dir: import.meta.url}) })() @@ -136,8 +136,7 @@ to }, ``` -You may have references to the bin scripts in your `.vscode/launch.json`. You'll need to update these as well. - +You may have references to the bin scripts in your `.vscode/launch.json` or in the `scripts` of your `package.json`. You'll need to update these as well. ### Update mocharc settings diff --git a/website/sidebars.json b/website/sidebars.json index cd1e4021..b9b8b226 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -39,8 +39,7 @@ "flexible_taxonomy", "global_flags", "single_command_cli", - "esm", - "ES2022" + "esm" ], "Also See": [ "examples",