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

background

Let’s say you create a lot of repositories locally and run into problems reinstalling your system. Local projects are lost altogether, and when you want to develop, you have to clone the repository again, which is a repeat of the steps, a bit against the idea of encapsulation. Thus, this script was born.

And if you read my last article, you know about zX, the tool library on which this project is based. There is no easy way to learn how to use Javascript to elegantly write script commands.

To prepare

The installationzx

Set up the project, install ZX, and create a ZX executable

#! /usr/bin/env zxCopy the code

To understandgithub api

This article uses the Repositories API for reference

instructions URL note
The request url Api.github.com/users/ {user…
Parameter 1 affiliation Subordinate relations
Parameter 2 direcion Sorting mode: ASC ascending, DESC descending
Parameter 3 page paging
Four parameters sort
Parameter 5 visibility Subordinate relations

This article uses only one of these apis. See # Build app-Github Open API for more details

script

Get warehouse list

Let’s call the Github API directly. What does it print out

= 'Chris - zhu' const user / / making user name const data = await the fetch (` {user} https://api.github.com/users/$/ repos `) const urls  = await data.json()Copy the code

We can see several details, first of all, the interface returns a warehouse information [], each warehouse information describes the details of the warehouse, the warehouse ID, name, URL, description, fork… so we can filter useful information, For example, we only want the original (not fork) item for ${user}

So we screen

const repos = urls.filter((info) => ! info.fork).map((info) => info.git_url)Copy the code

Creating a folder

We need to create a folder to hold clone projects and use the CD () command to access this folder

await $`rm -rf ${dir}`
await $`mkdir ${dir}`
cd(`./${dir}`)
Copy the code

clone

We can use it in conjunction with promise to loop and pull our warehouse address

Promise.all(repos.map((url) => $`git clone ${url}`))
Copy the code

We have finished all the clone script writing, of course we can be more flexible to pass parameters, better call so we can configure the project in package.json.

Parameter configuration

1 Modify the configuration file

Go to package.json and switch the user name of the address to clone

"scripts": {
    "clone": "npx zx src/clone/index.mjs"
 },

"config": {
    "port": "1111",
    "dir": "repos",
    "user": "CHANGE YOUR GITHUB_USERNAME"
},
Copy the code

Then, using script commands

npm run clone

// or

yarn run clone
Copy the code

② Use script commands to transmit parameters

It is injected through command parameter transmission

npm run clone -- xxx // xxx is your github_username

or

yarn run clone -- xxx // xxx is your github_username

Copy the code

Script accepts arguments

let [, , , User] = process. Argv user = user | | process. The env. Npm_package_config_user / / clone user name const dir = Process.env.npm_package_config_dir // The folder where the clone directory is storedCopy the code

Perfect.