暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

welcome.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const spawn = require("child_process").spawn;
  2. const fork = require("child_process").fork;
  3. const readline = require("readline");
  4. const chalk = require("chalk");
  5. const banner = ` ######## #### ######## ######## ######## ### ###### ######## ##
  6. ## ## ## ## ## ## ## ## ## ## ## ##
  7. ###### ## ######## ###### ######## ######### ###### ###### ##
  8. ## ## ## ## ## ## ## ## ## ## ##
  9. ## #### ## ## ######## ######## ## ## ###### ######## ##
  10. `;
  11. const firebase_exe = process.argv[2];
  12. let firebase_bin;
  13. (async () => {
  14. const line = new Array(process.stdout.columns)
  15. .fill(undefined)
  16. .map(() => "#")
  17. .join("");
  18. const thin_line = new Array(process.stdout.columns)
  19. .fill(undefined)
  20. .map(() => "-")
  21. .join("");
  22. console.log(line);
  23. console.log("");
  24. console.log(" Welcome to...");
  25. console.log(chalk.yellow(banner));
  26. console.log("");
  27. console.log(line);
  28. console.log("");
  29. await CheckFirebaseTools();
  30. console.log("");
  31. await CheckIsLoggedIn();
  32. console.log("");
  33. console.log(
  34. `${chalk.green("+")} You can now use the 'firebase' or 'npm' commands!`
  35. );
  36. console.log(
  37. `${chalk.blue("~")} For more help see https://firebase.google.com/docs/cli/`
  38. );
  39. console.log("");
  40. console.log(thin_line);
  41. process.exit();
  42. })();
  43. async function CheckFirebaseTools() {
  44. console.log(
  45. `${chalk.cyan("~")} Let's make sure your Firebase CLI is ready...`
  46. );
  47. firebase_bin = await GetFirebaseToolsBins();
  48. const isSetup = !!firebase_bin;
  49. if (isSetup) {
  50. console.log(
  51. `${chalk.green("+")} Looks like ${chalk.green("your CLI is set up")}!`
  52. );
  53. } else {
  54. console.log(
  55. `${chalk.cyan("~")} Looks like ${chalk.yellow(
  56. "your CLI needs to be set up"
  57. )}.\n`
  58. );
  59. console.log(`${chalk.cyan("~")} This may take a moment`);
  60. process.stdout.write(`${chalk.green("+")} Setting up environment...`);
  61. return new Promise(resolve => {
  62. const exe_split = firebase_exe.split(" ");
  63. const install = spawn(
  64. exe_split[0],
  65. [...exe_split.slice(1), "--tool:force-setup"],
  66. {}
  67. );
  68. install.stderr.on("data", buf => {
  69. readline.clearLine(process.stdout, 2);
  70. readline.cursorTo(process.stdout, 2, null);
  71. const line = (
  72. buf
  73. .toString()
  74. .split("\n")
  75. .filter(l => l)
  76. .slice(-1)[0] || ""
  77. )
  78. .trim()
  79. .slice(0, process.stdout.columns - 5);
  80. process.stdout.write(line);
  81. });
  82. install.on("exit", () => {
  83. readline.clearLine(process.stdout, 0);
  84. readline.cursorTo(process.stdout, 0, null);
  85. process.stdout.write(
  86. `${chalk.green("+")} Alright, ${chalk.green("your CLI is set up")}!\n`
  87. );
  88. resolve();
  89. });
  90. });
  91. }
  92. }
  93. async function GetFirebaseToolsBins() {
  94. return new Promise(resolve => {
  95. const exe_split = firebase_exe.split(" ");
  96. const checkSpawn = spawn(exe_split[0], [
  97. ...exe_split.slice(1),
  98. "--tool:setup-check"
  99. ]);
  100. let checkSpawnData = "";
  101. checkSpawn.stdout.on("data", buf => {
  102. checkSpawnData += buf;
  103. });
  104. checkSpawn.on("close", () => {
  105. firebase_bin = JSON.parse(checkSpawnData).bins[0];
  106. resolve(firebase_bin);
  107. });
  108. });
  109. }
  110. async function CheckIsLoggedIn() {
  111. firebase_bin = firebase_bin || (await GetFirebaseToolsBins());
  112. return new Promise(resolve => {
  113. const finish = SimpleSpinner(
  114. `${chalk.cyan("~")} Checking your Firebase credentials`
  115. );
  116. const listSpawn = fork(firebase_bin, ["projects:list", "--json"], { silent: true });
  117. let listSpawnData = "";
  118. listSpawn.stdout.on("data", buf => {
  119. listSpawnData += buf;
  120. });
  121. listSpawn.on("close", () => {
  122. const isLoggedIn =
  123. JSON.parse(listSpawnData.toString()).status === "success";
  124. if (!isLoggedIn) {
  125. finish(
  126. "🚫",
  127. `${chalk.cyan(
  128. "~"
  129. )} Looks like you're not authenticated. ${chalk.yellow(
  130. "Let's log in"
  131. )}!\n`
  132. );
  133. } else {
  134. finish("", "");
  135. }
  136. const login = fork(firebase_bin, ["login"], { stdio: "inherit" });
  137. login.on("exit", () => {
  138. resolve(isLoggedIn);
  139. });
  140. });
  141. });
  142. }
  143. function SimpleSpinner(message) {
  144. const icons = ["[/]", "[-]", "[\\]", "[-]", "[|]"];
  145. let index = 0;
  146. const cancel = setInterval(() => {
  147. readline.clearLine(process.stdout, 0);
  148. readline.cursorTo(process.stdout, 0, null);
  149. process.stdout.write(message + chalk.white(icons[index]));
  150. index++;
  151. index %= icons.length;
  152. }, 300);
  153. return (_, final_message) => {
  154. clearInterval(cancel);
  155. readline.clearLine(process.stdout, 0);
  156. readline.cursorTo(process.stdout, 0, null);
  157. process.stdout.write(final_message);
  158. };
  159. }