Skip to content

Instantly share code, notes, and snippets.

@camacho
Last active October 4, 2019 13:37
Show Gist options
  • Save camacho/dd2dbcf00a53bc4bcaf91acaa449bd17 to your computer and use it in GitHub Desktop.
Save camacho/dd2dbcf00a53bc4bcaf91acaa449bd17 to your computer and use it in GitHub Desktop.
A node script to make commits every 5 minutes
#!/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);
}
});
}
@camacho
Copy link
Author

camacho commented Oct 4, 2019

The objective was to use only native node.js πŸ€“.

I usually run this on an experiment branch to keep the noise down on the true working branch πŸ”‡.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment