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

Issues when running script from Usage in readme #530

Closed
tr00st opened this issue Oct 19, 2022 · 2 comments · Fixed by #532
Closed

Issues when running script from Usage in readme #530

tr00st opened this issue Oct 19, 2022 · 2 comments · Fixed by #532

Comments

@tr00st
Copy link

tr00st commented Oct 19, 2022

Describe the bug
Couple of possible issues (or unlisted expectations) with the Usage of the README:

  • Appears to require a package.json set with type: 'module'?
    • Error returned when running: Cannot use import statement outside a module
  • --help doesn't seem to work after patching to use require()
    • TypeError: cmd._help is not a function - had this elsewhere when migrating, couldn't find where _help was supposed to be set to try and debug it
    • Possibly to be raised elsewhere? Advice on how to refer to this usage scenario when raising that bug would be appreciated.

To Reproduce
Steps to reproduce the behavior:

  1. Init a new npm package (npm init)
  2. Add oclif (npm install -S oclif)
  3. Create myscript with contents matching Usage
  4. Make executable (chmod +x myscript)
  5. Run it (./myscript)

Expected behavior
Command output roughly matches the README

Environment (please complete the following information):

  • Win11 w/ PowerShell
  • or Debian buster + bash-5.0

Additional context
As mentioned above - had issues with the --help flag when migrating (from 1.x) using the same sort of file structure as the one used in this repo's Usage section, so figured I'd start again from scratch at that point. Not sure if the help not working is a bug with the help code or an issue with how it's used in the example?

Patched source below, updated to use require() and formatted to pass our eslint checks so my editor doesn't complain:

#!/usr/bin/env ts-node

const fs = require('fs');
const {Command, Flags} = require('@oclif/core');

class LS extends Command {
    static flags = {
        version: Flags.version(),
        help: Flags.help(),
        // run with --dir= or -d=
        dir: Flags.string({
            char: 'd',
            default: process.cwd(),
        }),
    };

    async run() {
        const {flags} = await this.parse(LS);
        let files = fs.readdirSync(flags.dir);
        for (let f of files) {
            this.log(f);
        }
    }
}

LS.run()
    .catch(require('@oclif/core/handle'));
@mdonnalley
Copy link
Contributor

@tr00st Thanks for filing the issue. I just created #532 to update the example and to fix the broken --help flag.

As a side note, I'd encourage you to use a generated cli (npx oclif generate my-cli) instead of trying to use oclif/core in a script. While it should work for you, it's not a use case we can put a lot of support towards right now

@tr00st
Copy link
Author

tr00st commented Oct 20, 2022

@mdonnalley Much appreciated - I'll certainly look to use the generator going forwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants