gitConfig.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { spawn } from 'child_process';
  2. function spawnAsync(command, args) {
  3. return new Promise((resolve, reject) => {
  4. const child = spawn(command, args, { shell: false });
  5. let stdout = '';
  6. child.stdout.on('data', (data) => { stdout += data.toString(); });
  7. child.on('error', (error) => { reject(error); });
  8. child.on('close', (code) => {
  9. if (code === 0) { resolve({ stdout }); return; }
  10. reject(new Error(`Command failed with code ${code}`));
  11. });
  12. });
  13. }
  14. /**
  15. * Read git configuration from system's global git config
  16. * @returns {Promise<{git_name: string|null, git_email: string|null}>}
  17. */
  18. export async function getSystemGitConfig() {
  19. try {
  20. const [nameResult, emailResult] = await Promise.all([
  21. spawnAsync('git', ['config', '--global', 'user.name']).catch(() => ({ stdout: '' })),
  22. spawnAsync('git', ['config', '--global', 'user.email']).catch(() => ({ stdout: '' }))
  23. ]);
  24. return {
  25. git_name: nameResult.stdout.trim() || null,
  26. git_email: emailResult.stdout.trim() || null
  27. };
  28. } catch (error) {
  29. return { git_name: null, git_email: null };
  30. }
  31. }