Code Monkey home page Code Monkey logo

Comments (7)

nsamsami avatar nsamsami commented on May 30, 2024 2

@Junkern I'm a product manager at Heroku where we've been developing oclif. As we're just getting started with the project, I'd be really interested in your feedback and learning more about your use case. Feel free to respond here or drop us a note at [email protected]

from oclif.

jdxcode avatar jdxcode commented on May 30, 2024

by design we do not call any files so we only load the files we need. In a large project, it might take several seconds to create everything.

It sounds like you want an init hook. We should have some docs and a generator for that soon, but for now here is what you need to do:

First add a line like this to declare the hook: https://github.com/heroku/cli/blob/oclif/package.json#L56

(You will want init probably for your usage though, prerun happens later)

Then create a hook file like this: https://github.com/heroku/cli/blob/oclif/src/hooks/prerun/analytics.ts

You can also create your own hooks

from oclif.

jdxcode avatar jdxcode commented on May 30, 2024

reading your message again though, I would probably use a base class here. Init hooks are more for things you want to run on every command. It would work, but you'd likely have to involve some globals. Unfortunately I don't have a great example of setting this up, but I would do something like this:

class MyBaseClass extends Command {
  myApiClient = new ApiClient()
}

then in the command:

class extends MyBaseClass {
  async run() {
    this.myApiClient.doSomething()
  }
}

Which does not give you dependency injection though. Maybe https://github.com/thlorenz/proxyquire would be helpful?

from oclif.

jdxcode avatar jdxcode commented on May 30, 2024

A third idea:

class MyBaseClass extends Command {
  static apiClient = ApiClient
  myApiClient = new MyBaseClass.apiClient()
}

and in the test:

beforeEach(() => {
  MyBaseClass.apiClient = MockApiClient
})

Hopefully one of these ideas is helpful. Let me know if you have any ideas how else this might be done!

from oclif.

Junkern avatar Junkern commented on May 30, 2024

Thanks for the quick feedback!

I indeed went for the base class scenario. But instead of doing that with static, I simply but the initialization of the classes I need in all commands into the init() function. It worked, but I don't know if there are any downsides^^ Just tested it out with static and that works as well.

from oclif.

jdxcode avatar jdxcode commented on May 30, 2024

so long as you don't need to modify the static property in the test then doing it as an instance property should be fine. The advantage of static is you have access to it in the test if you want to modify it

from oclif.

Junkern avatar Junkern commented on May 30, 2024

That makes totally sense! Thanks a lot:)

from oclif.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.