getLogs.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import chalk from 'chalk';
  2. // @ts-ignore
  3. import gradient from 'gradient-string';
  4. import type { RenderContext } from './getRenderContext';
  5. export function logBanner() {
  6. console.log(`\n${getBanner()}\n`);
  7. }
  8. export function logSuccess(message: string) {
  9. console.log(chalk.green('✔︎') + ` ${message}`);
  10. }
  11. export function logError(message: string) {
  12. console.log(chalk.red('✖') + ` ${message}`);
  13. }
  14. export function logErrorAndExit(message: string) {
  15. logError(message);
  16. process.exit(1);
  17. }
  18. export async function logStep<T>(title: string, callback: () => T): Promise<T> {
  19. try {
  20. const result = await spinner(`${title}...`, callback);
  21. logSuccess(`${title}.`);
  22. return result;
  23. } catch (e) {
  24. logError(`${title}.\n`);
  25. console.log(e);
  26. process.exit(1);
  27. }
  28. }
  29. export function logDone(ctx: RenderContext) {
  30. console.log(chalk.green('✔︎') + ` ${ctx.language.infos.done}\n`);
  31. // Log next steps: Cd into the target directory.
  32. if (ctx.targetDirectory !== ctx.currentDirectory) {
  33. const cdCommand = `cd ${ctx.targetDirectoryName.includes(' ') ? `"${ctx.targetDirectoryName}"` : ctx.targetDirectoryName}`;
  34. console.log(` ${chalk.bold(chalk.green(cdCommand))}`);
  35. }
  36. // Log next steps: Install dependencies.
  37. const installCommand = ctx.getNpmCommand('install');
  38. console.log(` ${chalk.bold(chalk.green(installCommand))}`);
  39. // Log next steps: Generate Idls and clients.
  40. const generateCommand = ctx.getNpmCommand('generate');
  41. console.log(` ${chalk.bold(chalk.green(generateCommand))}`);
  42. // Final line break.
  43. console.log();
  44. }
  45. export async function spinner<T>(callback: () => T): Promise<T>;
  46. export async function spinner<T>(title: string, callback: () => T): Promise<T>;
  47. export async function spinner<T>(
  48. title: string | (() => T),
  49. callback?: () => T
  50. ): Promise<T> {
  51. if (typeof title == 'function') {
  52. callback = title;
  53. title = '';
  54. }
  55. let i = 0;
  56. const spin = () =>
  57. process.stderr.write(` ${'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[i++ % 10]} ${title}\r`);
  58. const id = setInterval(spin, 100);
  59. let result: T;
  60. try {
  61. result = await callback!();
  62. } finally {
  63. clearInterval(id as NodeJS.Timeout);
  64. process.stderr.write(' '.repeat(process.stdout.columns - 1) + '\r');
  65. }
  66. return result;
  67. }
  68. function getBanner() {
  69. const textBanner = 'Create Solana Program';
  70. const gradientBanner = chalk.bold(
  71. gradient(['#89d7c8', '#dc7a8b'])(textBanner, {
  72. interpolation: 'hsv',
  73. hsvSpin: 'long',
  74. })
  75. );
  76. return process.stdout.isTTY && process.stdout.getColorDepth() > 8
  77. ? gradientBanner
  78. : textBanner;
  79. }