cli icon indicating copy to clipboard operation
cli copied to clipboard

Control the loader in custom template to hide it for prompt feature

Open JeremyDolle opened this issue 2 years ago • 14 comments

Hi all 👋

Is there any way to access the ora loader in the post init script ?

The use case is that I have a custom template that prompt a question to the user. The problem is that the "Executing post init script" loader is above the response of the prompt.

It could be cool that we can control the loader in custom template to hide it for example. Or if someone have an idea how I can achieve this I will appreciate. I try a lot of things like moving the cursor position in the terminal but the "executing post init script" follow it.

Hope someone can help me 🙏 Thanks for all!

JeremyDolle avatar Nov 01 '21 20:11 JeremyDolle

PRs welcome :)

thymikee avatar Nov 01 '21 20:11 thymikee

Thanks @thymikee, I will try to investigate and see if I found several solution. If it makes sense, I will provide a PR.

Btw, how can I test the init command locally. I don't find it in the CONTRIBUTING.md file.

Thanks 🙏

JeremyDolle avatar Nov 03 '21 19:11 JeremyDolle

I'm also very interested in this. The use-case is exactly the same: my postInitiScript asks the user for the input

Shaddix avatar Jan 02 '22 14:01 Shaddix

Regrading PRs/implementation:

  1. I assume it's not possible to control the ora from postInitScript, since it's impossible to pass the ora object across the scripts run via exec.
  2. If 1. is correct, then the only way to achieve the goal is to actually NOT show the loader for postInitScript
  3. To do that I see 2 possibilities:
    1. Never show the loader for postInitScripts, and use logger.log('Executing post init script'); instead.
    2. Do not show the loader depending on an option/flag in template.config.js (e.g. skipLoaderForPostInitScript with default to false).

I could PR any of the 3.1 or 3.2, if I'm correct in 1. :)

For me 3.1 is preferred, but it breaks the current behavior. 3.2 leaves the current behavior as is, but adds a configuration option for the thing that (imo) doesn't worth a configuration option :)

Shaddix avatar Jan 02 '22 19:01 Shaddix

I arrived to the same conclusion ! For me 3.2 was my first choice because we leave the same behavior and only projects that need to control that loader add the flag.

Btw, Hope the PR will be accepted even if it's 3.1 ! 😊

JeremyDolle avatar Jan 03 '22 07:01 JeremyDolle

I'm good with moving the responsibility of showing the user progress of the "post init script" to the implementor – i.e. going with the solution 3.1. I would like the CLI's documentation to mention this responsibility though, ideally with an example of using ora in a similar fashion that we do today.

thymikee avatar Jan 10 '22 12:01 thymikee

@thymikee could you please check if PR created by Jeremy is ok?

Shaddix avatar Feb 06 '22 08:02 Shaddix

Cc @grabbou

thymikee avatar Feb 06 '22 10:02 thymikee

I just left a review on it.

grabbou avatar Feb 15 '22 13:02 grabbou

@grabbou , @thymikee , @Esemesek : Are there any news?

Angelk90 avatar Mar 23 '22 11:03 Angelk90

@Angelk90 we're waiting for @JeremyDolle to answer feedback on https://github.com/react-native-community/cli/pull/1526 :)

thymikee avatar Mar 23 '22 11:03 thymikee

Please @Angelk90 be patient. I will update the MR when I will have the time.

JeremyDolle avatar Mar 23 '22 12:03 JeremyDolle

Is there any way to clear this .info() instance from the log, or will there always be this spinner artifact + duplicates?

Isn't my implementation the same as the example in the linked PR?

✔ Downloading template
✔ Copying template
✔ Processing template
⠸ Executing post init script ⠋ Executing post init script 
Template initialization successful! 🚀
✔ Executing post init script 
✔ Executing post init script 
✔ Installing CocoaPods dependencies

Code:

const ora = require("ora");

const spinner = ora("Executing post init script ");

module.exports = {
  async success() {
    return new Promise((resolve) => {
      spinner.start();
      console.log("\nTemplate initialization successful! 🚀");
      resolve();
    })
      .then(() => {
        spinner.succeed();
      })
      .catch(() => {
        spinner.fail();
        throw new Error(
          "Something went wrong during the post init script execution"
        );
      });
  },
};


Even without interacting with the spinner, this unfulfilled spinner frame is leftover:

✔ Downloading template
✔ Copying template
✔ Processing template
⠸ Executing post init script 
Template initialization successful! 🚀
✔ Executing post init script 

cjhines avatar Apr 26 '22 07:04 cjhines

@cjhines, I think the .info() is not deployed yet. (@thymikee, @grabbou can you confirm ?)

I made a workaround waiting the deploy by dealing with ANSI Escape Sequences like this :

 console.log('\033[2A', 'Template initialization successful! 🚀');

for more details : https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

JeremyDolle avatar Apr 26 '22 07:04 JeremyDolle