executor.js 2.9 KB

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