Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- const readline = require('readline');
- const { execFileSync } = require('child_process');
- const [minutes, yes] = process.argv.slice(2);
- const interval = 60 * 1000 * minutes;
- const cwd = process.cwd();
- if (!minutes || parseInt(minutes) === NaN) {
- console.error('Add a (real) minutes argument e.g. <./gaca 5>');
- process.exit(1);
- }
- let timeout;
- process.on('exit', () => clearTimeout(timeout));
- process.stdin.resume();
- const save = () => {
- console.log('🔍 Checking for changes');
- const changedFiles = execFileSync('git', ['status', '--porcelain'], {
- cwd,
- encoding: 'utf8',
- });
- if (!changedFiles) {
- console.log('🍽 No changes were found');
- } else {
- console.log('🙌 New changes found');
- execFileSync('git', ['add', '--all'], { cwd, encoding: 'utf8' });
- console.log('👩🍳 Making a fresh commit');
- execFileSync(
- 'git',
- [
- 'commit',
- '--allow-empty',
- '-nm',
- `"auto commit at ${new Date().toJSON()}"`,
- ],
- { cwd, encoding: 'utf8' }
- );
- console.log('🧁 New commit added');
- }
- console.log(
- `⏲ Another commit is scheduled to be made ${minutes} minute${
- minutes === 1 ? '' : 's'
- } from ${new Date().toJSON()} (roughly)`
- );
- timeout = setTimeout(save, interval);
- };
- const start = () => {
- try {
- save();
- } catch (error) {
- console.error(error);
- process.exit((error && error.exitCode) || 1);
- }
- };
- {
- const rl = readline.createInterface(process.stdin, process.stdout);
- rl.question('Are you sure (Y/n)? ', (answer = 'Y') => {
- rl.close();
- if (['Y', ''].includes(answer.trim().toUpperCase())) {
- start();
- } else {
- console.log('😑 no auto commits scheduled');
- process.exit(0);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement