No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

golang.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const util_1 = require("util");
  4. const fs = require("fs");
  5. const path = require("path");
  6. const spawn = require("cross-spawn");
  7. const clc = require("colorette");
  8. const error_1 = require("../../../error");
  9. const prompt_1 = require("../../../prompt");
  10. const utils = require("../../../utils");
  11. const go = require("../../../deploy/functions/runtimes/golang");
  12. const logger_1 = require("../../../logger");
  13. const RUNTIME_VERSION = "1.13";
  14. const TEMPLATE_ROOT = path.resolve(__dirname, "../../../../templates/init/functions/golang");
  15. const MAIN_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "functions.go"), "utf8");
  16. const GITIGNORE_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "_gitignore"), "utf8");
  17. async function init(setup, config) {
  18. await writeModFile(config);
  19. const modName = config.get("functions.go.module");
  20. const [pkg] = modName.split("/").slice(-1);
  21. await config.askWriteProjectFile("functions/functions.go", MAIN_TEMPLATE.replace("PACKAGE", pkg));
  22. await config.askWriteProjectFile("functions/.gitignore", GITIGNORE_TEMPLATE);
  23. }
  24. async function writeModFile(config) {
  25. const modPath = config.path("functions/go.mod");
  26. if (await (0, util_1.promisify)(fs.exists)(modPath)) {
  27. const shoudlWriteModFile = await (0, prompt_1.promptOnce)({
  28. type: "confirm",
  29. message: "File " + clc.underline("functions/go.mod") + " already exists. Overwrite?",
  30. default: false,
  31. });
  32. if (!shoudlWriteModFile) {
  33. return;
  34. }
  35. await (0, util_1.promisify)(fs.unlink)(modPath);
  36. }
  37. const modName = await (0, prompt_1.promptOnce)({
  38. type: "input",
  39. message: "What would you like to name your module?",
  40. default: "acme.com/functions",
  41. });
  42. config.set("functions.go.module", modName);
  43. config.writeProjectFile("functions/go.mod", `module ${modName} \n\ngo ${RUNTIME_VERSION}\n\n`);
  44. utils.logSuccess("Wrote " + clc.bold("functions/go.mod"));
  45. for (const dep of [go.FUNCTIONS_SDK, go.ADMIN_SDK, go.FUNCTIONS_CODEGEN, go.FUNCTIONS_RUNTIME]) {
  46. const result = spawn.sync("go", ["get", dep], {
  47. cwd: config.path("functions"),
  48. stdio: "inherit",
  49. });
  50. if (result.error) {
  51. logger_1.logger.debug("Full output from go get command:", JSON.stringify(result, null, 2));
  52. throw new error_1.FirebaseError("Error installing dependencies", { children: [result.error] });
  53. }
  54. }
  55. utils.logSuccess("Installed dependencies");
  56. }
  57. module.exports = init;