executor.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 () {
  26. var ownKeys = function(o) {
  27. ownKeys = Object.getOwnPropertyNames || function (o) {
  28. var ar = [];
  29. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  30. return ar;
  31. };
  32. return ownKeys(o);
  33. };
  34. return function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. })();
  42. Object.defineProperty(exports, "__esModule", { value: true });
  43. exports.default = default_1;
  44. const core_1 = require("@angular-devkit/core");
  45. const node_child_process_1 = require("node:child_process");
  46. const path = __importStar(require("node:path"));
  47. function default_1(factoryOptions = {}) {
  48. const rootDirectory = factoryOptions.rootDirectory || process.cwd();
  49. return async (options = {}, context) => {
  50. const authorName = options.authorName;
  51. const authorEmail = options.authorEmail;
  52. const execute = (args, ignoreErrorStream) => {
  53. const outputStream = 'ignore';
  54. const errorStream = ignoreErrorStream ? 'ignore' : process.stderr;
  55. const spawnOptions = {
  56. stdio: [process.stdin, outputStream, errorStream],
  57. shell: true,
  58. cwd: path.join(rootDirectory, options.workingDirectory || ''),
  59. env: {
  60. ...process.env,
  61. ...(authorName ? { GIT_AUTHOR_NAME: authorName, GIT_COMMITTER_NAME: authorName } : {}),
  62. ...(authorEmail
  63. ? { GIT_AUTHOR_EMAIL: authorEmail, GIT_COMMITTER_EMAIL: authorEmail }
  64. : {}),
  65. },
  66. };
  67. return new Promise((resolve, reject) => {
  68. (0, node_child_process_1.spawn)('git', args, spawnOptions).on('close', (code) => {
  69. if (code === 0) {
  70. resolve();
  71. }
  72. else {
  73. reject(code);
  74. }
  75. });
  76. });
  77. };
  78. const hasCommand = await execute(['--version']).then(() => true, () => false);
  79. if (!hasCommand) {
  80. return;
  81. }
  82. const insideRepo = await execute(['rev-parse', '--is-inside-work-tree'], true).then(() => true, () => false);
  83. if (insideRepo) {
  84. context.logger.info(core_1.tags.oneLine `
  85. Directory is already under version control.
  86. Skipping initialization of git.
  87. `);
  88. return;
  89. }
  90. // if git is not found or an error was thrown during the `git`
  91. // init process just swallow any errors here
  92. // NOTE: This will be removed once task error handling is implemented
  93. try {
  94. await execute(['init']);
  95. await execute(['add', '.']);
  96. if (options.commit) {
  97. const message = options.message || 'initial commit';
  98. await execute(['commit', `-m "${message}"`]);
  99. }
  100. context.logger.info('Successfully initialized git.');
  101. }
  102. catch { }
  103. };
  104. }