설명 없음
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.

ext-dev-init.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.command = void 0;
  4. const fs = require("fs");
  5. const path = require("path");
  6. const { marked } = require("marked");
  7. const TerminalRenderer = require("marked-terminal");
  8. const checkMinRequiredVersion_1 = require("../checkMinRequiredVersion");
  9. const command_1 = require("../command");
  10. const config_1 = require("../config");
  11. const error_1 = require("../error");
  12. const prompt_1 = require("../prompt");
  13. const logger_1 = require("../logger");
  14. const npmDependencies = require("../init/features/functions/npm-dependencies");
  15. marked.setOptions({
  16. renderer: new TerminalRenderer(),
  17. });
  18. const TEMPLATE_ROOT = path.resolve(__dirname, "../../templates/extensions/");
  19. const FUNCTIONS_ROOT = path.resolve(__dirname, "../../templates/init/functions/");
  20. function readCommonTemplates() {
  21. return {
  22. extSpecTemplate: fs.readFileSync(path.join(TEMPLATE_ROOT, "extension.yaml"), "utf8"),
  23. preinstallTemplate: fs.readFileSync(path.join(TEMPLATE_ROOT, "PREINSTALL.md"), "utf8"),
  24. postinstallTemplate: fs.readFileSync(path.join(TEMPLATE_ROOT, "POSTINSTALL.md"), "utf8"),
  25. changelogTemplate: fs.readFileSync(path.join(TEMPLATE_ROOT, "CHANGELOG.md"), "utf8"),
  26. };
  27. }
  28. exports.command = new command_1.Command("ext:dev:init")
  29. .description("initialize files for writing an extension in the current directory")
  30. .before(checkMinRequiredVersion_1.checkMinRequiredVersion, "extDevMinVersion")
  31. .action(async (options) => {
  32. const cwd = options.cwd || process.cwd();
  33. const config = new config_1.Config({}, { projectDir: cwd, cwd: cwd });
  34. try {
  35. const lang = await (0, prompt_1.promptOnce)({
  36. type: "list",
  37. name: "language",
  38. message: "In which language do you want to write the Cloud Functions for your extension?",
  39. default: "javascript",
  40. choices: [
  41. {
  42. name: "JavaScript",
  43. value: "javascript",
  44. },
  45. {
  46. name: "TypeScript",
  47. value: "typescript",
  48. },
  49. ],
  50. });
  51. switch (lang) {
  52. case "javascript": {
  53. await javascriptSelected(config);
  54. break;
  55. }
  56. case "typescript": {
  57. await typescriptSelected(config);
  58. break;
  59. }
  60. default: {
  61. throw new error_1.FirebaseError(`${lang} is not supported.`);
  62. }
  63. }
  64. await npmDependencies.askInstallDependencies({}, config);
  65. const welcome = fs.readFileSync(path.join(TEMPLATE_ROOT, lang, "WELCOME.md"), "utf8");
  66. return logger_1.logger.info("\n" + marked(welcome));
  67. }
  68. catch (err) {
  69. if (!(err instanceof error_1.FirebaseError)) {
  70. throw new error_1.FirebaseError(`Error occurred when initializing files for new extension: ${err.message}`, {
  71. original: err,
  72. });
  73. }
  74. throw err;
  75. }
  76. });
  77. async function typescriptSelected(config) {
  78. const packageLintingTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "typescript", "package.lint.json"), "utf8");
  79. const packageNoLintingTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "typescript", "package.nolint.json"), "utf8");
  80. const tsconfigTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "typescript", "tsconfig.json"), "utf8");
  81. const tsconfigDevTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "typescript", "tsconfig.dev.json"), "utf8");
  82. const indexTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "typescript", "index.ts"), "utf8");
  83. const gitignoreTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "typescript", "_gitignore"), "utf8");
  84. const eslintTemplate = fs.readFileSync(path.join(FUNCTIONS_ROOT, "typescript", "_eslintrc"), "utf8");
  85. const lint = await (0, prompt_1.promptOnce)({
  86. name: "lint",
  87. type: "confirm",
  88. message: "Do you want to use ESLint to catch probable bugs and enforce style?",
  89. default: true,
  90. });
  91. const templates = readCommonTemplates();
  92. await config.askWriteProjectFile("extension.yaml", templates.extSpecTemplate);
  93. await config.askWriteProjectFile("PREINSTALL.md", templates.preinstallTemplate);
  94. await config.askWriteProjectFile("POSTINSTALL.md", templates.postinstallTemplate);
  95. await config.askWriteProjectFile("CHANGELOG.md", templates.changelogTemplate);
  96. await config.askWriteProjectFile("functions/src/index.ts", indexTemplate);
  97. if (lint) {
  98. await config.askWriteProjectFile("functions/package.json", packageLintingTemplate);
  99. await config.askWriteProjectFile("functions/.eslintrc.js", eslintTemplate);
  100. }
  101. else {
  102. await config.askWriteProjectFile("functions/package.json", packageNoLintingTemplate);
  103. }
  104. await config.askWriteProjectFile("functions/tsconfig.json", tsconfigTemplate);
  105. if (lint) {
  106. await config.askWriteProjectFile("functions/tsconfig.dev.json", tsconfigDevTemplate);
  107. }
  108. await config.askWriteProjectFile("functions/.gitignore", gitignoreTemplate);
  109. }
  110. async function javascriptSelected(config) {
  111. const indexTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "javascript", "index.js"), "utf8");
  112. const packageLintingTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "javascript", "package.lint.json"), "utf8");
  113. const packageNoLintingTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "javascript", "package.nolint.json"), "utf8");
  114. const gitignoreTemplate = fs.readFileSync(path.join(TEMPLATE_ROOT, "javascript", "_gitignore"), "utf8");
  115. const eslintTemplate = fs.readFileSync(path.join(FUNCTIONS_ROOT, "javascript", "_eslintrc"), "utf8");
  116. const lint = await (0, prompt_1.promptOnce)({
  117. name: "lint",
  118. type: "confirm",
  119. message: "Do you want to use ESLint to catch probable bugs and enforce style?",
  120. default: false,
  121. });
  122. const templates = readCommonTemplates();
  123. await config.askWriteProjectFile("extension.yaml", templates.extSpecTemplate);
  124. await config.askWriteProjectFile("PREINSTALL.md", templates.preinstallTemplate);
  125. await config.askWriteProjectFile("POSTINSTALL.md", templates.postinstallTemplate);
  126. await config.askWriteProjectFile("CHANGELOG.md", templates.changelogTemplate);
  127. await config.askWriteProjectFile("functions/index.js", indexTemplate);
  128. if (lint) {
  129. await config.askWriteProjectFile("functions/package.json", packageLintingTemplate);
  130. await config.askWriteProjectFile("functions/.eslintrc.js", eslintTemplate);
  131. }
  132. else {
  133. await config.askWriteProjectFile("functions/package.json", packageNoLintingTemplate);
  134. }
  135. await config.askWriteProjectFile("functions/.gitignore", gitignoreTemplate);
  136. }