• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

background

Project source: github.com/chris-zhu/a…

If you’re still worried about your Github submission record, this script will automate submission. Keep your Githubgreen for 365 days.

Of course, this is just a lazy way to do it, but I hope you can submit your code efficiently.

script

The principle of

Change the file contents through daily scheduled tasks, and then commit them using SSH

Question: How do you automate submission?

thinking

  • The premise of submission is that the document needs to be changed so thatgitFound file changes
  • git commitusehttpLink the way, will frequently enter the account password
  • How to automatically help me submit every day

With the above problems, I began the road of small automation projects

File reading and writing

In order not to interfere with the logic and structure of the project itself, and to let Git know about the change of the file, I created an invalid file named test. TXT in the project directory for change and change, so that Git could detect it.

Each commit we need to read and write to the file and update it with changes.

/** * async function read2write() {let data = await fs.readfilesync (path.resolve(__dirname, '.. /test.txt'), { encoding: 'utf-8' }) data += `this is my ${count++} commit ---> ${new Date()} \r\n` await fs.writeFileSync(path.resolve(__dirname, '.. /test.txt'), data) }Copy the code

ssh

Git Clone project address: Git Clone project address: Git Clone project address: Git Clone project address: Git Clone project address: Git Clone

To generate an SSH key, see # Github: Add an SSH Key

So, when you’re done, you’re more than halfway done.

Scheduled Task Submission

When I went to learn about timed tasks, I found a timed task library, where I chose toad-Scheduler as the development, and the functions of the library are mainly as follows

  • Perform this operation at an intervaltask
  • multipletaskmanagement
  • .

It was developed because it suited the project

First we create a task scheduling center, and then we create a stop task function to facilitate the termination of the task scheduling

import { ToadScheduler } from 'toad-scheduler'

export const scheduler = new ToadScheduler()

export function stopAll() {
  scheduler.stop()
}
Copy the code

With the mid-stage set up, we just need to create tasks for scheduling to execute.

Const config = {taskName: 'commit_task', // schedule: {hours: 10, runImmediately: Const task = new AsyncTask(config.taskName, Export const job = new SimpleIntervalJob(config.schedule, task)Copy the code

In the code above, we define the execution cycle of the task. The task is executed every 10 hours, and the work done in the task is encapsulated as a work

Let’s take a look at what work does.

Async function work() {await read2write() {await handleGit()}Copy the code

Git commit

HandleGit handles a series of git processes, and now that we’ve learned how to use ZX, we can better manipulate Git through javascript.

*/ async function handleGit() {await $' git add. 'await $' git commit -m 'This is a revision submission at ${count}'` await $`git pull` await $`git push` console.log('done') }Copy the code

Since we created SSH for the commit, we shouldn’t be too surprised.

conclusion

When we put the script on the server, we fully automated the submission of the script. No more worrying about forgetting to commit every day.

Sublimation: Scripts are just for slackers like me, so I hope you can commit as much as possible.

Project source: github.com/chris-zhu/a…