Ei kuvausta
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.

lifecycleHooks.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.lifecycleHooks = void 0;
  4. const utils = require("../utils");
  5. const clc = require("colorette");
  6. const childProcess = require("child_process");
  7. const error_1 = require("../error");
  8. const needProjectId = require("../projectUtils").needProjectId;
  9. const logger_1 = require("../logger");
  10. const path = require("path");
  11. function runCommand(command, childOptions) {
  12. const escapedCommand = command.replace(/\"/g, '\\"');
  13. const translatedCommand = '"' +
  14. process.execPath +
  15. '" "' +
  16. path.resolve(require.resolve("cross-env"), "..", "bin", "cross-env-shell.js") +
  17. '" "' +
  18. escapedCommand +
  19. '"';
  20. return new Promise((resolve, reject) => {
  21. logger_1.logger.info("Running command: " + command);
  22. if (translatedCommand === "") {
  23. return resolve();
  24. }
  25. const child = childProcess.spawn(translatedCommand, [], childOptions);
  26. child.on("error", (err) => {
  27. reject(err);
  28. });
  29. child.on("exit", (code, signal) => {
  30. if (signal) {
  31. reject(new Error("Command terminated with signal " + signal));
  32. }
  33. else if (code !== 0) {
  34. reject(new Error("Command terminated with non-zero exit code " + code));
  35. }
  36. else {
  37. resolve();
  38. }
  39. });
  40. });
  41. }
  42. function getChildEnvironment(target, overallOptions, config) {
  43. var _a;
  44. const projectId = needProjectId(overallOptions);
  45. const projectDir = overallOptions.projectRoot;
  46. let resourceDir;
  47. switch (target) {
  48. case "hosting":
  49. resourceDir = overallOptions.config.path((_a = config.public) !== null && _a !== void 0 ? _a : config.source);
  50. break;
  51. case "functions":
  52. resourceDir = overallOptions.config.path(config.source);
  53. break;
  54. default:
  55. resourceDir = overallOptions.config.path(overallOptions.config.projectDir);
  56. }
  57. return Object.assign({}, process.env, {
  58. GCLOUD_PROJECT: projectId,
  59. PROJECT_DIR: projectDir,
  60. RESOURCE_DIR: resourceDir,
  61. });
  62. }
  63. function runTargetCommands(target, hook, overallOptions, config) {
  64. let commands = config[hook];
  65. if (!commands) {
  66. return Promise.resolve();
  67. }
  68. if (typeof commands === "string") {
  69. commands = [commands];
  70. }
  71. const childOptions = {
  72. cwd: overallOptions.config.projectDir,
  73. env: getChildEnvironment(target, overallOptions, config),
  74. shell: true,
  75. stdio: [0, 1, 2],
  76. };
  77. const runAllCommands = commands.reduce((soFar, command) => {
  78. return soFar.then(() => runCommand(command, childOptions));
  79. }, Promise.resolve());
  80. let logIdentifier = target;
  81. if (config.target) {
  82. logIdentifier += `[${config.target}]`;
  83. }
  84. return runAllCommands
  85. .then(() => {
  86. utils.logSuccess(clc.green(clc.bold(logIdentifier + ":")) +
  87. " Finished running " +
  88. clc.bold(hook) +
  89. " script.");
  90. })
  91. .catch((err) => {
  92. throw new error_1.FirebaseError(logIdentifier + " " + hook + " error: " + err.message);
  93. });
  94. }
  95. function getReleventConfigs(target, options) {
  96. let targetConfigs = options.config.get(target);
  97. if (!targetConfigs) {
  98. return [];
  99. }
  100. if (!Array.isArray(targetConfigs)) {
  101. targetConfigs = [targetConfigs];
  102. }
  103. if (!options.only) {
  104. return targetConfigs;
  105. }
  106. let onlyTargets = options.only.split(",");
  107. if (onlyTargets.includes(target)) {
  108. return targetConfigs;
  109. }
  110. onlyTargets = onlyTargets
  111. .filter((individualOnly) => {
  112. return individualOnly.indexOf(`${target}:`) === 0;
  113. })
  114. .map((individualOnly) => {
  115. return individualOnly.replace(`${target}:`, "");
  116. });
  117. return targetConfigs.filter((config) => {
  118. return !config.target || onlyTargets.includes(config.target);
  119. });
  120. }
  121. function lifecycleHooks(target, hook) {
  122. return function (context, options) {
  123. return getReleventConfigs(target, options).reduce((previousCommands, individualConfig) => {
  124. return previousCommands.then(() => {
  125. return runTargetCommands(target, hook, options, individualConfig);
  126. });
  127. }, Promise.resolve());
  128. };
  129. }
  130. exports.lifecycleHooks = lifecycleHooks;