executor.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. var desc = Object.getOwnPropertyDescriptor(m, k);
  12. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  13. desc = { enumerable: true, get: function() { return m[k]; } };
  14. }
  15. Object.defineProperty(o, k2, desc);
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || function (mod) {
  26. if (mod && mod.__esModule) return mod;
  27. var result = {};
  28. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  29. __setModuleDefault(result, mod);
  30. return result;
  31. };
  32. Object.defineProperty(exports, "__esModule", { value: true });
  33. exports.default = default_1;
  34. const core_1 = require("@angular-devkit/core");
  35. const child_process_1 = require("child_process");
  36. const path = __importStar(require("path"));
  37. function default_1(factoryOptions = {}) {
  38. const rootDirectory = factoryOptions.rootDirectory || process.cwd();
  39. return async (options = {}, context) => {
  40. const authorName = options.authorName;
  41. const authorEmail = options.authorEmail;
  42. const execute = (args, ignoreErrorStream) => {
  43. const outputStream = 'ignore';
  44. const errorStream = ignoreErrorStream ? 'ignore' : process.stderr;
  45. const spawnOptions = {
  46. stdio: [process.stdin, outputStream, errorStream],
  47. shell: true,
  48. cwd: path.join(rootDirectory, options.workingDirectory || ''),
  49. env: {
  50. ...process.env,
  51. ...(authorName ? { GIT_AUTHOR_NAME: authorName, GIT_COMMITTER_NAME: authorName } : {}),
  52. ...(authorEmail
  53. ? { GIT_AUTHOR_EMAIL: authorEmail, GIT_COMMITTER_EMAIL: authorEmail }
  54. : {}),
  55. },
  56. };
  57. return new Promise((resolve, reject) => {
  58. (0, child_process_1.spawn)('git', args, spawnOptions).on('close', (code) => {
  59. if (code === 0) {
  60. resolve();
  61. }
  62. else {
  63. reject(code);
  64. }
  65. });
  66. });
  67. };
  68. const hasCommand = await execute(['--version']).then(() => true, () => false);
  69. if (!hasCommand) {
  70. return;
  71. }
  72. const insideRepo = await execute(['rev-parse', '--is-inside-work-tree'], true).then(() => true, () => false);
  73. if (insideRepo) {
  74. context.logger.info(core_1.tags.oneLine `
  75. Directory is already under version control.
  76. Skipping initialization of git.
  77. `);
  78. return;
  79. }
  80. // if git is not found or an error was thrown during the `git`
  81. // init process just swallow any errors here
  82. // NOTE: This will be removed once task error handling is implemented
  83. try {
  84. await execute(['init']);
  85. await execute(['add', '.']);
  86. if (options.commit) {
  87. const message = options.message || 'initial commit';
  88. await execute(['commit', `-m "${message}"`]);
  89. }
  90. context.logger.info('Successfully initialized git.');
  91. }
  92. catch { }
  93. };
  94. }