|
| 1 | +#! /usr/bin/env node |
| 2 | +const spawn = require("child_process").spawn |
| 3 | +const yesno = require("yesno"); |
| 4 | +const parse = require("parse-duration"); |
| 5 | +// import { spawn } from "child_process"; |
| 6 | +// import parse from "parse-duration"; |
| 7 | +// import yesno from "yesno"; |
| 8 | +const myArgs = process.argv.slice(2); |
| 9 | + |
| 10 | +if(!myArgs || myArgs.length < 1) exitPrintingHelp(); |
| 11 | + |
| 12 | +// process args |
| 13 | +const firstArg = myArgs[0]; |
| 14 | +myArgs.shift(); |
| 15 | +myArgs.unshift("commit"); |
| 16 | + |
| 17 | +// parse time |
| 18 | +const msShift = parse(firstArg); |
| 19 | +if (!msShift) { |
| 20 | + exitPrintingHelp("Invalid time format"); |
| 21 | +} |
| 22 | + |
| 23 | +// prepare future date |
| 24 | +const nowMillis = Date.now(); |
| 25 | +const futureMillis = |
| 26 | + msShift && !isNaN(msShift) ? nowMillis + msShift : nowMillis; |
| 27 | +const futureDate = new Date(futureMillis); |
| 28 | + |
| 29 | +// confirm and commit |
| 30 | +seekConfirmation(futureDate); |
| 31 | + |
| 32 | + |
| 33 | +// functions below |
| 34 | + |
| 35 | +// prompt to confirm |
| 36 | +async function seekConfirmation(futureDate) { |
| 37 | + console.log("Commit time: ", futureDate.toLocaleString()); |
| 38 | + const ok = await yesno({ |
| 39 | + question: "Continue?", |
| 40 | + }); |
| 41 | + if (ok) gitCommit(); |
| 42 | +} |
| 43 | + |
| 44 | +// run git command |
| 45 | +function gitCommit() { |
| 46 | + // Git format |
| 47 | + // GIT_AUTHOR_DATE='2021-12-21 21:03' GIT_COMMITTER_DATE='2021-12-21 21:03' git commit -m 'msg' |
| 48 | + |
| 49 | + const formattedDateWithQuotes = "'" + formatDate(futureDate) + "'"; |
| 50 | + |
| 51 | + try { |
| 52 | + var child = spawn("git", myArgs, { |
| 53 | + env: { |
| 54 | + ...process.env, |
| 55 | + GIT_AUTHOR_DATE: formattedDateWithQuotes, |
| 56 | + GIT_COMMITTER_DATE: formattedDateWithQuotes, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + //spit stdout to screen |
| 61 | + child.stdout.on("data", function (data) { |
| 62 | + process.stdout.write(data.toString()); |
| 63 | + }); |
| 64 | + |
| 65 | + //spit stderr to screen |
| 66 | + child.stderr.on("data", function (data) { |
| 67 | + process.stdout.write(data.toString()); |
| 68 | + }); |
| 69 | + |
| 70 | + child.on("close", function (code) { |
| 71 | + console.log(code === 0 ? "See you in the future!": "Back to present!"); |
| 72 | + }); |
| 73 | + } catch (error) { |
| 74 | + console.log(error); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// function to format date in '2021-12-21 21:03' format |
| 79 | +function formatDate(date) { |
| 80 | + const year = date.getFullYear(); |
| 81 | + const month = date.getMonth() + 1; |
| 82 | + const day = date.getDate(); |
| 83 | + const hours = date.getHours(); |
| 84 | + const minutes = date.getMinutes(); |
| 85 | + return `${year}-${month}-${day} ${hours}:${minutes}`; |
| 86 | +} |
| 87 | + |
| 88 | +function exitPrintingHelp(msg) { |
| 89 | + if (msg) { |
| 90 | + console.log(""); |
| 91 | + console.log("Error: " + msg); |
| 92 | + console.log(""); |
| 93 | + } |
| 94 | + printHelp(); |
| 95 | + process.exit(1); |
| 96 | +} |
| 97 | + |
| 98 | +function printHelp() { |
| 99 | + console.log("-------------------"); |
| 100 | + console.log("Usage: future-commit <time> -m <message>"); |
| 101 | + console.log('Example: future-commit 1h -m "Commit message"'); |
| 102 | + console.log('Example: future-commit 1h30m -m "Commit message"'); |
| 103 | + // log blank line |
| 104 | + console.log(""); |
| 105 | + console.log( |
| 106 | + "Tip: You can pass any other git commit arguments after the time" |
| 107 | + ); |
| 108 | + console.log("-------------------"); |
| 109 | +} |
0 commit comments