Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. const readline = require('readline');
  4. const { execFileSync } = require('child_process');
  5.  
  6. const [minutes, yes] = process.argv.slice(2);
  7. const interval = 60 * 1000 * minutes;
  8. const cwd = process.cwd();
  9.  
  10. if (!minutes || parseInt(minutes) === NaN) {
  11. console.error('Add a (real) minutes argument e.g. <./gaca 5>');
  12. process.exit(1);
  13. }
  14.  
  15. let timeout;
  16. process.on('exit', () => clearTimeout(timeout));
  17. process.stdin.resume();
  18.  
  19. const save = () => {
  20. console.log('🔍 Checking for changes');
  21. const changedFiles = execFileSync('git', ['status', '--porcelain'], {
  22. cwd,
  23. encoding: 'utf8',
  24. });
  25.  
  26. if (!changedFiles) {
  27. console.log('🍽 No changes were found');
  28. } else {
  29. console.log('🙌 New changes found');
  30. execFileSync('git', ['add', '--all'], { cwd, encoding: 'utf8' });
  31.  
  32. console.log('👩‍🍳 Making a fresh commit');
  33. execFileSync(
  34. 'git',
  35. [
  36. 'commit',
  37. '--allow-empty',
  38. '-nm',
  39. `"auto commit at ${new Date().toJSON()}"`,
  40. ],
  41. { cwd, encoding: 'utf8' }
  42. );
  43. console.log('🧁 New commit added');
  44. }
  45.  
  46. console.log(
  47. `⏲ Another commit is scheduled to be made ${minutes} minute${
  48. minutes === 1 ? '' : 's'
  49. } from ${new Date().toJSON()} (roughly)`
  50. );
  51. timeout = setTimeout(save, interval);
  52. };
  53.  
  54. const start = () => {
  55. try {
  56. save();
  57. } catch (error) {
  58. console.error(error);
  59. process.exit((error && error.exitCode) || 1);
  60. }
  61. };
  62.  
  63. {
  64. const rl = readline.createInterface(process.stdin, process.stdout);
  65. rl.question('Are you sure (Y/n)? ', (answer = 'Y') => {
  66. rl.close();
  67.  
  68. if (['Y', ''].includes(answer.trim().toUpperCase())) {
  69. start();
  70. } else {
  71. console.log('😑 no auto commits scheduled');
  72. process.exit(0);
  73. }
  74. });
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement