logs.ts 2.6 KB

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