Last active
October 4, 2019 13:37
-
-
Save camacho/dd2dbcf00a53bc4bcaf91acaa449bd17 to your computer and use it in GitHub Desktop.
A node script to make commits every 5 minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 π.