cli
cli copied to clipboard
Control the loader in custom template to hide it for prompt feature
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!
PRs welcome :)
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 🙏
I'm also very interested in this. The use-case is exactly the same: my postInitiScript asks the user for the input
Regrading PRs/implementation:
- I assume it's not possible to control the
ora
from postInitScript, since it's impossible to pass theora
object across the scripts run viaexec
. - If 1. is correct, then the only way to achieve the goal is to actually NOT show the loader for postInitScript
- To do that I see 2 possibilities:
- Never show the loader for
postInitScripts
, and uselogger.log('Executing post init script');
instead. - Do not show the loader depending on an option/flag in
template.config.js
(e.g.skipLoaderForPostInitScript
with default tofalse
).
- Never show the loader for
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 :)
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 ! 😊
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 could you please check if PR created by Jeremy is ok?
Cc @grabbou
I just left a review on it.
@grabbou , @thymikee , @Esemesek : Are there any news?
@Angelk90 we're waiting for @JeremyDolle to answer feedback on https://github.com/react-native-community/cli/pull/1526 :)
Please @Angelk90 be patient. I will update the MR when I will have the time.
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, 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